Select 动态 Table 范围在 Excel 使用 VBA

Select Dynamic Table Range In Excel Using VBA

我正在尝试 select 动态 table - 我可以弄清楚脚本的 copy/pasting 部分,但我无法弄清楚最初如何 select 这个 table.

table 在行数和列数方面都是动态的。之所以如此,是因为这个单独的工作簿需要可供不同的业务部门使用,调用不同的 SQL 服务器 table。因此,用户将他们的输入放在 Sheet1 中,刷新连接,然后 table 在 Sheet2 中 returned。

以下是片段,包括有效部分和损坏部分:

    'Variable designations
    Dim rowcount As String
    Dim columncount As String
    Dim sheetref1 As String
    Dim sheetref2 As String
    Dim rangeselect1 As String
    Dim rangeselect2 As String

    rowcount = Cells(Rows.Count, 1).End(xlUp).Row
    columncount = Cells(1, Columns.Count).End(xlToLeft).Column
    sheetref1 = "Sheet1"
    sheetref2 = "Sheet2"
    rangeselect1 = "A2:A" & rowcount
    rangeselect2 = "A1:" & columncount & rowcount '<--BROKEN

    'Copy column with populated rows
    Sheets(sheetref1).Range(rangeselect1).Copy '<--WORKING

    'Copy table with populated rows and columns
    Sheets(sheetref2).Range(rangeselect2).Copy '<--BROKEN

所以这里 rangeselect2 = "A1:" & columncount & rowcount 我正在尝试 return 类似 A1:Z10 或 A1:F3000 的东西 - 动态范围(顺便说一句,A1 是静态的)。我的尝试是让 columncount 为 return "Z" 或 "F" 或任何最后一列字母,而 rowcount(希望)正确 returns 最后一行号。

希望这是有道理的。我很乐意回答任何进一步的问题,非常感谢任何 advice/help.

这将找到并 select 从 A1 到包含代码所在工作簿 Sheet1 上数据的最后一个单元格的所有单元格:

Sub SelectDynamicTable()

    Dim rFinalRange As Range
    Dim lLastRow As Long
    Dim lLastCol As Long

    With ThisWorkbook.Worksheets("Sheet1")
        lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
        lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row

        If lLastCol = 0 Then lLastCol = 1
        If lLastRow = 0 Then lLastRow = 1

        Set rFinalRange = .Range(.Cells(1, 1), .Cells(lLastRow, lLastCol))
    End With

    rFinalRange.Select

End Sub

使用单元格而不是范围 - 单元格可以接受行号和列号而不是字母指定。

在给定工作表引用和要查看的可选列时,此函数将return引用最后一个单元格。

Public Function LastCell(wrkSht As Worksheet, Optional Col As Long = 0) As Range

    Dim lLastCol As Long, lLastRow As Long

    On Error Resume Next

    With wrkSht
        If Col = 0 Then
            lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
            lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
        Else
            lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
            lLastRow = .Columns(Col).Find("*", , , , xlByColumns, xlPrevious).Row
        End If

        If lLastCol = 0 Then lLastCol = 1
        If lLastRow = 0 Then lLastRow = 1

        Set LastCell = wrkSht.Cells(lLastRow, lLastCol)
    End With
    On Error GoTo 0

End Function

此测试程序将显示活动工作簿第 2 列中最后一个单元格的地址:

Public Sub TestLastCell()

    MsgBox LastCell(ActiveWorkbook.Worksheets("Sheet2"), 2).Address

End Sub

如果您想查找第一个单元格,请在代码中使用 xlNext 而不是 xlPrevious

您的 rangeselect2 = "A1:" & columncount & rowcount 无效,因为 columncountrowcount 返回为指示行和列的数字。如果要将 columncountrowcount 转换为 A1 地址(例如第 2 列和第 5 行是 B5),则可以使用 Cells(rowcount, columncount).Address,其中 returns 该坐标的绝对参考。

rangeselect2 = "A1:" & Cells(rowcount, columncount).Address 应该适用于您在这里尝试做的事情。