Excel 将粘贴从多张工作表复制到一张工作表(仅需要多列,并在合并工作表和seprate上设置自动筛选)
我有三张表1、表2和表5需要从表1 A、B列复制到表5 c、G、H,并从表2 J、K、N列复制粘贴到表5 c、G,H不应覆盖和Sheet5我的前三行是我的标题,它应粘贴Sheet5列G,其中包含sheet1和sheet2中的数据:需要使用自动筛选分离特定文本JOhn,Alex,france。单独的工作表名称应为JOhn,Alex,法国。在代码方面需要您的帮助:我已经尝试了以下代码,但这些代码不符合我的要求,并且无法编辑,因为我从google Sub copypaste Dim lastrow获得的更多条件是整数,erow为整数,sheet1为工作表,sheet2作为工作表集合sheet1=工作表sheet1集合sheet2=工作表sheet2 lastrow=sheet1.CellsRows.Count,1.EndxlUp.Row For i=2到lastrow erow=sheet2.CellsRows.Count,2.EndxlUp.Offset1,0.Row sheet2.CellsRow,2=sheet1.CellsRow,3=sheet2.CellsRow,3=sheet1.Cellsi,6 sheet2.CellsRow=sheet1.CellsRow,4=sheet1.Cellsi下一个i-End子Excel 将粘贴从多张工作表复制到一张工作表(仅需要多列,并在合并工作表和seprate上设置自动筛选),excel,vba,Excel,Vba,我有三张表1、表2和表5需要从表1 A、B列复制到表5 c、G、H,并从表2 J、K、N列复制粘贴到表5 c、G,H不应覆盖和Sheet5我的前三行是我的标题,它应粘贴Sheet5列G,其中包含sheet1和sheet2中的数据:需要使用自动筛选分离特定文本JOhn,Alex,france。单独的工作表名称应为JOhn,Alex,法国。在代码方面需要您的帮助:我已经尝试了以下代码,但这些代码不符合我的要求,并且无法编辑,因为我从google Sub copypaste Dim lastrow获得
Sub CopyDataWithoutHeaders()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim shLast As Long
Dim CopyRng As Range
Dim StartRow As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the sheet "RDBMergeSheet" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a worksheet with the name "RDBMergeSheet"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "RDBMergeSheet"
'Fill in the start row
StartRow = 2
'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
'Find the last row with data on the DestSh and sh
Last = LastRow(DestSh)
shLast = LastRow(sh)
'If sh is not empty and if the last row >= StartRow copy the CopyRng
If shLast > 0 And shLast >= StartRow Then
'Set the range that you want to copy
Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))
'Test if there enough rows in the DestSh to copy all the data
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If
'This example copies values/formats, if you only want to copy the
'values or want to copy everything look below example 1 on this page
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
'AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
有关更多详细信息,请参阅下面的链接
子copypaste Dim lastrow为整数,erow为整数,sheet1为工作表,sheet2为工作表集合sheet1=工作表sheet1集合sheet2=工作表sheet2 lastrow=sheet1.CellsRows.Count,i=2的1.EndxlUp.Row到lastrow erow=sheet2.CellsRows.Count,2.EndxlUp.OFSET1,0.Row sheet2=sheet2.CELLSEROWS,3个sheet2.CellsRow,3=sheet1.Cellsi,6个sheet2.CellsRow,4=sheet1.Cellsi,9下一个i'ThisWorkbook.WorksheetsSheets2.Columns.AutoFit'sheet1.Cells1,1.激活结束子子子子子子子子文件夹CopyPase1 Dim lastrow作为整数,erow作为整数,sheet5作为工作表,sheet2作为工作表集合sheet5=工作表sheet5集合sheet2=工作表sheet2 lastrow=sheet5.CellsRows.Count,1.EndxlUp.Row For i=2到lastrow erow=sheet2.CellsRows.Count,2.EndxlUp.Offset1,0.Row sheet2.CellsRow,2=sheet5.Cellsi,3=sheet2.CellsRow,3=sheet5.Cellsi,6 sheet2.CellsRow=sheet5.Cellsi,9下一步我将“ThisWorkbook.WorksheetsSheet2.Columns.AutoFit”sheet1.Cells1,1.激活结束子问题,将代码作为文本。我使用的是上述代码,但没有帮助me@Basavarajk.s. 注释中的代码是无用途的,因为它不可读。换行符在VBA代码中很重要,但在注释中看不到它们。请回答您的原始问题并在此处添加代码。使用编辑器工具栏中的按钮或ctrl+K将其正确格式化为代码块。