[英]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
假设
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.