gpt4 book ai didi

vba - 如何从某一行开始将整列复制到另一列

转载 作者:行者123 更新时间:2023-12-04 20:42:24 25 4
gpt4 key购买 nike

我目前正在做一个宏,将整个列从一个工作表复制到另一列,从另一个工作表的第 2 行开始。但是我认为它不起作用,Set des = Sheets(3).Range("P2:P" & Rows(i))这是行导致错误。

Sub Sample()
Dim lastRow As Long, i As Long
Dim CopyRange As Range
Dim rw As Range
Dim rw1 As Range
Dim rw2 As Range
Dim rw3 As Range
Dim des As Range
Dim des1 As Range
Dim des2 As Range
Dim des3 As Range
'~~> Change Sheet1 to relevant sheet name
With Sheets(1)
lastRow = .Range("A" & .Rows.Count).End(xlUp).row

For i = 2 To lastRow
If Len(Trim(.Range("A" & i).Value)) <> 0 Then
If CopyRange Is Nothing Then
Set CopyRange = .Rows(i)
Else
Set CopyRange = Union(CopyRange, .Rows(i))
Set rw = .Columns(16)
Set rw1 = .Columns(23)
Set rw2 = .Columns(3)
Set rw3 = .Columns(18)
End If
End If
Next

If Not CopyRange Is Nothing Then
Set des = Sheets(3).Range("P2:P" & Rows(i))
Set des1 = Sheets(3).Columns("R")
Set des2 = Sheets(3).Columns("T")
Set des3 = Sheets(3).Columns("U")
'~~> Change Sheet2 to relevant sheet name
rw.Copy des
rw1.Copy des1
rw2.Copy des2
rw3.Copy des3

Application.CutCopyMode = False
End If
End With
End Sub

最佳答案

您需要将复制的列更改为范围以正确粘贴它们,如下所示:

Sheets(1).Range("A1:A" & Sheets(1).Cells(Sheets(1).Rows.Count, "a").End(xlUp).Row).Copy ' this code copies from first to last cell with data in column A

然后稍后您需要将它们粘贴到例如
sheets(3).range("R2").paste

关于vba - 如何从某一行开始将整列复制到另一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30391994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com