使用带有Autofilter criteria1 vba的变量

时间:2016-03-24 02:18:13

标签: excel vba excel-2013 autofilter

我一直面临一个非常烦人的问题,在Excel 2013 VBA中使用Autofilter criteria1参数时似乎无法解决这个问题。

我发现了这个link,但它对我不起作用

基本上我只想将变量传递给criteria1参数,如下所示。

import random

print("hello, what is your name?")
GG = input()

print("well, " + GG + ", I am thinking of a number between 0 and 20")
number = random.randint(0,20)

try:
    for taken in range(1,7):
        print("Take a guess.")   
        guess = int(input())

        if guess < number:
            print("your guess is too low.")
        elif guess > number:
            print("your guess is too high.")
        else:
            break

except ValueError:
    print("please enter a valid number")

    if guess == number:
       print("good job, "+ GG + " you guessed my number in " + str(taken) + " guesses")
    else:
       print("nope,the number i was thinking of was " + str(number))

没有为变量&#34; Site&#34;做出声明。结果是过滤器将使用标准&#34; Equals&#34;过滤列。符号&#34; *&#34;。

我注意到如果我直接为变量分配一个字符串并将其放在criteria参数中它工作正常,所以我尝试使用CStr将Site转换为字符串变量,但仍无法正常工作。

Results from the Macro

*更新:我发现下面的代码可以过滤我需要的内容:

For Each Site In Worksheets("Results").Range("A2:A200")
    With Worksheets("2G Cell Data")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range("A1:O" & lastRow).AutoFilter Field:=Column2GSiteName, _
           Criteria1:="=*" & Site & "*", Operator:=xlAnd         
    End With
Next Site

0 个答案:

没有答案