运行时错误'424' – Excel中的VBA
我对VBA世界非常陌生。
我想从不同的工作簿中的2个工作表中select2个范围。
但是我在剧本中得到了这个信息。 请帮忙
Sub CommandButton1_Click() Application.ScreenUpdating = False Dim wb1 As Workbook, wb2 As Workbook Dim ws1 As Worksheet, ws2 As Worksheet Dim rng1 As Range, rng2 As Range Set wb1 = Workbooks("TEMPLATE.xls") Set ws1 = Sheets("macro") Set rng1 = Range(Range("C3"), Range("C3").End(xlDown)).Select Set wb2 = Workbooks("booking.xlsx") Set ws2 = Sheets("ABC") Set rng2 = Range(Range("L3"), Range("L3").End(xlDown)).Select End Sub
尝试和整个testing。
我试图通过创build您的代码中提到的书籍和工作表来复制您的错误。 向macros表中添加一个CommandButton1并将其代码插入到Click事件处理程序中。
试图逐句通过代码没有selectmacros表上的范围,但扔了错误424(对象需要)和代码执行终止,没有突出显示在IDE中的任何东西。
更改:
Set rng1 = Range(Range("C3"), Range("C3").End(xlDown)).Select
至
Set rng1 = Range(Range("C3"), Range("C3").End(xlDown)) rng1.Select
摆脱了错误,但下一步通过Set ws2 = Sheets("ABC")
给出了错误9(下标超出范围Set ws2 = Sheets("ABC")
。 这是因为TEMPLATE.xls仍然是活动工作簿。
更改Set ws2 = Sheets("ABC")
Set ws2 = wb2.Sheets("ABC")
清除了此错误,但由于在Set Rng2
行上select了错误424。 此外,select了macros表中列L中的单元格。
更改:
Set rng2 = Range(Range("L3"), Range("L3").End(xlDown)).Select
至:
Set rng2 = Range(Range("L3"), Range("L3").End(xlDown)) rng2.Select
摆脱了那个错误,但macros单中的单元格仍然被选中。
为了解决我改变了:
Set rng2 = Range(Range("L3"), Range("L3").End(xlDown)) rng2.Select
至:
With ws2 Set rng2 = Range(.Range("L3"), .Range("L3").End(xlDown)) End With rng2.Select
我以为已经解决了,但后来得到了`错误1004(select方法的范围类失败),我只能通过激活bookings.xlsx!
您需要明确声明您的表格和范围:
Sub CommandButton1_Click() Dim wb1 As Workbook, wb2 As Workbook Dim ws1 As Worksheet, ws2 As Worksheet Dim rng1 As Range, rng2 As Range Application.ScreenUpdating = False Set wb1 = Workbooks("TEMPLATE.xls") Set ws1 = wb1.Sheets("macro") With ws1 Set rng1 = .Range(.Range("C3"), .Range("C3").End(xlDown)) rng1.Select End With Set wb2 = Workbooks("booking.xlsx") wb2.Activate Set ws2 = wb2.Sheets("ABC") With ws2 Set rng2 = .Range(.Range("L3"), .Range("L3").End(xlDown)) End With rng2.Select wb1.Activate Application.ScreenUpdating = True End Sub
我在代码中看到了多个问题。 首先,当您通过Sheets()属性select工作表时,默认情况下它将在当前激活的工作表(也称为ActiveWorkbook )工作表中查找。 select单元格范围(Range()函数)也是一样,在活动工作表中查找。 所以你必须在这种情况下更具体:
Dim wb1 As Workbook, wb2 As Workbook Dim ws1 As Worksheet, ws2 As Worksheet Dim rng1 As Range, rng2 As Range Set wb1 = Workbooks("TEMPLATE.xls") Set ws1 = wb1.Sheets("macro") Set rng1 = ws1.Range(Range("C3"), Range("C3").End(xlDown)) rng1.Select Set wb2 = Workbooks("booking.xlsx") Set ws2 = wb2.Sheets("ABC") Set rng2 = ws2.Range(Range("L3"), Range("L3").End(xlDown)) rng2.Select
另外请注意,Select函数只突出显示返回的单元格,但不返回值本身。 所以,而不是突出显示单元格,我只是返回它—>删除。select部分。
虽然注意到您不能在工作簿中同时显示两个单元格,但通过使用此代码,您始终可以通过rng1和rng2variables访问这两个单元格
如果您发现我的答案最有帮助,请用绿色的勾号奖励我;)
您不能同时从2个工作簿中Select
2个Range
,您可以一次完成一个(不确定为什么您需要Select
Range
)。
您需要完全限定您的所有Worksheets
和Range
。
Set ws1 = Sheets("macro")
– 尝试在ActiveWorkbook
查找名为“macros”的工作表,而不是wb1
。
Set rng1 = Range(Range("C3"), Range("C3").End(xlDown)).Select
,将引发运行时错误。 Set
Range. Also, this will set the range for
时无法Select
Range. Also, this will set the range for
Range. Also, this will set the range for
ActiveSheet Range. Also, this will set the range for
and not
ws1`。
您需要先Set
您的Range
: Set rng1 = ws1.Range(ws1.Range("C3"), ws1.Range("C3").End(xlDown))
,并且仅在(如果真的需要)之后select它: rng1.Select
。
码
Sub CommandButton1_Click() Application.ScreenUpdating = False Dim wb1 As Workbook, wb2 As Workbook Dim ws1 As Worksheet, ws2 As Worksheet Dim rng1 As Range, rng2 As Range Dim UnRng As Range Set wb1 = Workbooks("TEMPLATE.xls") Set ws1 = wb1.Sheets("macro") Set rng1 = ws1.Range(ws1.Range("C3"), ws1.Range("C3").End(xlDown)) rng1.Select Set wb2 = Workbooks("booking.xlsx") Set ws2 = wb2.Sheets("ABC") Set rng2 = ws2.Range(ws2.Range("L3"), ws2.Range("L3").End(xlDown)) rng2.Select End Sub