带有表格(“”)。范围(“B:B”); search时可变范围

这里的东西,我有一个代码,使用固定工作表中的固定范围来search一个值。 我现在需要使表单variables。 到目前为止,我已经尝试了几件没有运气的东西。 我需要search的工作表的名称由另一个工作表中的单元格确定。 用“Sheets(”Sheets(“asd”)。range(“A1”)“)。Range(”B:B“)不起作用。

我的代码:

FindString = W If Trim(FindString) <> "" Then With Sheets(**"CARS"**).Range("B:B") Set Rng = Cells.Find(What:=FindString, _ After:=.Cells(.Cells.Count), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) If Not Rng Is Nothing Then Application.Goto Rng, True Else MsgBox "Nothing found" End If End With End If 

让我知道是否需要更多的信息。 谢谢!

(“”asd“)。range(”A1“)”)。Range(“B:B”)

通过:

 dim VAR_Sheet as string Var_Sheet=Sheets("asd").range("A1").value With Sheets(Var_Sheet).Range("B:B") ...code... end with 

作品也是这样(相同的结果):

 With Sheets(Sheets("asd").range("A1").value).Range("B:B") 'removed quotes that made error 

尝试省略一些双引号:使用表格(表格(“asd”)。range(“A1”))。Range(“B:B”)

说明

当你这样做,你会得到一个编译错误:预期列表分隔符或)

 With Sheets("Sheets("asd").range("A1")").Range("B:B") 

这是因为双引号封装了一个string文字,所以在这种情况下,string文字是"Sheets(" ,它在asd处引发错误(以及后续的错误)。

解决scheme是简单地引用Sheets("asd")对象,没有必要在引用中限定该对象:)

注意 Brad在代码中指出了另一个可能的错误,请参阅下面的答案。

它看起来像,而你键入With你实际上没有使用它。

 With Sheets("CARS").Range("B:B") Set Rng = Cells.Find(What:=FindString, _ <~~ Cells references the active sheet After:=.Cells(.Cells.Count), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) If Not Rng Is Nothing Then Application.Goto Rng, True Else MsgBox "Nothing found" End If End With 

你需要用一个圆点开始声明,表示你正在继续build立int eh的参考

 With Sheets("CARS").Range("B:B") Set Rng = .Find(What:=FindString, _ <~~ .Cells After:=.Cells(.Cells.Count), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) If Not Rng Is Nothing Then Application.Goto Rng, True Else MsgBox "Nothing found" End If End With 

我假设double *是强调( Sheets(**"CARS"**) – > Sheets("CARS")