我有一个电子表格,每天需要一个新文件时才提取数据。
它的职责是:
从(C列的)底部填充行开始
填写今天的日期并将其设置为静态
删除超过2个月的工作表顶部数据
我确定我可以完成其他两项任务,但是我正在努力争取把lastrow作为范围内的点
Dim ws As Worksheet, lastRow As Long
Set ws = Worksheets("Report")
lastRow = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row + 1 ' bottom populated cell of Column "C", plus 1
'Error on this "line" below
With ws.QueryTables.Add(Connection:= _
"TEXT;N:\etc\etc\etc\FMSQRY.CSV", Destination:= _
ws.Range(Cells(lastRow, 3)))
'Data Import here
End With
经典错误1004-对象工作表范围失败
但是当我使用相同的方法在不同的工作簿(和不同的项目)中使用lastRow选择N2:N
时,效果很好
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 'Finds the bottom populated row
With ws.Range(ws.Cells(2, 14), ws.Cells(LastRow, 14)) 'Selects N2:N
'magical formula
End With
答案 0 :(得分:2)
尝试此代码。检查单元格之前是否不需要该范围:
Dim ws As Worksheet, lastRow As Long
Set ws = Worksheets("Hoja1")
lastRow = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row + 1 ' bottom populated cell of Column "C", plus 1
With ws.QueryTables.Add(Connection:= _
"TEXT;N:\etc\etc\etc\FMSQRY.CSV", Destination:= _
ws.Cells(lastRow, 3))
'Data Import here
End With