Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在excelvba中选择全量程_Vba_Excel - Fatal编程技术网

如何在excelvba中选择全量程

如何在excelvba中选择全量程,vba,excel,Vba,Excel,当我调试excel vba程序时,我知道我的数据范围未完全选定 下图显示了我的数据模型和我的问题。 我用这个代码来选择整个范围。但这并不正常 Dim rngTemp As Range Set rngTemp = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious) With rngTemp 请提供上图所示的选择整个范围的代码,以帮助我。在您的代码中,您通过xlByRows进行搜索。因此,您将获得最后一个单元格的

当我调试excel vba程序时,我知道我的数据范围未完全选定

下图显示了我的数据模型和我的问题。

我用这个代码来选择整个范围。但这并不正常

Dim rngTemp As Range
Set rngTemp = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
With rngTemp

请提供上图所示的选择整个范围的代码,以帮助我。

在您的代码中,您通过
xlByRows
进行搜索。因此,您将获得最后一个单元格的地址,该单元格包含的数据是
G7

除了我的评论,这是你正在尝试的吗

Sub Sample()
    Dim lastrow As Long, lastcol As Long
    Dim rng As Range

    With Sheets("Sheet1") '<~~ Change this to the relevant sheet
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lastrow = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row
            lastcol = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByColumns, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Column
        Else
            lastrow = 1: lastcol = 1
        End If

        Set rng = .Range("A1:" & _
                         Split(.Cells(, lastcol).Address, "$")(1) & _
                         lastrow)

        MsgBox rng.Address
    End With
End Sub
子样本()
调暗lastrow和lastcol一样长
变暗rng As范围

对于图纸(“Sheet1”)”请注意,以下方法在某些情况下不可靠。我将在这里留下这个答案作为一个坏例子。有关详细信息,请参见本节@SiddharthRout的解释

我将使用以下代码查找已使用的范围,而不是在单元格值中查找“*”:

Sub SelectRange()
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim aWB As Workbook
    Dim aWS As Worksheet

    Set aWB = ActiveWorkbook
    Set aWS = aWB.ActiveSheet '<-You can change sheet name like aWB.sheets("SheetName")

    With aWS.UsedRange
        LastRow = .Rows(.Rows.Count).Row
        LastColumn = .Columns(.Columns.Count).Column
    End With

    aWS.Range(Cells(1, 1), Cells(LastRow, LastColumn)).Select '<---Cells(1, 1) is the starting cell of range)

End Sub
子选择范围()
最后一排一样长
将最后一列变长
将aWB设置为工作簿
将aWS设置为工作表
设置aWB=ActiveWorkbook

设置aWS=aWB.ActiveSheet'Try。然后使用类似的方法找到最后一列,然后构建你的范围。你不能使用Set rngTemp=Cells.usedrange吗?@99moorem:usedrange是个坏主意,正如我在@99moorem中提到的那样,
usedrange
是个坏主意。你可能想看看我在问题下面贴的链接:我刚刚注意到的染料。谢谢你的提醒。我将从现在起停止使用此方法。非常感谢你。