vba 将单元格内容从一列复制到同一工作表中的另一列---宏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6269903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:24:08  来源:igfitidea点击:

Copy cell contents from one column to another column in the same worksheet---Macro?

excelvbaexcel-vba

提问by Luke

I have account numbers in column E, and need those to replicate into column H for any cell that contains a value.

我在 E 列中有帐号,并且需要将这些帐号复制到包含值的任何单元格的 H 列中。

End result would be E1 = H1 and so on till all column E cells had been copied into coulmn H.

最终结果将是 E1 = H1 依此类推,直到所有列 E 单元格都已复制到 coulmn H 中。

Thanks in advance!

提前致谢!

采纳答案by mechanical_meat

Option Explicit

Public Sub x()

    Call copy_acct_nums("E", "H")

End Sub

Private Sub copy_acct_nums(copy_from_col, copy_to_col)

    Application.ScreenUpdating = False
    Dim i As Long

    With ActiveWorkbook.ActiveSheet

        For i = 1 To .UsedRange.Rows.Count Step 1
            .Cells(i, copy_to_col).Value = .Cells(i, copy_from_col).Value
        Next i

    End With

    Application.ScreenUpdating = True

End Sub

If you have multiple sheets to which you need to apply this code you could add a sheet argument to the copy subroutine. I will leave that to you :D

如果您有多个工作表需要应用此代码,则可以向复制子例程添加工作表参数。我会把它留给你:D

回答by alykat

Another option:

另外一个选项:

Sub copy()

  numrows = Cells.SpecialCells(xlLastCell).Row
  Range("E1:E" & numrows & "").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("H1")

End Sub

The columns could have also been passed in as variables if needed.

如果需要,列也可以作为变量传入。

Sub runCopy()

  Call copy("E", "H")

End Sub

Sub copy(col1, col2)

  numrows = Cells.SpecialCells(xlLastCell).Row
  Range("" & col1 & "1:" & col1 & "" & numrows & "").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("" & col2 & "1")

End Sub