将多个工作表中的数据合并到列中关键字的一张表中

米尔科·斯坦尼斯奇

我很抱歉提出类似的问题,但我遇到了一个问题,bcs 我不太了解 VBA 编码......我发现了很多类似的问题,我找到了一个可以满足我的需求的代码。我在这里找到了代码但我不知道如何编辑该代码以便他可以在我的工作簿中工作。我有包含 35 个工作表的工作簿,所有工作表的格式都相同,值在“A:F”列中,在“E”列中,我有文本“库存”和“已发送”,我希望所有工作表中的所有行都具有“开”库存”列“E”中的值将复制到名为“空白列表”的工作表中。我试图自己编辑代码,但它无法运行,没有任何反应。提前致谢。编辑代码

Sub CommandButton4_Click()
    Dim wM As Worksheet, ws As Worksheet
    Dim r As Long, lr As Long, nr As Long, y As Long
    Dim c As Range, firstaddress As String
    Application.ScreenUpdating = False
    Set wM = Sheets("Blanko List")
    lr = wM.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
    If lr > 1 Then wM.Range("A2:G" & lr).ClearContents
    For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Blanko List" Then
    y = 0
    On Error Resume Next
    y = Application.CountIf(ws.Columns(7), "On Stock")
    On Error GoTo 0
    If y > 1 Then
      firstaddress = ""
      With ws.Columns(7)
        Set c = .Find("On Stock", LookIn:=xlValues, LookAt:=xlWhole)
        If Not c Is Nothing Then
          firstaddress = c.Address
          Do
            nr = wM.Range("G" & Rows.Count).End(xlUp).Offset(1).Row
            ws.Range("A" & c.Row & ":G" & c.Row).Copy wM.Range("A" & nr)
            Application.CutCopyMode = False
            Set c = .FindNext(c)
          Loop While Not c Is Nothing And c.Address <> firstaddress
        End If
      End With
    End If
  End If
