我正在尝试在数据末尾添加新列并自动填充函数。这是我的代码。我在自动填充部分出错了。方法类的范围失败。有人可以看一下吗?谢谢!
Sub Geocode()
'Add Lat and Long columns to the end of the report
Dim lastColumn As Long
Dim lastRow As Long
With Sheets("ReportResults 1")
lastColumn = .Cells(1, Columns.Count).End(xlToLeft).Column
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(1, lastColumn + 1).Value = "Location"
.Cells(1, lastColumn + 2).Value = "Latitude"
.Cells(1, lastColumn + 3).Value = "Longitude"
.Cells(2, lastColumn + 1).FormulaR1C1 = _
"=RC[-17]&"", ""&RC[-16]&"", ""&RC[-15]&"" ""&RC[-14]&"", USA"""
'auto fill formula
.Range(lastColumn + 1 & "2").Select
Selection.AutoFill Destination:=Range(lastColumn + 1 & "2:" & lastColumn + 1 & lastRow)
'copay paste value
Columns(lastColumn + 1).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
End Sub
答案 0 :(得分:0)
这些行会导致问题,因为它们的格式不合适:
.Range(lastColumn + 1 & "2").Select
Selection.AutoFill Destination:=Range(lastColumn + 1 & "2:" & lastColumn + 1 & lastRow)
而是试试这个:
.Cells(2, lastColumn + 1).Select
Selection.AutoFill Destination:=Range(Cells(2, lastColumn + 1), Cells(lastRow, lastColumn + 1))
甚至更好地在一条线上:
.Cells(2, lastColumn + 1).AutoFill Destination:=Range(Cells(2, lastColumn + 1), Cells(lastRow, lastColumn + 1))