用变量替换Excel公式参数(由用户提供)

时间:2017-04-06 06:07:37

标签: excel vba excel-vba

概念: 通过基于宏的关闭参数删除整行,这些参数由用户表示为excel公式。这里的想法是用户可以使用他们已经熟悉的布尔公式来评估范围内的值(阅读"过程"以下进一步说明)。

过程: 用户单击显示表单的按钮。此表单包含两个输入字段(或参数); "柱"和"公式"。 "专栏"是宏将循环的范围(让我们说$A:$A)。 "公式"是一个基于Excel的公式,在用户参数字段中表示为=OR(A1="X",A1="Y")。但是,我已指示用户将A1的任何实例替换为rng。我已经请求用户执行此操作,因为这里的想法是我用VBA中的更改变量替换rng,该变量循环遍历"列"中指定的所有单元格。参数。

问题: 我不知道用VBA中的范围变量替换Excel公式中的rng表示的任何方法。

更新4-7-17 谢谢大家的回复,但我很确定我的问题是在翻译中丢失了。我知道这是我的错,因为我没有提供任何分析代码。不幸的是,其中存在问题。我不知道该写些什么。我会尽我所能写一些代码(我知道这是错的),这将有希望传达我想要完成的事情。

示例代码4-7-17

Sub SampleCode()
Dim wRng As Range
    Set wRng = Range("A1:A26") 'Let's assume that the values in this range are the
                                ' letters of the alphabet
Dim Counter As Integer


'Cell "B2" will contain a formula that the user has entered
'which is:  =OR(rng="X",rng="M")
'Obviously the formula returns an error in excel (#NAME? to be
'exact) but that's understood.
Dim wFormula As String
    wFormula = Range("B2").Formula

Dim rng As Range 'This variable "rng" is what is represented in the
'formula that was written in Range("B2")

'*** This is where I get stuck.  I know I'm missing code here to
'be able to proceed with my routine below.
'code
'code
'code

Counter = wRng.Rows.Count
For i = 1 To wRng.Rows.Count
    Set rng = Cells(Counter, 1)
    If wFormula = True Then
        rng.EntireRow.Delete
    End If
    Counter = Counter - 1
Next i

'The ending result should be that row 24 was deleted because it contained
'the letter "X" and row 13 was deleted because it contained the letter "M"
'
'The objective of this code is to use any Excel formula which evaluates out
'to a True or False value.

End Sub

2 个答案:

答案 0 :(得分:0)

嘿Jon首先你需要声明一个相关数据类型的变量,然后传递Range&的值。最后使用你想要的地方,比如,

Dim Src As Variant Src =表格(“Sheet3”)。范围(“A2:A9”)。值

答案 1 :(得分:0)

嘿John这段代码将帮助您获得解决方案,

Public Sub ProcessData() Const TEST_COLUMN As String =" A"
昏暗的Lastrow长长的昏暗我长 Application.ScreenUpdating = False

使用ActiveSheet
Lastrow = .Cells(.Rows.Count,TEST_COLUMN).End(xlUp).Row

对于i = Lastrow To 1 Step -1

If Cells(i,TEST_COLUMN).Value2 Like" AU "或_ Cells(i,TEST_COLUMN).Value2 Like" AZ "然后
.Rows(ⅰ).Delete
结束如果
接下来我
以。结束 Application.ScreenUpdating = True 结束子