我尝试使用以下代码将整个列从一个页面复制到另一个页面。
我常见的标题是6月分散在表格中,我只想复制列,标题名称为" 6月"在另一张纸上一个接一个地。
如果Col A,C,L,M的列标题为6月,那么在下一张表中,这些应仅复制为A,B,C,D。
Sheets("sheet1").Select
June = WorksheetFunction.Match("Description", Rows("1:1"), 0)
Sheets("sheet1").Columns(June).Copy Destination:=Sheets("sheet2").Range("A1")
答案 0 :(得分:1)
以下可能有所帮助:
Option Explicit
Sub Demo()
Dim srcSht As Worksheet, destSht As Worksheet
Dim headerRng As Range, cel As Range, copyRng As Range
Dim lastCol As Long, col As Long
Set srcSht = ThisWorkbook.Sheets("Sheet1") 'Sheet1
Set destSht = ThisWorkbook.Sheets("Sheet2") 'Sheet2
lastCol = srcSht.Cells(1, srcSht.Columns.Count).End(xlToLeft).Column 'last column of Sheet1
With srcSht
For Each cel In .Range(.Cells(1, 1), .Cells(1, lastCol)) 'loop through each header name in Sheet1
If cel = "June" Then 'check header is "June"
If copyRng Is Nothing Then 'assign range to copy
Set copyRng = .Columns(cel.Column)
Else
Set copyRng = Union(copyRng, .Columns(cel.Column))
End If
End If
Next cel
End With
copyRng.Copy destSht.Range("A1") 'copy and paste desired columns
End Sub