将列复制并粘贴到另一列
Copy and Paste Column to another column
我想搜索列标题以查找包含文本“国家/地区代码”的标题。
我想剪切这一栏并粘贴到第六栏。
我知道Destination:=Worksheets("Sheet1").Range("E5")
错了。
屏幕截图:国家代码在 W 列。我想粘贴到新的 F 列。
Sub Sample()
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim colName As String
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set aCell = .Range("A1:X50").Find(What:="CountryCode", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'~~> If Found
If Not aCell Is Nothing Then
Worksheets("Sheet1").Range("W1:W3").Cut _
Destination:=Worksheets("Sheet1").Range("E5")
Columns([23]).EntireColumn.Delete
Columns("F:F").Insert Shift:=xlToRight, _
CopyOrigin:=xlFormatFromLeftOrAbove
'~~> If not found
Else
MsgBox "Country Not Found"
End If
End With
End Sub
不需要使用删除或插入。 Range().Cut Destination:=Range()
将为您移动单元格。
Sub Sample()
Dim aCell As Range
With ThisWorkbook.Sheets("Sheet1")
Set aCell = .Rows(1).Find(What:="CountryCode", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.EntireColumn.Cut Destination:=.Columns(5)
Else
MsgBox "Country Not Found"
End If
End With
End Sub
此代码是否符合您的要求?
Sub Sample()
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim colName As String
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set aCell = .Range("A1:X50").Find(What:="CountryCode", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'~~> If Found
If Not aCell Is Nothing Then
'~~> Cut the entire column
aCell.EntireColumn.Cut
'~~> Insert the column here
Columns("F:F").Insert Shift:=xlToRight
Else
MsgBox "Country Not Found"
End If
End With
End Sub
我想搜索列标题以查找包含文本“国家/地区代码”的标题。
我想剪切这一栏并粘贴到第六栏。
我知道Destination:=Worksheets("Sheet1").Range("E5")
错了。
屏幕截图:国家代码在 W 列。我想粘贴到新的 F 列。
Sub Sample()
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim colName As String
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set aCell = .Range("A1:X50").Find(What:="CountryCode", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'~~> If Found
If Not aCell Is Nothing Then
Worksheets("Sheet1").Range("W1:W3").Cut _
Destination:=Worksheets("Sheet1").Range("E5")
Columns([23]).EntireColumn.Delete
Columns("F:F").Insert Shift:=xlToRight, _
CopyOrigin:=xlFormatFromLeftOrAbove
'~~> If not found
Else
MsgBox "Country Not Found"
End If
End With
End Sub
不需要使用删除或插入。 Range().Cut Destination:=Range()
将为您移动单元格。
Sub Sample()
Dim aCell As Range
With ThisWorkbook.Sheets("Sheet1")
Set aCell = .Rows(1).Find(What:="CountryCode", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.EntireColumn.Cut Destination:=.Columns(5)
Else
MsgBox "Country Not Found"
End If
End With
End Sub
此代码是否符合您的要求?
Sub Sample()
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim colName As String
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set aCell = .Range("A1:X50").Find(What:="CountryCode", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'~~> If Found
If Not aCell Is Nothing Then
'~~> Cut the entire column
aCell.EntireColumn.Cut
'~~> Insert the column here
Columns("F:F").Insert Shift:=xlToRight
Else
MsgBox "Country Not Found"
End If
End With
End Sub