简体   繁体   English

引用其他工作表中的单元格。 动态/可变

[英]referencing a cell in other worksheet. DYNAMIC/VARIABLE

i have got a question about referencing a cell in other worksheet. 我有一个关于在其他工作表中引用单元格的问题。 I have got this code in VBA: 我在VBA中有以下代码:

If Application.CountIf(.Rows(Lrow), '8000')= 0 And Application.CountIf(.Rows(Lrow), '9000')) = 0 Then .Rows(Lrow).Delete 如果Application.CountIf(.Rows(Lrow),'8000')= 0并且Application.CountIf(.Rows(Lrow),'9000'))= 0然后.Rows(Lrow).Delete

Which is capable for me to delete any row WITHOUT words 8000 and 9000. However, since there would be future update, how can i adjust the code so that the value for excel to execute can be "DYNAMIC" (ie not hard-cored) Say, if i enter a number at cell (17,2) in SHEET 1, what can i make excel to look at the cell address instead of the "absolute value" from SHEET 2 by VBA? 这使我能够删除不带单词8000和9000的任何行。但是,由于将来会有更新,因此我该如何调整代码,以便excel执行的值可以是“动态的”(即,不是硬核的)假设,如果我在SHEET 1的单元格(17,2)中输入数字,我该如何擅长查看单元格地址,而不是VBA从SHEET 2中获得的“绝对值”?

Thanks. 谢谢。

You could add these couple of lines above the code you already have. 您可以在已有的代码上方添加以下几行。 This is assuming that you want to change either number by entering them into cell B17 or B18. 假设您要通过将其输入到单元格B17或B18中来更改任一数字。

Dim lowerBound as Long, upperBound as Long
lowerBound = Sheet1.Range("B17").Value
upperBound = Sheet1.Range("B18").Value

Then replace '8000' in with lowerBound and '9000' with upperBound. 然后将“ 8000”替换为lowerBound,将“ 9000”替换为upperBound。

You can add a column X to your source sheet, headed "Condx for DELETE" and fill this column with a formula representing the DELETE condition as a Boolean value - like in your case: =NOT(OR(A2=8000,A2=9000)) (asuming values 8000, 9000, etc. appear in column A) 您可以将X列添加到源工作表中,标题为“ Condx for DELETE”,并用表示DELETE条件的布尔值公式填充此列-如您的情况:= NOT(OR(A(A2 = 8000,A2 = 9000 ))(假设值8000、9000等出现在A列中)

Then you cycle thru the source table and delete all records for which column X is TRUE. 然后,您遍历源表并删除X列为TRUE的所有记录。 Asuming that 假设

  • your source has been defined within VBA as a range object (MyRange) 您的来源已在VBA中定义为范围对象(MyRange)
  • the first row is the header row 第一行是标题行
  • no empty cells in first column between header and end of list 标题和列表结尾之间的第一列中没有空单元格
  • the condition is found in 3rd column 条件在第三列中找到

a purger could look like this 吹扫者可能看起来像这样

Sub MySub()
Dim MyRange As Range, Idx As Integer, MyCondx As Integer

    Set MyRange = ActiveSheet.[A1]      ' represent source data as a range
    Idx = 2                             ' asuming one header row
    MyCondx = 3                         ' asuming condition is in 3rd column

    Do While MyRange(Idx, 1) <> ""
        If MyRange(Idx, MyCondx) Then
            MyRange(Idx, 3).EntireRow.Delete
        Else
            Idx = Idx + 1
        End If
    Loop
End Sub

This way you have full transparency, no hardcoding and you are very flexible to not only change a set of 2 values but specify whatever condition serves the business. 这样,您就具有完全的透明度,无需进行硬编码,并且非常灵活,不仅可以更改一组2个值,而且可以指定为业务服务的条件。

Hope that helps - good luck 希望有帮助-祝你好运

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

相关问题 通过将另一个单元格中的字符串引用为列来在另一个工作表中引用动态单元格 - Dynamic cell reference in another worksheet by referencing string in another cell as column VBA自动筛选器,其标准作为另一个工作表中的单元格。 收到“运行时错误9” - VBA Autofilter with criteria as a cell in a differant worksheet. Getting “runtime error 9” 在VBA函数中引用动态单元格以创建具有相同名称的工作表索引 - Referencing a dynamic cell in VBA function for creating an Index of Worksheet with that same name 引用另一个工作表中的单元格 - Referencing cell in another worksheet 动态引用工作表 - Dynamic referencing to worksheet 引用单元格到不同工作表中的范围 - referencing a cell to a range in different worksheet 如何在Excel工作表中获取Excel范围左上和右下单元格。 - How to get excel range topleft and bottomright cell in en Excel worksheet. 动态范围 - 通过引用其他单元格值来修改范围 - Dynamic Range - Modifiying Range by referencing other cell values 根据其他工作表中的键列复制数据。 (尝试执行此操作,因为Vlookup在VBA中不起作用) - Copying data against a key column from other worksheet. (Trying to do as vlookup is not working in VBA) 在VBA函数中引用另一个工作表中的单元格 - Referencing a cell from another worksheet in a function for VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM