【发布时间】:2017-10-05 06:06:51
【问题描述】:
使用以下代码运行宏时出现错误消息。
直接从 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
【问题讨论】:
-
工作表“L”是否存在于活动工作簿中?
-
您是否尝试过使用 Workbooks("InstData_TEMS_Existing.xlsx").Sheets("L")?
-
Windows("InstData_TEMS_Existing.xlsx")...?或.xlsm或任何可能...或Windows("InstData_TEMS_Existing.xlsx":1).Sheets(... -
@QHarr 是的,它存在。
-
@Tyler 如果您尝试使用键盘快捷键运行它:Ctrl+E,那么这将不起作用,因为该快捷键已被称为Flash Fill 的内置 Excel 函数使用。这个函数导致错误不是你的宏!定义一个尚未被 Excel 使用的 new shortcut,它将起作用:List of shortcuts。
标签: vba excel compiler-errors