复制列到下一列

huangapple go评论91阅读模式
英文:

Copying Column to next one

问题

我的目标是创建一个宏,将整个列 R 复制并粘贴到下一列 S。当我再次使用按钮时,列 R 将被复制,并应粘贴到列 T。

Sub CopyPaste()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet

Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet1")

copySheet.Range("R:R").Copy
pasteSheet.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).PasteSpecial

Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

但是当我使用宏时,列会被复制,但不会移动到下一列,永远不会粘贴到列 R。

英文:

My Goal is to get a Macro that copies the whole Column R and pastes it into the next Column S. When i use the button again, the column R gets copied and it should be pasted into the column T.


Sub CopyPaste()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet


Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet1")

copySheet.Range("R:R").Copy
pasteSheet.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).PasteSpecial


Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

But when I use the macro, the column gets copied, but it doesn't move to the next column and never gets pasted into the column R.

答案1

得分: 1

为避免空单元格问题,请使用以下代码:

Sub CopyPaste()
    Application.ScreenUpdating = False
    Dim copySheet As Worksheet
    Dim pasteSheet As Worksheet

    Set copySheet = Worksheets("Sheet1")
    Set pasteSheet = Worksheets("Sheet1")

    copySheet.Range("R:R").Copy
    pasteSheet.Columns(pasteSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column + 1).PasteSpecial

    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
英文:

To avoid empty cell issue use this

Sub CopyPaste()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet


Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet1")

copySheet.Range("R:R").Copy
pasteSheet.Columns(pasteSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column + 1).PasteSpecial


Application.CutCopyMode = False  
Application.ScreenUpdating = True
End Sub

</details>



# 答案2
**得分**: 1

复制整列
-

```vb
Sub CopyEntireColumn()
    
    Application.ScreenUpdating = False
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' 包含此代码的工作簿
    
    Dim copySheet As Worksheet: Set copySheet = wb.Worksheets("Sheet1")
    Dim pasteSheet As Worksheet: Set pasteSheet = wb.Worksheets("Sheet1")
    
    Dim copyRange As Range: Set copyRange = copySheet.Columns("R")
    
    Dim pasteRange As Range:
    
    With pasteSheet.UsedRange
        Set pasteRange = .Resize(, 1).Offset(, .Columns.Count).EntireColumn
    End With
    
    copyRange.Copy pasteRange
    
    Application.ScreenUpdating = True

End Sub
```

不引入范围变量,您也可以使用更短但不太可读的方式(不建议):

```vb
With pasteSheet.UsedRange
    copySheet.Columns("R").Copy .Resize(, 1).Offset(, .Columns.Count).EntireColumn
End With
```

<details>
<summary>英文:</summary>

Copy Entire Column
-

&lt;!-- language: lang-vb --&gt;

    Sub CopyEntireColumn()
        
        Application.ScreenUpdating = False
        
        Dim wb As Workbook: Set wb = ThisWorkbook &#39; workbook containing this code
        
        Dim copySheet As Worksheet: Set copySheet = wb.Worksheets(&quot;Sheet1&quot;)
        Dim pasteSheet As Worksheet: Set pasteSheet = wb.Worksheets(&quot;Sheet1&quot;)
        
        Dim copyRange As Range: Set copyRange = copySheet.Columns(&quot;R&quot;)
        
        Dim pasteRange As Range:
        
        With pasteSheet.UsedRange
            Set pasteRange = .Resize(, 1).Offset(, .Columns.Count).EntireColumn
        End With
        
        copyRange.Copy pasteRange
        
        Application.ScreenUpdating = True
    
    End Sub

Instead of introducing the range variables, you could use the shorter but less readable  
(not recommended):

    With pasteSheet.UsedRange
        copySheet.Columns(&quot;R&quot;).Copy .Resize(, 1).Offset(, .Columns.Count).EntireColumn
    End With
       

</details>



# 答案3
**得分**: 0

```
不需要复制粘贴。

```
子复制范围()

Dim xLAppL As Excel.Application: Set xLAppL = GetObject(, "Excel.Application")
Dim wrkBk As Excel.Workbook: Set wrkBk = xLAppL.ActiveWorkbook
Dim wrksH As Worksheet: Set wrksH = wrkBk.ActiveSheet


Dim sourceArr() As Variant: sourceArr = wrksH.Range("r:r").Value2 '.CurrentRegion

    wrksH.Range("T1").Resize(UBound(sourceArr, 1), UBound(sourceArr, 2)).Value2 = sourceArr

End Sub
```

<details>
<summary>英文:</summary>

No need to copy paste.

```
Sub copyRange()

Dim xLAppL As Excel.Application: Set xLAppL = GetObject(, &quot;Excel.Application&quot;)
Dim wrkBk As Excel.Workbook: Set wrkBk = xLAppL.ActiveWorkbook
Dim wrksH As Worksheet: Set wrksH = wrkBk.ActiveSheet


Dim sourceArr() As Variant: sourceArr = wrksH.Range(&quot;r:r&quot;).Value2 &#39;.CurrentRegion

    wrksH.Range(&quot;T1&quot;).Resize(UBound(sourceArr, 1), UBound(sourceArr, 2)).Value2 = sourceArr

End Sub

```

</details>



huangapple
  • 本文由 发表于 2023年8月9日 16:47:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866037-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定