我对VBA非常熟悉,所以这是一个非常奇怪的问题。我正在尝试在导入数据后自动填充单独的列范围。一切正常,A:C列自动正确填充,但AT列的自动填充会导致整个列清除。我确保正确的公式在AT2单元格中。我可能在代码中犯了一个愚蠢的错误,但是我很沮丧。任何帮助将不胜感激。
Dim wbCopy As Workbook
Dim wsCopy As Worksheet, wsPaste As Worksheet
Dim FileCopy As String
Dim rgCopy As Range, rgPaste As Range
Dim lRowPaste As Long
FileCopy = "C:\Users\Nick\File.csv"
Set wbCopy = Workbooks.Open(FileCopy)
Set wsCopy = wbCopy.Sheets(1)
Set wsPaste = Workbooks("Stats").Sheets("Logs")
Set rgCopy = wsCopy.Range("A1").CurrentRegion
Set rgCopy = rgCopy.Resize(rgCopy.Rows.Count - 1).Offset(1, 1)
Set rgPaste = wsPaste.Range("D2")
Set rgPaste = rgPaste.Resize(rgCopy.Rows.Count, rgCopy.Columns.Count)
rgPaste.Value2 = rgCopy.Value2
wbCopy.Close savechanges:=False
lRowPaste = wsPaste.Range("F" & Rows.Count).End(xlUp).Row
wsPaste.Range("A2:C" & lRowPaste).FillDown '-----> DOES WORK
wsPaste.Range("AT2").AutoFill Destination:=wsPaste.Range("AT2:AT" & lRowPaste) '-----> DOES NOT WORK
wsPaste.Range("AT2:AT" & lRowPaste).FillDown '-----> DOES NOT WORK
答案 0 :(得分:0)
我看不到任何问题,但是您可以尝试更换此部分:
With wsPaste
lRowPaste = .Range("F" & Rows.Count).End(xlUp).Row
.Range("A2:C" & lRowPaste).FillDown '-----> DOES WORK
.Range("AT2").AutoFill Destination:=.Range("AT2:AT" & lRowPaste) '-----> DOES NOT WORK
.Range("AT2:AT" & lRowPaste).FillDown '-----> DOES NOT WORK
End With
与此:
With wsPaste
lRowPaste = .Range("F" & Rows.Count).End(xlUp).Row
.Range("A2:C" & lRowPaste).FillDown '-----> DOES WORK
.Range("AT2").AutoFill Destination:=.Range("AT2:AT" & lRowPaste) '-----> DOES NOT WORK
.Range("AT2:AT" & lRowPaste).FillDown '-----> DOES NOT WORK
End With
并检查lastrow是否获得正确的值,以确认您使用的范围正确。