excel的vb中单击命令按钮把sheet1中的A列内容复制到sheet6中A列
2个回答
2016-04-18
展开全部
程序如下,希望你喜欢:
Sub Run_Sort_Copy()
On Error GoTo ErrExit
Dim strSheetName_1 As String, strSheetName_2 As String
Dim iClass As Long, iMaxRow As Long, iRow As Long, iNewRow As Long
Application.ScreenUpdating = False
strSheetName_1 = "Sheet1" '此处可修改表1名称
strSheetName_2 = "Sheet2" '此处可修改表2名称
Sheets(strSheetName_1).Select
Range("A1").CurrentRegion.Select
iMaxRow = Selection.Rows.Count
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range( _
"C2"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase _
:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:= _
xlSortNormal, DataOption2:=xlSortNormal
iRow = 2
iClass = Range("B2").Value
iNewRow = 1
GoSub CopyMain
GoSub CopyRange
iRow = 3
Do Until iRow > iMaxRow
If Range("B" & iRow).Value = iClass Then
GoSub CopyRange
Else
iClass = Range("B" & iRow).Value
iNewRow = iNewRow + 2
GoSub CopyMain
GoSub CopyRange
End If
iRow = iRow + 1
Loop
Application.CutCopyMode = False
Application.StatusBar = False
Application.ScreenUpdating = True
Exit Sub
ErrExit:
MsgBox Err.Description, vbCritical, "错误"
Application.CutCopyMode = False
Application.StatusBar = False
Application.ScreenUpdating = True
Exit Sub
CopyMain:
Application.StatusBar = "正在处理 " & iRow & " / " & iMaxRow & " 行"
Sheets(strSheetName_2).Select
Range("A" & iNewRow) = iClass
iNewRow = iNewRow + 1
Sheets(strSheetName_1).Select
Rows("1:1").Select
Selection.Copy
Sheets(strSheetName_2).Select
Range("A" & iNewRow).Select
Selection.Insert Shift:=xlDown
iNewRow = iNewRow + 1
Sheets(strSheetName_1).Select
Return
CopyRange:
Application.StatusBar = "正在处理 " & iRow & " / " & iMaxRow & " 行"
Rows(iRow & ":" & iRow).Select
Selection.Copy
Sheets(strSheetName_2).Select
Range("A" & iNewRow).Select
Selection.Insert Shift:=xlDown
iNewRow = iNewRow + 1
Sheets(strSheetName_1).Select
Return
End Sub
Sub Run_Sort_Copy()
On Error GoTo ErrExit
Dim strSheetName_1 As String, strSheetName_2 As String
Dim iClass As Long, iMaxRow As Long, iRow As Long, iNewRow As Long
Application.ScreenUpdating = False
strSheetName_1 = "Sheet1" '此处可修改表1名称
strSheetName_2 = "Sheet2" '此处可修改表2名称
Sheets(strSheetName_1).Select
Range("A1").CurrentRegion.Select
iMaxRow = Selection.Rows.Count
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range( _
"C2"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase _
:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:= _
xlSortNormal, DataOption2:=xlSortNormal
iRow = 2
iClass = Range("B2").Value
iNewRow = 1
GoSub CopyMain
GoSub CopyRange
iRow = 3
Do Until iRow > iMaxRow
If Range("B" & iRow).Value = iClass Then
GoSub CopyRange
Else
iClass = Range("B" & iRow).Value
iNewRow = iNewRow + 2
GoSub CopyMain
GoSub CopyRange
End If
iRow = iRow + 1
Loop
Application.CutCopyMode = False
Application.StatusBar = False
Application.ScreenUpdating = True
Exit Sub
ErrExit:
MsgBox Err.Description, vbCritical, "错误"
Application.CutCopyMode = False
Application.StatusBar = False
Application.ScreenUpdating = True
Exit Sub
CopyMain:
Application.StatusBar = "正在处理 " & iRow & " / " & iMaxRow & " 行"
Sheets(strSheetName_2).Select
Range("A" & iNewRow) = iClass
iNewRow = iNewRow + 1
Sheets(strSheetName_1).Select
Rows("1:1").Select
Selection.Copy
Sheets(strSheetName_2).Select
Range("A" & iNewRow).Select
Selection.Insert Shift:=xlDown
iNewRow = iNewRow + 1
Sheets(strSheetName_1).Select
Return
CopyRange:
Application.StatusBar = "正在处理 " & iRow & " / " & iMaxRow & " 行"
Rows(iRow & ":" & iRow).Select
Selection.Copy
Sheets(strSheetName_2).Select
Range("A" & iNewRow).Select
Selection.Insert Shift:=xlDown
iNewRow = iNewRow + 1
Sheets(strSheetName_1).Select
Return
End Sub
追问
这么多吗?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询