Unb*_*ain 1 excel vba excel-vba
我有一段代码在当前工作表中搜索特定单元格值的范围,然后对该列执行操作.宏由同一页面上的表单控件按钮启动.我需要将表单控件按钮移动到另一个工作表,但我不知道如何将我的代码更改为仅在sheet1中搜索而不是在按钮所在的工作表中搜索.
下面是我在使用同一工作表上的按钮作为数据时的代码.我只需要它来查看sheet1而不是当前工作表.
Dim R1 As Range
Set R1 = Range(Range("A2:AX2").Find("BMA Authorization ID"), Range("A2:AX2").Find("BMA Authorization ID").End(xlDown))
R1.Select
R1.Copy
R1.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Run Code Online (Sandbox Code Playgroud)
小智 5
使用With ... End With语句设置范围/单元格父工作表.
with worksheets("Sheet1") '<~~change to suit
with .Range(.Range("A2:AX2").Find("BMA Authorization ID"), _
.Range("A2:AX2").Find("BMA Authorization ID").End(xlDown))
.value = .value 'same as .Copy .PasteSpecial xlPasteValues
end with
end with
Run Code Online (Sandbox Code Playgroud)
请注意,所有这些Range(...)都是现在.Range(...).前缀句点(aka .或句号)使所有范围的父级在With .. End With中引用的工作表.
正如findwindow所说; 你需要对表格进行限定.Normaly我会在评论中留下这个,但是这条线太长,不能使用带有块.
Dim R1 As Range
Dim ws As Worksheet
Set ws = Sheets("Sheet1")'Change this name to the sheet desired.
'Use a with block.
'When using a with block .Range = ws.Range
With ws
Set R1 = .Range(.Range("A2:AX2").Find("BMA Authorization ID"), .Range("A2:AX2").Find("BMA Authorization ID").End(xlDown))
R1.Value = R1.Value
End With
Run Code Online (Sandbox Code Playgroud)