简体   繁体   English

如何在单独的工作表中查找特定的单元格值?

[英]How to find a specific cell value in separate worksheet?

I have a piece of code that searches a range in the current worksheet for a specific cell value and then does an action on that column. 我有一段代码,在当前工作表的范围内搜索特定的单元格值,然后对该列进行操作。 The macro is launched by a form control button on the same page. 该宏由同一页面上的表单控制按钮启动。 I need to move the form control button to a different worksheet but I am not sure how to udpate my code to only search in sheet1 and not in the sheet where the button in. 我需要将表单控制按钮移动到另一个工作表,但是我不确定如何添加代码以仅在sheet1中而不是在按钮所在的工作表中进行搜索。

Below is my code that works when using the button on the same worksheet as the data. 下面是我的代码在使用与数据相同的工作表上的按钮时可以工作的代码。 I just need it to look at sheet1 instead of the current sheet. 我只需要它来查看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

Use a With ... End With statement to set the range/cell parent worksheet. 使用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

Note that all of the Range(...) are now .Range(...) . 请注意,所有的Range(...)现在都是.Range(...) The prefix period (aka . or full stop ) makes all of the ranges' parent the worksheet referenced within the With .. End With. 前缀句点(也称为.句号 )使所有范围的父级成为With .. End With中引用的工作表。

As findwindow suggested; 正如findwindow所建议的; you need to qualify the sheet. 您需要使工作表合格。 Normaly I would leave this in the comments but the line is too long not to use a with block. 通常,我会在注释中保留此内容,但该行太长,无法使用with块。

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

You can just use Sheets(1).Range... or Sheets("Sheet1").Range... 您可以只使用Sheets(1).Range...Sheets("Sheet1").Range...

Take a look here for more details on referencing sheets in VBA... Trying to reference another worksheet in active workbook 在此处查看有关在VBA 中引用工作表的更多详细信息... 尝试在活动工作簿中引用另一个工作表

EDIT : Please don't take this as an argument against the other answers here. 编辑 :请不要将此作为反对其他答案的论据。 They are excellent answers for the OP (part of why I waited until one was accepted). 它们是OP的绝佳答案(这是我等到被接受之前的部分原因)。 I just think this is worth mentioning as a consideration. 我只是认为这值得一提。

I have personally adapted, come to appreciate, and would recommend the convention of avoiding with statements whenever possible per the following sentiment... 我个人进行了改编,非常欣赏,并根据以下观点,建议尽量避免with声明的惯例...

Why doesn't C# have VB.NET's 'with' operator? 为什么C#没有VB.NET的“ with”运算符?

Many people, including the C# language designers, believe that 'with' often harms readability, and is more of a curse than a blessing. 许多人,包括C#语言设计师在内,都认为“带有”通常会损害可读性,并且更多的是诅咒而不是祝福。 It is clearer to declare a local variable with a meaningful name, and use that variable to perform multiple operations on a single object, than it is to have a block with a sort of implicit context. 声明具有有意义名称的局部变量,并使用该变量对单个对象执行多个操作要比拥有具有某种隐式上下文的块要清晰得多。

by @Jon Skeet 通过@Jon Skeet
https://stackoverflow.com/a/4174826/3546415 https://stackoverflow.com/a/4174826/3546415

The use of variable declaration would look something like this... 变量声明的使用看起来像这样...

Dim S1 As Worksheet
Set S1 = Sheets("Sheet1")
S1.Range... blah blah

Of course, there are still situations where using with does still make sense. 当然,在某些情况下,使用with仍然有意义。 Such as when a variable can't be defined or when setting a large number of properties, but I do agree that readability is generally enhanced when you don't have to worry about nesting and checking the top of your with structure to see what .Range is actually referring to. 例如,当一个变量不能被定义或设置大量的属性的时候,但我不同意,当你不担心嵌套和检查您的顶部标签的可读性一般增强with结构看什么.Range实际上是指。 For a simple program this is hardly an issue, but it's worth keeping in mind for something more complicated. 对于一个简单的程序来说,这几乎不是问题,但是对于更复杂的事情则值得牢记。

暂无
暂无

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

相关问题 当两个工作表中的单元格匹配且具有单独的特定值时,如何更改单元格值? - How to change a cell value when cells from two worksheet match and a separate specific value? Excel VBA在工作表上找到具有特定值(字符串)的单元格 - Excel VBA find a cell on worksheet with specific value (string) 如何在另一个工作表中找到匹配的数据并在 Excel 的特定单元格中返回布尔值? - How can I find matching data in another worksheet and return boolean value in a specific cell in Excel? 查找单元格,然后将以下单元格复制到单独的工作表中 - Find cells and copy the following cell to a separate worksheet 如何通过关联在另一个工作表的Excel单元格中显示特定值 - How to display a specific value in an excel cell from another worksheet by association 如何 select 工作表上的特定单元格? - How to select a specific cell on a worksheet? 如何使用Excel工作表函数查找具有最接近值的单元格 - How to find a cell with closest value using Excel worksheet function 如何将单元格值定义为工作表 - How to define a cell value as a worksheet 如何自动化电子表格以便更改一个单元格中的值允许整行显示在工作表的单独选项卡中? - How to automate a spreadsheet so that changing a value in one cell allows that entire row to appear in a separate tab of the worksheet? 如何在双击之前将单元格值复制到新工作表中的特定单元格? - How to copy cell value to a specific cell in a new worksheet before double click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM