繁体   English   中英

使用 Excel VBA 复制列中的范围

[英]Copy a range in column using Excel VBA

我正在尝试添加一个按钮,该按钮在带有值的最后一列之后添加一个新列。 这有效。

现在我想将值复制到新列。 值应从第 32 行的最后一列复制到 A 列中具有值的最后一列。现在我有一个用于复制整列的代码。 我如何专注于特定范围?

Sub AddMeeting()
Dim ws As Worksheet
Dim lastcol As Long
 Set ws = ActiveSheet

lastcol = ws.Cells(32, ws.Columns.Count).End(xlToLeft).Column
Columns(lastcol).Copy Destination:=Columns(lastcol + 1)
Range ((Cells.Columns.Count.End(xlLeft)) & Range(32)), (lastcol + 1) & Cells.Rows.Count.End(xlUp)



 Application.CutCopyMode = False
End Sub

值应从第 32 行的最后一列复制到 A 列中有值的最后一列

这是你正在尝试的吗?

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, lCol As Long
    Dim LastColumn As String
    Dim rngToCopy As Range

    '~~> Set this to the relevant worksheet
    Set ws = Sheet1

    With ws
        '~~> Find last row in Col A
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
        '~~> Find last column in row 32
        lCol = .Cells(32, .Columns.Count).End(xlToLeft).Column
        '~~> Get Column Name from column number
        ' https://stackoverflow.com/questions/10106465/excel-column-number-from-column-name
        LastColumn = Split(Cells(, lCol).Address, "$")(1)

        Set rngToCopy = .Range("A32:" & LastColumn & lRow)

        Debug.Print rngToCopy.Address

        With rngToCopy
            '
            '   Do what you want here
            '
        End With
    End With
End Sub

暂无
暂无

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

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