Next ws
wM.Activate
Application.ScreenUpdating = True
    ''''

Original code:

    Option Explicit
    Sub GetYes()

    Dim wM As Worksheet, ws As Worksheet
    Dim r As Long, lr As Long, nr As Long, y As Long
    Dim c As Range, firstaddress As String
    Application.ScreenUpdating = False
    Set wM = Sheets("Master")
    lr = wM.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
    If lr > 1 Then wM.Range("A2:G" & lr).ClearContents
    For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Master" Then
    y = 0
    On Error Resume Next
    y = Application.CountIf(ws.Columns(7), "Yes")
    On Error GoTo 0
    If y > 1 Then
      firstaddress = ""
      With ws.Columns(7)
        Set c = .Find("Yes", LookIn:=xlValues, LookAt:=xlWhole)
        If Not c Is Nothing Then
          firstaddress = c.Address
          Do
            nr = wM.Range("G" & Rows.Count).End(xlUp).Offset(1).Row
            ws.Range("A" & c.Row & ":G" & c.Row).Copy wM.Range("A" & nr)
            Application.CutCopyMode = False
            Set c = .FindNext(c)
          Loop While Not c Is Nothing And c.Address <> firstaddress
        End If
      End With
    End If
  End If
Next ws
wM.Activate
Application.ScreenUpdating = True
End Sub
VBasic2008

复制条件行

Option Explicit

Sub CopyCriteriaRows()
    
    ' Source
    Const sCols As String = "A:F"
    Const sfRow As Long = 2
    Const scCol As Long = 5
    Const sCriteria As String = "On Stock"
    ' Destination
    Const dName As String = "Blanco List"
    Const dFirst As String = "A2"
    ' Exceptions
    Const ExceptionsList As String = "Blanco List" ' add more
    Const ListSeparator As String = ","
    ' Workbook
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Write the names of the worksheets to be 'processed' to an array.
    Dim wsNames As Variant
    wsNames = ArrWorksheetNames(wb, ExceptionsList, ListSeparator)
    If IsEmpty(wsNames) Then Exit Sub ' no worksheet found
    
    ' Create a reference to the first destination row range.
    ' Note that the number of columns is equal in source and destination.
    Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
    Dim cCount As Long: cCount = dws.Columns(sCols).Columns.Count
    Dim drrg As Range: Set drrg = dws.Range(dFirst).Resize(, cCount)
    
    Dim sws As Worksheet ' Source Worksheet
    Dim srg As Range ' Source Range
    Dim sfrrg As Range ' Source First Row Range
    Dim drg As Range ' Destination Range
    
    Dim Data As Variant ' Data Array
    Dim cValue As Variant ' Current Value
    
    Dim dr As Long ' Destination Row Counter
    Dim sr As Long ' Source Row Counter
    Dim c As Long ' Column Counter
    
    For Each sws In wb.Worksheets(wsNames)
        ' Create a reference to the current Source First Row Range.
        Set sfrrg = sws.Columns(sCols).Rows(sfRow)
        Set srg = Nothing
        ' Create a reference to the current Source Range.
        Set srg = RefColumns(sfrrg)
        If Not srg Is Nothing Then ' the current Source Range is not empty
            ' Write the values from the current Source Range to the Data Array.
            Data = GetRange(srg)
            ' Write the matches to the top of the Data Array. The size
            ' of the array stays the same but 'dr' is used: to track
            ' the number of, to move, and finally, to write (to the worksheet)
            ' the 'destination' values.
            dr = 0
            For sr = 1 To UBound(Data, 1)
                cValue = Data(sr, scCol)
                If StrComp(CStr(cValue), sCriteria, vbTextCompare) = 0 Then
                    dr = dr + 1
                    For c = 1 To cCount
                        Data(dr, c) = Data(sr, c)
                    Next c
                End If
            Next sr
            If dr > 0 Then ' there have been matches
                ' Create a reference to the Destination Range.
                Set drg = drrg.Resize(dr)
                ' Write only the 'destination' values (dr) from
                ' the Data Array to the Destination Range.
                drg.Value = Data
                ' Create a reference to the next Destination First Row Range.
                Set drrg = drrg.Offset(dr)
            End If
        End If
    Next sws
    
    ' The 'Clear Range' is the range spanning
    ' from the last 'Destination First Row Range'
    ' (which was referenced, but was not written to)
    ' to the bottom-most row range of the worksheet.
    Dim crg As Range
    Set crg = drrg.Resize(dws.Rows.Count - drrg.Row + 1)
    crg.ClearContents
    
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Returns the names of the worksheets of a workbook ('wb'),
'               that are not included in a list ('ExceptionsList'),
'               in an array.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ArrWorksheetNames( _
    ByVal wb As Workbook, _
    Optional ByVal ExceptionsList As String = "", _
    Optional ByVal ListSeparator As String = ",", _
    Optional ByVal FirstIndex As Long = 0) _
As Variant
    If wb Is Nothing Then Exit Function
    
    Dim wsCount As Long: wsCount = wb.Worksheets.Count
    If wsCount = 0 Then Exit Function ' There could e.g. only be charts.
    
    Dim IndexDiff As Long: IndexDiff = FirstIndex - 1
    Dim LastIndex As Long: LastIndex = wsCount + IndexDiff
    Dim Arr() As String: ReDim Arr(FirstIndex To LastIndex)
    Dim n As Long: n = IndexDiff
    
    Dim ws As Worksheet
    
    If Len(ExceptionsList) = 0 Then
        For Each ws In wb.Worksheets
            n = n + 1
            Arr(n) = ws.Name
        Next ws
    Else
        Dim Exceptions() As String
        Exceptions = Split(ExceptionsList, ListSeparator)
        For Each ws In wb.Worksheets
            If IsError(Application.Match(ws.Name, Exceptions, 0)) Then
                n = n + 1
                Arr(n) = ws.Name
            End If
        Next ws
    End If
    
    Select Case n
    Case IndexDiff
        Exit Function
    Case Is < LastIndex
        ReDim Preserve Arr(FirstIndex To n)
    End Select
    
    ArrWorksheetNames = Arr
    
End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Creates a reference to the range spanning from the first row
'               of a given range ('rg') to the row containing the bottom-most
'               non-empty cell of the given range's columns.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RefColumns( _
    ByVal rg As Range) _
As Range
    If rg Is Nothing Then Exit Function
    
    With rg.Rows(1)
        Dim lCell As Range
        Set lCell = .Resize(.Worksheet.Rows.Count - .Row + 1) _
            .Find("*", , xlFormulas, , xlByRows, xlPrevious)
        If lCell Is Nothing Then Exit Function ' empty range
        Set RefColumns = .Resize(lCell.Row - .Row + 1)
    End With

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Returns the values of a range in a 2D one-based array.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetRange( _
    ByVal rg As Range) _
As Variant
    If rg Is Nothing Then Exit Function
    
    Dim rData As Variant
    If rg.Rows.Count + rg.Columns.Count = 2 Then ' one cell only
        ReDim rData(1 To 1, 1 To 1): rData(1, 1) = rg.Value
    Else
        rData = rg.Value
    End If

    GetRange = rData
End Function


' Irrelevant to the Question,
' but for a better understanding of `ArrWorksheetNames`.

Sub ArrWorksheetNamesTEST()
    
    Const ExceptionsList As String = "Sheet1,Sheet2,Sheet3,Sheet4"
    Const ListSeparator As String = ","
    Const FirstIndex As Long = 4
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    
    Dim wsNames As Variant
    wsNames = ArrWorksheetNames(wb, ExceptionsList, ListSeparator, FirstIndex)
    
    If IsEmpty(wsNames) Then
        Debug.Print "No worksheets."
    Else
        Debug.Print "[" & LBound(wsNames) & "," & UBound(wsNames) & "]" _
            & vbLf & Join(wsNames, vbLf)
    End If

End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将多个谷歌工作表更快地合并到一张工作表中

将多个 Excel 文件合并到一张 Excel 工作表中,无需复制公式和重复数据

将一张以上的表合并到一张现有的表中

将一张表中的多行合并到一个SQL查询结果集中的多个列中

数组公式,用于将几列合并到另一张工作表中。可能吗?

将多个工作表中的数据复制到一张工作表中

将多张工作表中的列合并为另一张工作表中的一列

Excel VBA 将多个工作簿合并到一张纸中

使用 for cyclus 将 Pandas 中的数据排序到工作表 - 一张工作表中的多个数据

将Postgres查询结果合并到一张表中

如何将多级统计信息合并到一张表中?

将2种不同的mySql结果合并到一张表中

试图从多个工作表中一张一张地复制数据并粘贴到不同的工作表中。

将多个表中的数据合并到一个列中

Excel VBA-将特定的列从多个文件合并到一张表

将一张表中的数据添加到另一张表中

在一张工作表中合并两个或多个 Private Sub

从一列中选择多个数据,然后将合并结果更新到另一张表中

将一张工作表中列中的范围转移到另一张工作表中的一行

将所有数据合并到新的工作表中,第一工作表除外

如何使用VBA将多个工作簿的第一列合并到其他工作簿的工作表中

检查谷歌工作表中单元格数据中的唯一关键字

在Postgresql中多次合并到同一张表的最佳方法是什么?

将数据插入另一张工作表中的下一个空白行或列的宏?

将同一张表中的 3 个 select 语句合并到 sql 中的 1 个表中

如何将一列中的数据插入到另一张表中

如何通过pandas将多个工作表中的列合并到一个excel文件中

从一张表中获取2列的数据

将 Excel 工作簿合并到一张工作表