将偏移量添加到最后一行

时间:2013-10-19 07:07:23

标签: excel excel-vba vba

我只想在offset (0,6)添加lastRow。有什么帮助吗?

lastRow = oSht.Range(vari1 & Rows.Count).End(xlUp).Row

2 个答案:

答案 0 :(得分:1)

在你澄清之后,我可以建议你可以通过范围扩展来做到这一点。您可以使用Offset而不是Resize

正如您所说,这对您的S column

有效
oSht.Range(vari2, vari1 & lastRow).Select

在我们添加调整大小后,您将获得新范围:

oSht.Range(vari2, vari1 & lastRow).Resize(,6).Select

答案 1 :(得分:0)

我认为这就是你想要的?

Option Explicit

Sub Sample()
    Dim oSht As Worksheet
    Dim lastRow As Long
    Dim Rng As Range
    Dim vari1 As String

    '~~> Change this to the relevant column letter
    vari1 = "S"

    '~~> Change this to the relevant sheet
    Set oSht = ThisWorkbook.Sheets("Sheet1")

    With oSht
        lastRow = .Range(vari1 & .Rows.Count).End(xlUp).Row

        Set Rng = .Range(vari1 & 2 & ":" & .Range(vari1 & lastRow).Offset(, 6).Address)

        '~~> S2:X32 in your case if lastrow is 32
        Debug.Print Rng.Address
    End With
End Sub