简体   繁体   English

Excel marco on复制并粘贴到特定的空白列

[英]Excel marco on copy and paste to specific blank columns

Need help with a simple excel macro. 需要有关简单excel宏的帮助。 I have data in sheet 1 column X1 through X20. 我在工作表1列X1至X20中有数据。 I want to autmatically paste this information to column A and then when I update the numbers in Column XI want to paste this information to Column B and then to Column C... I only need to do this 12 times. 我想以自动方式将此信息粘贴到A列,然后在更新XI列中的数字时希望将此信息粘贴到B列然后粘贴到C列...我只需要这样做12次。 Here is the simple macro I tried. 这是我尝试过的简单宏。

Workbooks("copynpaste2.xlsm").Sheets("Sheet1").Range("X1:X20").copy _ 
destination:=Workbooks("copynpaste2.xlsm").Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)

What about this? 那这个呢?

Sub dothis()
    Dim c As Long
    For c = 1 To 12
        If Cells(1, c) = "" Then
            Range("X1:X20").Copy 
            Range(Cells(1, c), Cells(20, c)).Select
            Selection.PasteSpecial xlPasteValues
            selection.NumberFormat = "0.00"
            Exit Sub
        End If
    Next c
End Sub

Each time you run the macro it will check if cell 1 in columns A:L is blank and copy cells X1:X20 to the first empty column. 每次运行宏时,它将检查A:L列中的单元格1是否为空白,并将单元格X1:X20复制到第一空列。

Use the Worksheet_Change event. 使用Worksheet_Change事件。 With this event, each time you change column X, the code will fire and copy to columns A... B... C... And you will never have to manually run any code. 发生此事件时,每次更改X列时,代码都会触发并复制到A ... B ... C ...列,您将不必手动运行任何代码。

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Me.Range("X1:X20")) Is Nothing Then 'only do this when making changes to X1:X20

    Application.EnableEvents = False

    If IsEmpty(Me.Range("A1")) Then

        Me.Range("A1:A20").Value = Me.Range("X1:X20").Value

    ElseIf IsEmpty(Me.Range("B1")) Then

        Me.Range("B1:B20").Value = Me.Range("X1:X20").Value

    Else

        Me.Range("A1:A20").End(xlToRight).Offset(, 1).Value = Me.Range("X1:X20").Value

    End If

    Application.EnableEvents = True

End If

Place this code inside the Worksheet Module on the sheet where the data is located. 将此代码放在数据所在的工作表模块上的工作表模块中

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

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