vba - 从 VBA 复制范围中排除 1 行

标签 vba excel copy range

我正在编写一些代码来合并多个工作表,这些工作表将各个零件列表形成一个大的零件列表。

到目前为止,我有两个函数可以扫描每个工作表的最后一行和最后一列

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
                        After:=sh.Range("A1"), _
                        Lookat:=xlPart, _
                        LookIn:=xlFormulas, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Row
    On Error GoTo 0
End Function

Function LastCol(sh As Worksheet)
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
                        After:=sh.Range("A1"), _
                        Lookat:=xlPart, _
                        LookIn:=xlFormulas, _
                        SearchOrder:=xlByColumns, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Column
    On Error GoTo 0
End Function

然后我有另一个子程序,它创建一个名为“零件列表”的新工作表并将范围粘贴到其中。

Sub CopyRangeFromMultiWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

' Delete the summary sheet if it exists.
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Parts List").Delete
On Error GoTo 0
Application.DisplayAlerts = True

' Add a new summary worksheet.
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "Parts List"


' Loop through all worksheets and copy the data to the
' summary worksheet.
For Each sh In ActiveWorkbook.Worksheets
    If sh.Name <> DestSh.Name Then

        ' Find the last row with data on the summary worksheet.
        Last = LastRow(DestSh)

        ' Specify the range to place the data.
        ' Set CopyRng = sh.Range("B3:G10").
        Set CopyRng = sh.UsedRange

        ' Test to see whether there are enough rows in the summary
        ' worksheet to copy all the data.
        If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
            MsgBox "There are not enough rows in the " & _
               "summary worksheet to place the data."
            GoTo ExitTheSub
        End If

        ' This statement copies values and formats from each
        ' worksheet.
        CopyRng.Copy
        With DestSh.Cells(Last + 1, "A")
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
        End With

        ' Optional: This statement will copy the sheet
        ' name in the H column.
        DestSh.Cells(Last + 1, "I").Resize(CopyRng.Rows.Count).Value = sh.Name

    End If
Next

ExitTheSub:

Application.Goto DestSh.Cells(1)

' AutoFit the column width in the summary sheet.
DestSh.Columns.AutoFit

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
End Sub

我遇到的问题是标题行与范围一起复制。有谁知道如何从行和列扫描或复制中排除标题?

enter image description here enter image description here

感谢您的帮助 丹

最佳答案

还没有测试过它,但是这些方面的内容应该可以帮助您循环遍历单元格中的所有行并使用 union 函数从中创建一个新范围。然后,当检查所有行的数值时,可以使用您的代码复制总范围。

Dim row as integer
Dim temprange as range
Dim totalrange as range
Dim startrow as integer
For row = 2 to lastrow+1  `assuming there is always a title in row 1
If IsNum(Cells(row,1)) = false Then
    If temprange = Nothing then
         Set temprange = Range(Cells(2,1),Cells(row-1,[lastcolumn number] `[replace with number of last column]
         startrow = row+1
    Else
         Set temprange = Range(Cells(startrow,1),Cells(row-1,[lastcolumn number])
    End if
    If totalrange <> Nothing then
          Set totalrange = Union(totalrange,temprange)
    Else
          Set totalrange = temprange
    End if
End if
Next row

第二种方法,复制前删除标题行

For row = lastrow to 1 step -1
If IsNum(Cells(row,1) = False then
    Rows(row).EntireRow.Delete
End if
Next row

然后再次调用最后一行函数并执行其余代码。

关于vba - 从 VBA 复制范围中排除 1 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45299051/

相关文章:

excel - 我可以在同一工作簿中同时使用 Office 脚本和 VBA 宏吗?

excel - 如何在 VBA 中设置全局变量

svn - svn 复制后文件最终位于错误的目录中

ruby - 将元素添加到 ruby​​ 数组返回新数组

ms-access - 打字时更新文本框

excel - VBA:将数据从一列复制并转置到新行

vba - 从所有 Excel 表格 VBA 中删除过滤器

android - 将/data/data 中的文件夹复制到 sdcard,反之亦然

vba - Excel 正在等待另一个应用程序完成 OLE 操作

vba - 添加额外的 VBA 模块会在运行以前工作的模块时产生编译错误