VBA公式自动填充可独立工作,但不能与我的其余代码一起工作

时间:2019-01-11 19:14:36

标签: excel vba

我正在尝试在较大代码的末尾为vlookup添加自动填充。我编写的自动填充代码在其自己的模块中时可以按预期工作:公式从列的第二行开始,一直填充到最后一行。但是,当将其添加到较大代码的末尾时,似乎不再解释该范围。它将第一个公式放在标题所在的第一列中,仅将公式填充到第二行。

我发现,当我更改范围时,如果删除行号,或者如果更改数字,它只会增加或减少公式填充的行数,否则会出现运行时错误。

我试图删除上面较大的代码,然后重新运行自动填充,这继续导致了问题。我还尝试了分离潜艇,这也不起作用。似乎只有当它在与较大代码完全分离的新模块中时,它才起作用,而我不明白为什么。

Sub FacilitiesReport()

ActiveWorkbook.Windows(1).Caption = "Facilities Report"


Dim FilePath As String

FilePath = "C:\Users\12345\Desktop\FACILITIES.txt"

Open FilePath For Input As #1

row_number = 0

Do Until EOF(1)

    Line Input #1, LineFromFile

    LineItems = Split(LineFromFile, ";")

    ActiveCell.Offset(row_number, 0).Value = LineItems(0)
    ActiveCell.Offset(row_number, 1).Value = LineItems(1)
    ActiveCell.Offset(row_number, 2).Value = LineItems(2)
    ActiveCell.Offset(row_number, 3).Value = LineItems(3)
    ActiveCell.Offset(row_number, 4).Value = LineItems(4)
    ActiveCell.Offset(row_number, 5).Value = LineItems(5)
    ActiveCell.Offset(row_number, 6).Value = LineItems(6)
    ActiveCell.Offset(row_number, 7).Value = LineItems(7)
    ActiveCell.Offset(row_number, 8).Value = LineItems(8)
    ActiveCell.Offset(row_number, 9).Value = LineItems(9)
    ActiveCell.Offset(row_number, 10).Value = LineItems(10)
    ActiveCell.Offset(row_number, 11).Value = LineItems(11)
    ActiveCell.Offset(row_number, 12).Value = LineItems(12)
    ActiveCell.Offset(row_number, 13).Value = LineItems(13)
    ActiveCell.Offset(row_number, 14).Value = LineItems(14)
    ActiveCell.Offset(row_number, 15).Value = LineItems(15)
    ActiveCell.Offset(row_number, 16).Value = LineItems(16)
    ActiveCell.Offset(row_number, 17).Value = LineItems(17)
    ActiveCell.Offset(row_number, 18).Value = LineItems(18)
    ActiveCell.Offset(row_number, 19).Value = LineItems(19)
    ActiveCell.Offset(row_number, 20).Value = LineItems(20)
    ActiveCell.Offset(row_number, 21).Value = LineItems(21)
    ActiveCell.Offset(row_number, 22).Value = LineItems(22)
    ActiveCell.Offset(row_number, 23).Value = LineItems(23)
    ActiveCell.Offset(row_number, 24).Value = LineItems(24)
    ActiveCell.Offset(row_number, 25).Value = LineItems(25)
    ActiveCell.Offset(row_number, 26).Value = LineItems(26)
    ActiveCell.Offset(row_number, 27).Value = LineItems(27)
    ActiveCell.Offset(row_number, 28).Value = LineItems(28)
    ActiveCell.Offset(row_number, 29).Value = LineItems(29)
    ActiveCell.Offset(row_number, 30).Value = LineItems(30)
    ActiveCell.Offset(row_number, 31).Value = LineItems(31)
    ActiveCell.Offset(row_number, 32).Value = LineItems(32)
    ActiveCell.Offset(row_number, 33).Value = LineItems(33)
    ActiveCell.Offset(row_number, 34).Value = LineItems(34)

    row_number = row_number + 1

Loop

Close #1

With Range("A1:AI1").Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.249977111117893
    .PatternTintAndShade = 0
End With
    Range("A1:AL1").Font.Bold = True


    Range("$AJ$1").Value = "TWC"
    Range("$AK$1").Value = "CCC"
    Range("$AL$1").Value = "TWH"

 Columns("AJ:AL").Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With

Worksheets.Add After:=Worksheets(Worksheets.Count), Count:=1

Workbooks.Open Filename:="C:\Users\12345\Desktop\Facilities Report\FacilitiesReport-1.csv"

Windows("FacilitiesReport-1.csv").Activate
Range("A1:K1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Facilities Report").Activate
ActiveSheet.Paste
Selection.End(xlDown).Offset(1, 0).Select

Workbooks.Open Filename:="C:\Users\12345\Desktop\Facilities Report\FacilitiesReport-2.csv"

Windows("FacilitiesReport-2.csv").Activate
Range("A2:K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Facilities Report").Activate
ActiveSheet.Paste
Selection.End(xlDown).Offset(1, 0).Select

Workbooks.Open Filename:="C:\Users\12345\Desktop\Facilities Report\FacilitiesReport-3.csv"

Windows("FacilitiesReport-3.csv").Activate
Range("A2:K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Facilities Report").Activate
ActiveSheet.Paste

'-----------VLOOKUP FROM SHEET 2---------------------------------

Windows("Facilities Report").Activate
Worksheets("Sheet1").Activate
Dim rng As Range
Dim sht As Worksheet
Dim Lastrow As Long

Set sht = ThisWorkbook.Sheets("Sheet1")
Lastrow = sht.Cells(sht.Rows.Count, "L").End(xlUp).Row

Set rng = Range("AJ2:AJ" & Lastrow)
rng.Formula = "=VLOOKUP(RC[-24],Sheet2!C[-35]:C[-25],10,)"

End Sub

这可能是不必要的代码,但是我不确定问题出在哪里。自动填充是最后11行。

任何帮助都会很棒!

0 个答案:

没有答案