【问题标题】:Matching dictionaries and creating new wb匹配字典并创建新的 wb
【发布时间】:2020-08-04 00:41:44
【问题描述】:

我有两张表,我想比较出现在两张表第一列中的“代码”。

这是 sheet1: Sheet1

这是 sheet2:sheet2

我想遍历 sheet1 上的每个代码,并找到 sheet 2 上具有相同代码的所有行,并将行(来自 sheet2)插入新的 wb。

这就是我创建字典的方式。

iLastRow = ws1.Cells(Rows.Count, 3).End(xlUp).Row
For iRow = 18 To iLastRow
    sKey = ws1.Cells(iRow, 3)
    If Dict.Exists(sKey) Then
        Dict(sKey) = Dict(sKey) & ";" & iRow ' matched row on sheet 1
    Else
        Dict(sKey) = iRow
    End If
    Debug.Print ((sKey))
Next

Debug.Print ("These are the values in dictionary2")
'' Dictionary broker code sheet 2
iLastRow = ws2.Cells(Rows.Count, 1).End(xlUp).Row
For iRow = 2 To iLastRow
    sBROKER = ws2.Cells(iRow, 1)
    If Dict.Exists(sBROKER) Then
        dictBROKER(sBROKER) = dictBROKER(sKey) & ";" & iRow ' matched row on sheet 1
    Else
        dictBROKER(sBROKER) = iRow
    End If
    Debug.Print ((sBROKER))
Next

调试打印部分:debug.print

希望有人能帮助我

【问题讨论】:

  • 您的代码没有在字典中输入范围(行)。它只输入行号...您不能将其用于您声明的目的...
  • 嗨@FaneDuru。那你有办法解决这个问题吗?
  • 如果我能理解您想要完成的工作,我可以提供帮助,但我承认我不...尝试用文字解释您的代码。否则,这对我来说看起来很奇怪。它在字典中搜索 A:A 的 ws2 值,它应该做什么?替换现有值?那么,如果没有找到匹配项,dictBROKER(sBROKER) = iRow 想要成为什么?为什么加载找不到匹配的行?
  • “工作表 2 上具有相同代码的所有行”是什么意思?您的行保留什么“代码”?您是指 A:A 列中的“kode”吗?
  • 在 sheet1 我有一列(列“C”)。在该列中,我有一些仅出现一次的值(该值是特定代码)。例如,我在单元格“c18”中有值 00010。所以 00010 只出现在单元格 c18 中,而不是稍后出现在该列中。但是在 sheet2 中 00010 可以出现多次。我想要做的是创建一个新的 wb,其中的所有值都与 sheet1 中的特定值匹配。因此,如果 c18=00010 我想从 sheet2(整行)中取出所有 00010 并插入新的 wb

标签: excel vba


【解决方案1】:

请尝试下一个代码:

Sub copyToNewSheets()
 Dim ws1 As Worksheet, ws2 As Worksheet, rngC As Range, skey As String
 Dim i As Long, j As Long, lastCol As Long, iLastRow, jLastRow As Long
 Dim Wb As Workbook, wsNew As Worksheet, k As Long, rngHeader As Range

 Set ws1 = ActiveSheet               'use here your sheet
 Set ws2 = Worksheets("SecondSheet") 'use here your sheet, too
 iLastRow = ws1.cells(Rows.count, 3).End(xlUp).Row
 jLastRow = ws2.cells(Rows.count, 3).End(xlUp).Row
 Set rngHeader = ws2.Range("A1:E1")

 'Create the new workbook
 Set Wb = Workbooks.Add
 For i = 1 To Wb.Worksheets.count - 1
    Application.DisplayAlerts = False
       Wb.Sheets(i).Delete
    Application.DisplayAlerts = True
 Next i
 
  'for making the code faster:_________________
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual
  '____________________________________________
  
  lastCol = 5: k = 1
  For i = 18 To iLastRow
    skey = ws1.cells(i, 3).Value
    For j = 2 To jLastRow
        If skey = ws2.Range("A" & j).Value Then
            If rngC Is Nothing Then
                Set rngC = ws2.Range(ws2.Range("A" & j), ws2.cells(j, lastCol))
            Else
                Set rngC = Union(rngC, ws2.Range(ws2.Range("A" & j), ws2.cells(j, lastCol)))
            End If
        End If
    Next j
    If Not rngC Is Nothing Then
        If k = 1 Then
            Set wsNew = Wb.Sheets(k): k = k + 1
        Else
            Set wsNew = Wb.Sheets.Add(After:=Wb.Sheets(k - 1)): k = k + 1
            
        End If
        wsNew.Name = skey
        rngHeader.Copy Destination:=wsNew.Range("A1")
        rngC.Copy Destination:=wsNew.Range("A2")
        Set rngC = Nothing
   End If
 Next i
  Application.ScreenUpdating = True
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  MsgBox "Ready...", vbInformation
End Sub

【讨论】:

  • 所以它应该创建一个新的工作簿,但是它将 sheet2 中的所有值插入到同一个新工作簿中。所以我需要做的是为每个组/特定代码创建一个文件。
  • 所以为表 2 中所有 00010 的新工作簿,表 2 中 30200 的新文件等。这有意义吗?
  • 你有没有说什么,我错过了?现在我在开车。如果是的话,我会在家里适应它
  • 我想我已经试着解释过了。对不起,如果我的解释不够清楚。
  • 太棒了!感谢您的帮助,谢谢!假设我想要一个工作簿而不是每个代码的工作表,那么上面的代码很难改变吗?
【解决方案2】:

这里有一个稍微不同的方法

Sub CopyToNewWorkbook()

    Dim oMasterWS As Worksheet: Set oMasterWS = Sheet3        '<- Change to the sheet that has the codes
    Dim oDataWS As Worksheet: Set oDataWS = Sheet4            '<- Change to sheet where you want to copy row from
    Dim oNewWB As Workbook
    Dim iTRMWS As Long: iTRMWS = oMasterWS.Range("A" & oMasterWS.Rows.Count).End(xlUp).Row
    Dim iTRDWS As Long
    Dim iC As Long
    Dim oFilterRng As Range
    
    With oDataWS
        
        ' Get Data sheet row count
        If .AutoFilterMode Then .AutoFilterMode = False
        iTRDWS = .Range("A" & .Rows.Count).End(xlUp).Row
        
        ' Loop through all values in Master sheet
        For iC = 2 To iTRMWS
            
            ' Set filter on Data sheet based on the value from Master sheet
            .Range("A1").AutoFilter Field:=1, Criteria1:=oMasterWS.Range("A" & iC).Value
            
            ' Set filtered range
            Set oFilterRng = Nothing
            On Error Resume Next
            Set oFilterRng = .Range("A2:A" & iTRDWS).SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            
            ' If filtered range is found, copy it to a new workbook
            If Not oFilterRng Is Nothing Then
                Set oNewWB = Workbooks.Add
                oFilterRng.EntireRow.Copy oNewWB.Sheets(1).Range("A1")
                oNewWB.SaveAs ThisWorkbook.Path & "\" & oMasterWS.Range("A" & iC).Value
                oNewWB.Close savechanges:=False
            End If
            
            ' Clear filter
            If .AutoFilterMode Then .AutoFilterMode = False
        
        Next
        
    End With
    
End Sub

【讨论】:

  • 感谢您提供解决方案!但是@FaneDuru 提供了我所需要的,这就是为什么我将他的答案标记为我的问题的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 2021-12-19
  • 2021-12-11
  • 2020-09-04
  • 1970-01-01
相关资源
最近更新 更多