简体   繁体   English

运行时错误'424'-Excel中的VBA

[英]Run-Time Error '424' - VBA in Excel

I'm very very new to the VBA world. 我对VBA领域非常陌生。

I want to select 2 ranges from 2 worksheets in different workbooks. 我想从不同工作簿中的2个工作表中选择2个范围。

But I got that message in my script. 但是我在脚本中收到了该消息。 please help 请帮忙

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

I saw multiple problems in your code. 我在您的代码中看到了多个问题。 First of all, when you select a worksheet through the Sheets() property, it will look among the sheets of the currently activated (aka ActiveWorkbook ) workbook by default. 首先,当您通过Sheets()属性选择工作表时,默认情况下它将在当前激活的 (即ActiveWorkbook )工作簿的工作表中查找。 Same goes for selecting a Range of cells (Range() function), looks in the active worksheet. 在活动工作表中查找,选择单元格范围(Range()函数)也是如此。 So you have to be more specific in this case: 因此,在这种情况下,您必须更加具体:

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

And also notice that the Select function only highlights the returned cell, but it does not return the value itself. 还要注意,Select函数仅突出显示返回的单元格,但不返回值本身。 So instead of highlighting the cells, I just return it ---> removed the .Select parts. 因此,与其突出显示单元格,不如将其返回--->删除了.Select部分。

Although notice that you can't have two simultaneously highlighted cells across workbooks, but by using this code you can always access the two cells through the rng1 and rng2 variables 尽管注意在工作簿中不能同时具有两个突出显示的单元格,但是通过使用此代码,您始终可以通过rng1和rng2变量访问两个单元格

If you found my answer the most helpful, please reward me with a green tick ;) 如果您发现我的答案最有帮助,请用绿色勾号奖励我;)

Tried and tested throughout. 在整个过程中进行了尝试和测试。

I tried to replicate your error by creating books and sheets as alluded to in your code. 我试图通过创建代码中提到的书本和表格来复制您的错误。 Added a CommandButton1 to the macro sheet and inserted your code into its Click event handler. 在宏表中添加了CommandButton1,并将代码插入到其Click事件处理程序中。

Attempting to step through the code did select the range on macro sheet but threw Error 424 (Object required) and code execution terminated without highlighting anything in the IDE. 尝试单步执行代码的确在宏表上选择了范围,但抛出错误424(需要对象),并且代码执行终止了,而没有在IDE中突出显示任何内容。

Changing: 变更:

Set rng1 = Range(Range("C3"), Range("C3").End(xlDown)).Select

to

Set rng1 = Range(Range("C3"), Range("C3").End(xlDown))
rng1.Select

got rid of the error but next step through gave an Error 9 (Subscript out of range) on Set ws2 = Sheets("ABC") . 摆脱了错误,但下一步给出了Set ws2 = Sheets("ABC")上的错误9(下标超出范围Set ws2 = Sheets("ABC") This is because TEMPLATE.xls was still the active workbook. 这是因为TEMPLATE.xls仍然是活动工作簿。

Changing Set ws2 = Sheets("ABC") to Set ws2 = wb2.Sheets("ABC") cleared this error but then got Error 424 again due to .Select on the Set Rng2 line. Set ws2 = Sheets("ABC")更改为Set ws2 = wb2.Sheets("ABC")清除此错误,但由于Set Rng2行上的.Select ,再次出现错误424。 Also, cells in column L on macro sheet were selected. 此外,选择宏工作表上L列中的单元格。

Changing: 变更:

Set rng2 = Range(Range("L3"), Range("L3").End(xlDown)).Select

to: 至:

Set rng2 = Range(Range("L3"), Range("L3").End(xlDown))
rng2.Select

got rid of that error but cells in macros sheet were still selected. 摆脱了该错误,但仍选择了宏表中的单元格。

To fix that I changed: 要解决我更改的问题:

Set rng2 = Range(Range("L3"), Range("L3").End(xlDown))
rng2.Select

to: 至:

With ws2
    Set rng2 = Range(.Range("L3"), .Range("L3").End(xlDown))
End With
rng2.Select

I thought that had solved it but then got an `Error 1004 (Select method of Range class failed) which I could only overcome by activating bookings.xlsx! 我以为已经解决了,但是出现了错误1004(Range类的Select方法失败),我只能通过激活bookings.xlsx来克服!

You need to explicitly declare your Sheets and Ranges: 您需要明确声明表格和范围:

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

You can't Select 2 Range s from 2 workbooks at the same time, you can do it one at a time (not sure why you need to Select the Range at all). 您不能同时从2个工作簿中Select 2个Range ,一次只能选择一个(不知道为什么需要完全Select Range )。

You need to fully qualify all your Worksheets and Range s. 您需要完全限定所有WorksheetsRange

Set ws1 = Sheets("macro") - tries to look for a sheet named "macro" in ActiveWorkbook , and not wb1 . Set ws1 = Sheets("macro") -尝试在ActiveWorkbook查找名为“ macro”的工作表,而不是wb1

Set rng1 = Range(Range("C3"), Range("C3").End(xlDown)).Select , will raise a run-time error. Set rng1 = Range(Range("C3"), Range("C3").End(xlDown)).Select ,将引发运行时错误。 You can't Select when Set ting the Range. Also, this will set the range for 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 and not ws1`. Range. Also, this will set the range for ActiveSheet Range. Also, this will set the range for and not ws1` Range. Also, this will set the range for

You need to first Set your Range : Set rng1 = ws1.Range(ws1.Range("C3"), ws1.Range("C3").End(xlDown)) , and only after (if really needed) to Select it: rng1.Select . 您需要首先Set RangeSet rng1 = ws1.Range(ws1.Range("C3"), ws1.Range("C3").End(xlDown)) ,只有在(如果确实需要)选择它之后: rng1.Select 。选择。

Code

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM