我对VBA世界非常陌生。
我想从不同工作簿中的2个工作表中选择2个范围。
但我在剧本中收到了这条消息。请帮忙
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
答案 0 :(得分:1)
我在您的代码中看到了多个问题。首先,当您通过Sheets()属性选择工作表时,默认情况下它将在当前激活的(a.k.a。 ActiveWorkbook )工作簿的工作表中查找。选择一个单元格范围(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 parts。
虽然注意到您不能在工作簿中同时突出显示两个单元格,但通过使用此代码,您始终可以通过rng1和rng2变量访问这两个单元格
如果您发现我的答案最有帮助,请以绿色勾号奖励我;)
答案 1 :(得分:0)
全程尝试和测试。
我尝试通过创建代码中提到的书籍和工作表来复制错误。在宏表中添加了一个CommandButton1,并将您的代码插入到其Click事件处理程序中。
尝试单步执行代码确实在宏表上选择了范围,但抛出了错误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(下标超出范围)。这是因为TEMPLATE.xls仍然是活动工作簿。
将Set ws2 = Sheets("ABC")
更改为Set ws2 = wb2.Sheets("ABC")
会清除此错误,但由于.Select
行Set Rng2
而再次出现错误424。此外,选择宏表上L列中的单元格。
更改:
Set rng2 = Range(Range("L3"), Range("L3").End(xlDown)).Select
为:
Set rng2 = Range(Range("L3"), Range("L3").End(xlDown))
rng2.Select
摆脱了这个错误,但仍然选择了宏表中的单元格。
要解决我改变的问题:
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(Range类的选择方法失败),我只能通过激活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
答案 2 :(得分:0)
您可以同时从2个工作簿中make
2 Select
,您可以一次只执行一次(不确定为什么需要Range
总共Select
。
您需要完全限定所有Range
和Worksheets
。
Range
- 尝试查找名为" macro"在Set ws1 = Sheets("macro")
,而不是ActiveWorkbook
。
wb1
会引发运行时错误。 Set rng1 = Range(Range("C3"), Range("C3").End(xlDown)).Select
Select
ActiveSheet Set
ws1`时,您无法Range. Also, this will set the range for
。
您需要先and not
Set
:Range
,然后才能(如果确实需要)选择它:Set rng1 = ws1.Range(ws1.Range("C3"), ws1.Range("C3").End(xlDown))
。
<强>代码强>
rng1.Select