简体   繁体   English

EXCEL VBA-当工作表名称为变量时声明范围

[英]EXCEL VBA - declaring range when the sheet name is a variable

I use this line of code: 我使用以下代码行:

    ActiveWorkbook.Worksheets(sName).ListObjects(sName).Sort.SortFields.Add Key:=
    Range ("sName.Name[[#All],[Keyword]]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

Now, this line is a part of a longer function that supposed to do the same thing for a wider list of sheets. 现在,此行是较长功能的一部分,该功能应该对更广泛的工作表执行相同的操作。 Therefore, I need to change the range selection: 因此,我需要更改范围选择:

  Range ("sName.Name[[#All],[Keyword]]")

To be based on a variable, I just can't find the right solution on how to do it. 要基于变量,我只是找不到正确的解决方案。 ** I tried a few options, improvised, didn't work. **我尝试了几种选择,即兴演奏,没有用。

Thanks in advance. 提前致谢。

That is the full code btw: 那是完整的代码:

Sub Filter()
'Application.ScreenUpdating = False
    Call Filtering("US")                  'To prepare Data for All Keywords
    MsgBox "Updated"
'Application.ScreenUpdating = True
End Sub

Function Filtering(sName As String)
'
' Filtering Macro
'

'
    Sheets(sName).Select
    ActiveWorkbook.Worksheets(sName).ListObjects(sName).Sort.SortFields.Clear
    ActiveWorkbook.Worksheets(sName).ListObjects(sName).Sort.SortFields.Add Key:=
        Range ("sName.Name[[#All],[Keyword]]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

    With ActiveWorkbook.Worksheets(sName).ListObjects(sName).Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveWorkbook.Worksheets(sName).ListObjects(sName).Sort.SortFields.Clear
    ActiveWorkbook.Worksheets(sName).ListObjects(sName).Sort.SortFields.Add Key:= _
        Range("sName.Name[[#All],[Position]]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal _

    With ActiveWorkbook.Worksheets(sName).ListObjects(sName).Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.ListObjects(sName).Range.AutoFilter Field:=2, Criteria1:="<=20" _
        , Operator:=xlAnd
End Function

To answer your question, this task is fairly simple. 要回答您的问题,此任务非常简单。 First, I would like to point out (largely for future coders with similar issues) that the method you are trying to call needs a string argument and you are close. 首先,我想指出(主要是针对以后出现类似问题的编码人员),您尝试调用的方法需要一个字符串参数,并且您已经关闭了。

' String argument, but "sName" is never interpreted. It is taken literally.
Range ("sName[[#All],[Keyword]]")

' Returns the string representation of sName and concatenates it into
' the rest of the string argument. If sName was Foo', this would read as:
' Range ("Foo[[#All],[Keyword]]")
Range (sName & "[[#All],[Keyword]]")

Now, to make a larger observation on your code I would strongly suggest you find resources on improving the code overall. 现在,为了对您的代码有更深入的了解,我强烈建议您找到有关整体改进代码的资源。 References to 'ActiveWorkbook' can be risky, and you are bound to get run-time errors. 引用“ ActiveWorkbook”可能会带来风险,并且您一定会遇到运行时错误。 'Select' statements create the same risk. “选择”语句会产生相同的风险。 I know you are likely learning through the macro recorder (as we all did), but make sure you learn how to avoid Select and Activate before you actually release that project. 我知道您可能正在通过宏记录器学习(就像我们所做的一样),但是请确保您在实际发布该项目之前学习如何避免选择和激活。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM