Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 复制所有工作表中的范围并将其粘贴到新工作表中_Excel_Vba - Fatal编程技术网

Excel 复制所有工作表中的范围并将其粘贴到新工作表中

Excel 复制所有工作表中的范围并将其粘贴到新工作表中,excel,vba,Excel,Vba,请帮助我,我需要复制不同的区域,直到所有工作表的第一个空白单元格,然后将它们粘贴到新的单元格中。都在同一个工作簿中 这是我的尝试: Sub Target() Dim lRow As Long Dim copyRange As Range Dim sh As Worksheet Dim shReport As Worksheet Set shReport = ThisWorkbook.Worksheets("Target") Fo

请帮助我,我需要复制不同的区域,直到所有工作表的第一个空白单元格,然后将它们粘贴到新的单元格中。都在同一个工作簿中

这是我的尝试:

Sub Target()

   Dim lRow As Long
   Dim copyRange As Range
    Dim sh As Worksheet
    Dim shReport As Worksheet
    Set shReport = ThisWorkbook.Worksheets("Target")

    For Each sh In ThisWorkbook.Worksheets
        Select Case sh.Name
            Case Is <> "ALLProjectForReport"
                lRow = shReport.Cells(Rows.Count, "B").End(xlUp).Row
               Set copyRange = sh.Range("A3")

               copyRange.Copy Destination:=shReport.Range("B" & lRow)

        End Select
    Next
    Set shReport = Nothing
    Set sh = Nothing
End Sub
将部分列向下堆叠 选项显式 子报表 将wb设置为工作簿:设置wb=ThisWorkbook 将dst设置为工作表:设置dst=wb.WorksheetsTarget 暗淡的cel As范围 设置cel=dst.Cellsdst.Rows.Count,B.EndxlUp.Offset1 Dim src As工作表 变暗rng As范围 对于wb.工作表中的每个src 选择Case src.Name Case AllProjectForReport,Target 其他情况 带src.RangeA3 设置rng=.EndxlDown 如果rng.Row0,则 设置rng=.Offset 其他的 设置rng=无 如果结束 如果结束 如果不是,那么rng什么都不是 rng.复制目标:=cel 设置cel=cel.offsetrung.Rows.Count 如果结束 以 结束选择 下一个 已创建MsgBox报告,vbInformation,成功 端接头
这不是一件容易的事。就我个人而言,在我开始编写宏之前,我首先要编写我希望代码做的事情,分小块编写。然后我用编程来试一试

为了避免在代码中迷失方向,另一个建议是创建描述性变量名

我在这段代码中非常具有描述性,以帮助您更好地理解它是如何工作的。如果您测试它,请记住更改宏配置部分中的值

Sub Copy_Paste_Target_Ranges()

Dim divya_sheet As Worksheet, ranges_last_col As String, col_paste As Long
Dim next_row As Long, ranges_first_row As String, ranges_first_col As String
Dim last_row As Long, target_range As String, new_sheet As String

'CONFIG
'----------------------
ranges_first_row = 10       'first row of the data sets
ranges_first_col = "e"      'first column of the data sets
ranges_last_col = "g"       'last column of the data sets
new_sheet = "New Sheet"     'name of the new sheet to create
col_paste = 2               'start pasting from this column number
'-----------------------

'create a new sheet
Sheets.Add
ActiveSheet.Name = new_sheet

'do a loop for all the sheets in this workbook
For Each divya_sheet In ThisWorkbook.Sheets

    'if divya_sheet is different from the new sheet proceed
    If divya_sheet.Name <> new_sheet Then

    With Sheets(divya_sheet.Name)

    'discover the last row of this data set before a blank cell
    last_row = _
    .Cells(ranges_first_row, ranges_first_col).End(xlDown).Row

    'create range
    target_range = _
    ranges_first_col & ranges_first_row & ":" & ranges_last_col & last_row

    'copy data in this range
    .Range(target_range).Copy

    'go to the last row of the sheet and then
    'search for the first occupied cell
    'starting from the bottom up. Sum one to get the next row number.
    next_row = _
    Sheets(new_sheet).Cells(Rows.Count, col_paste).End(xlUp).Row + 1

    'paste copied range
    Sheets(new_sheet).Cells(next_row, col_paste).PasteSpecial

    End With

    End If

Next divya_sheet

End Sub

你已经试过什么了?请发布您尝试过的内容。以上是我尝试过的代码。首先,在For循环中使用基本IF语句来测试工作表。第二,将复制范围从A3设置为A列中最后使用的行。第三,将复制的范围粘贴到B列中的下一个空单元格时,它应仅为一个单元格。您还需要为您复制的每个工作表范围重置shReport Col B中的下一个空单元格。Hi@Gassz感谢您在这方面的帮助,但我仍然面临一个问题,我尝试更改范围。\u first\u row=53,但它仍然给我空白单元格,数据继续。请帮我弄清楚这一点,这将是非常感激的想象我有数据在表1第52行A52行向下,即1,2,3,4…空白单元格…6,7,8。。。。。。我希望它只复制粘贴到1,2,3,4,并且转到工作表2它应该再次复制数据,只复制到一个空白单元格。注意*它不应该在一个空白单元格得到你之后复制数据!这是一个简单的问题fix@divyadarshini,我刚修改了密码。它应该能解决你的问题。执行此操作的方法是使用EndxlDown.Row而不是EndxlUp.Row。它将从特定行开始查找数据集中最后一个占用的行。在这种情况下,这是RangeSeFixSTyLo.Valable。当我把我的范围更改为A53时,这个特殊的代码不起作用。请帮助我,你应该考虑在Gassz的帖子中添加你提供的附加信息。我已经相应地编辑了我的答案。非常感谢!!!!!!!!!我真的很感谢你的帮助。信息技术worked@divyadarschini记得接受最好的答案,并通过投票决定答案来回报。