Excel 在第一行和最后一行上选择G列

Excel 在第一行和最后一行上选择G列,excel,vba,Excel,Vba,我的一行代码有问题。我想根据FirstRow和LastRow在数据表中选择一列,我可以找到FirstRow和LastRow,我使用以下代码: Firstrow = Cells(Rows.Count, 1).End(xlUp).End(xlUp).Row LastRow = Cells(Rows.Count, 1).End(xlUp).Row Rows(Firstrow & ":" & LastRow).Select 当我运行这段代码时,它选择第一行和最后一行,但选择所有列。我需

我的一行代码有问题。我想根据FirstRow和LastRow在数据表中选择一列,我可以找到FirstRow和LastRow,我使用以下代码:

Firstrow = Cells(Rows.Count, 1).End(xlUp).End(xlUp).Row
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Rows(Firstrow & ":" & LastRow).Select
当我运行这段代码时,它选择第一行和最后一行,但选择所有列。我需要使用此选项仅选择G列。我已尝试在代码中添加此选项:

Rows(Firstrow & "G:G" & LastRow).Select
但这给了我一个错误:类型不匹配

我还尝试对FirstRow和LastRow语句进行调整:

Firstrow = Cells(Rows.Count, 7).End(xlUp).End(xlUp).Row
LastRow = Cells(Rows.Count, 7).End(xlUp).Row
但这也不行


有人有过这样的经历吗?我希望能够在选择功能中调整列,而不是第一行和最后一行,因为我以后需要选择不同的列。

使用
范围
,因为您不想选择整行:

Range("G" & Firstrow & ":G" & LastRow).Select
在求和公式中使用:

Range("G" & LastRow).Formula = "=SUM(G" & FirstRow & ",G" & LastRowSumC & ")"
你可以试试:

Sub test()

    Dim ColumnNo As Long, FirstRow As Long, LastRow As Long
    Dim ColumnLet As String

    'Create a with statement with the sheet1 you want
    With ThisWorkbook.Worksheets("Sheet1")

        'Import the columne Letter you want
        ColumnLet = "G"
        'Convert column letter to column number
        ColumnNo = Range(ColumnLet & 1).Column

        'Find FirstRow
        FirstRow = .Cells(.Rows.Count, ColumnNo).End(xlUp).End(xlUp).Row
        'Find LastRow
        LastRow = .Cells(.Rows.Count, ColumnNo).End(xlUp).Row

        'Select the range
        .Range(.Cells(FirstRow, ColumnNo), .Cells(LastRow, ColumnNo)).Select

    End With

End Sub

范围(“G”&Firstrow&“:G”&LastRow)。选择范围(单元格(Firstrow,“G”)、单元格(LastRow,“G”)。选择。但是为什么要使用
选择
?一般来说,你应该尽量避免。你能帮我做以下几件事吗。当我想用求和函数实现你的答案时,我得到了预期的错误:语句结束。它选择公式中的第一个G。Range(“G”&LastRow)。Formula=“=SUM(“G”&FirstRow&“:G”&LastRow)”感谢您的帮助,似乎无法在网上找到任何关于此的学习资料。你能帮我解决下一个问题吗:Selection.AutoFill Destination:=Range(“P”和FirstRow&“:P”和LastRow)。“范围类的自动填充方法失败