我在使用以下代码运行宏时出现错误消息。
直接从Excel工作簿运行时出现错误消息
我们没有填写值,因为活动列中某些单元格的显示格式使用不同的精度基础值
通过VBA运行时出现错误消息
运行时错误9,下标超出范围,
代码With Windows("InstData_TEMS_Existing").Sheets("L")
Sub Graph()
'
' Graph Macro
'
' Keyboard Shortcut: Ctrl+e
'
'Select values in a column from specified workbook and sheet
Dim LR As Long, cell As Range, rng As Range
With Workbooks("Area3-LG").Sheets("Graph data")
LR = .Range("B" & .Rows.Count).End(xlUp).Row
For Each cell In .Range("B4:B" & LR)
If cell.Value <> "" Then
If rng Is Nothing Then
Set rng = cell
Else
Set rng = Union(rng, cell)
End If
End If
Next cell
End With
rng.Copy ' copy the union range (no need to select it first)
'Paste without all the selecting
'Error code below
With Windows("InstData_TEMS_Existing").Sheets("L")
' Paste (without select) un the next empty cell fromn column AA
.Range("AA" & .Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End With
Application.CutCopyMode = False
'Go back to previous workbook & delete column
Workbooks("Area3-LG").Sheets("Graph data").Columns("B:B").Delete Shift:=xlToLeft
End Sub
答案 0 :(得分:0)
如果您已将 sktneer 建议更改为代码(使用With Workbooks("InstData_TEMS_Existing.xlsx").Sheets("L")
而不是With Windows(...
)并且您获得了下标超出范围的错误,则可能是文件InstData_TEMS_Existing.xlsx
未打开或其中没有工作表L
。我建议将2个Debug.Print语句置于with
语句之上。
Debug.print Workbooks("InstData_TEMS_Existing.xlsx").name
Debug.print Workbooks("InstData_TEMS_Existing.xlsx").sheets("L").name
然后将断点设置为代码的开头(F9
)并逐步执行代码(F8
)。如果您的范围很大,可以在rng.Copy
语句中设置另一个断点并让它运行(F5
)。观察运行时错误发生的位置。