VBA Create a table from two different workbooks based on Active status and report missing data or errors

Nena99

New Member
Joined
Apr 7, 2022
Messages
28
Office Version
  1. 2021
Platform
  1. Windows
I want to merge two tables from two different workbooks and that is what I managed to do so far.
The code I have is good to look for the rows that have active in them but it keeps empty rows in between the new table. Meaning, ID 1 and 4 are active but there is two rows non active and unknown so it wont copy them and I will have similar to the picture
Could someone help me to add a line so it does not leave an empty rows?

**Also I would like to add a line so it look for the ID in another table and copy the row and bring it to the new table.

**There are errors in the line (4 Stevens) so how can I make it report it back to me that there is error?

VBA Code:
Option Explicit

Sub Test()

Dim Cell As Range

With Sheets(1)
   ' loop column H untill last cell with value (not entire column)
   For Each Cell In .Range("G1:G" & .Cells(.Rows.Count, "H").End(xlUp).Row)
       If Cell.Value = "Active" Then
            ' Copy>>Paste in 1-line (no need to use Select)
           .Rows(Cell.Row).Copy Destination:=Sheets(4).Rows(Cell.Row)
           
       End If
   Next Cell
End With

End Sub
 

Attachments

  • Screenshot 2022-04-07 152813.png
    Screenshot 2022-04-07 152813.png
    13.3 KB · Views: 15
  • Screenshot 2022-04-08 132604.png
    Screenshot 2022-04-08 132604.png
    4.5 KB · Views: 15
  • Screenshot 2022-04-08 132826.png
    Screenshot 2022-04-08 132826.png
    6.7 KB · Views: 15
Last edited by a moderator:
file.xlsx
ABCDEFGHI
1Workbook1
2IDNameDOBNoDateLeftSTATUSPostCode
31Johns 15/12/1972AA00000115/12/1982ActiveAB1 1AA
42Alvis 06/04/1973AA00000206/04/1983InactiveAB1 1AA
53Paul 27/08/1977AA00000327/08/1987UnknowAB1 1AA
64Stevens 28/08/1980AA00000428/08/1990Active AB1 1AA
7
8Workbook2
9IDNONameTotalleft
101AA01Johns
112AA02Alvis
123AA03Paul
134#N/AStevens0.5424
14
15New Workbook
16IDNameDOBNoDateLeftSTATUSPostCodeTotalleft
171Johns 15/12/1972AA00000115/12/1982ActiveAB1 1AA
184Stevens 28/08/1980AA00000428/08/1990Active AB1 1AA0.5424
19
20Total
21
Sheet1
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Make sure that Workbook1 and Workbook2 are open. Open a new blank workbook and place this macro in a regular module in the new workbook. Also, insert the column headers in row 1 of Sheet1. The macro assumes that the sheet name in each of the 3 workbooks is "Sheet1". In the code change the worksheet names (in red) and the workbook names (in blue) to suit your needs
Rich (BB code):
Sub CreateTable()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet, desWS As Worksheet, fnd As Range, rName As Range
    Set ws1 = Workbooks("Workbook1.xlsx").Sheets("Sheet1")
    Set ws2 = Workbooks("Workbook2.xlsx").Sheets("Sheet1")
    Set desWS = Sheets("Sheet1")
    With ws1
        .Cells(1).CurrentRegion.AutoFilter 6, "Active"
        .AutoFilter.Range.Offset(1).Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1)
        For Each rName In desWS.Range("B2", desWS.Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
            Set fnd = ws2.Range("C:C").Find(rName, LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                rName.Offset(, 6) = fnd.Offset(, 1)
            End If
        Next rName
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Same resluts, but my version ;):

VBA Code:
Option Explicit
Sub forCompareDataOnTwoWorkbooks()

    Dim wbs1 As Workbook: Set wbs1 = Workbooks("mainData.xlsm")
    Dim wbs3 As Workbook: Set wbs3 = Workbooks("transferData.xlsm")
    
    Dim mws1 As Worksheet: Set mws1 = wbs1.Worksheets("Sheet1")
    Dim mws3 As Worksheet: Set mws3 = wbs3.Worksheets("Sheet1")
   
    Dim cell1 As Range, rng1 As Range, lastRow1 As Long
    Dim cell3 As Range, rng3 As Range, lastRow3 As Long
    
    lastRow1 = mws1.Cells(Rows.Count, "B").End(xlUp).Row
    lastRow3 = mws3.Cells(Rows.Count, "C").End(xlUp).Row
    
    Set rng1 = mws1.Range("B2:B" & lastRow1)
    Set rng3 = mws3.Range("C2:C" & lastRow3)

    For Each cell1 In rng1
        For Each cell3 In rng3
        With cell1
            If cell1.Value = cell3.Value Then
            mws1.Range("H" & cell1.Row).Value = mws3.Range("D" & cell3.Row).Value
            
            End If
        End With
        Next cell3
    Next cell1
End Sub
 

Attachments

  • forCompareDataOnTwoWorkbooks.jpg
    forCompareDataOnTwoWorkbooks.jpg
    94.6 KB · Views: 7
Upvote 0
Set ws1 = Workbooks("Workbook1.xlsx").Sheets("Sheet1")

I will borrow that format for future projects.
 
Upvote 0
Make sure that Workbook1 and Workbook2 are open. Open a new blank workbook and place this macro in a regular module in the new workbook. Also, insert the column headers in row 1 of Sheet1. The macro assumes that the sheet name in each of the 3 workbooks is "Sheet1". In the code change the worksheet names (in red) and the workbook names (in blue) to suit your needs
Rich (BB code):
Sub CreateTable()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet, desWS As Worksheet, fnd As Range, rName As Range
    Set ws1 = Workbooks("Workbook1.xlsx").Sheets("Sheet1")
    Set ws2 = Workbooks("Workbook2.xlsx").Sheets("Sheet1")
    Set desWS = Sheets("Sheet1")
    With ws1
        .Cells(1).CurrentRegion.AutoFilter 6, "Active"
        .AutoFilter.Range.Offset(1).Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1)
        For Each rName In desWS.Range("B2", desWS.Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
            Set fnd = ws2.Range("C:C").Find(rName, LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                rName.Offset(, 6) = fnd.Offset(, 1)
            End If
        Next rName
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
Thank you but it is not doing anything
 
Upvote 0
Set ws1 = Workbooks("Workbook1.xlsx").Sheets("Sheet1")

I will borrow that format for future projects.
Thank you very much but It should pick the rows based on Active status , I do not want all the rows.
Also, there are missing cells, so it should report there is missing cells.
Can you modify it please?
 
Upvote 0
I tested the macro on dummy workbooks using the data you posted and it worked exactly as you requested. Perhaps you could upload copies of your two files to a free site such as www.box.com or www.dropbox.com. Once you do that, mark each file for 'Sharing' and you will be given a link to each file that you can post here (de-sensitized if necessary).
 
Upvote 0
Ok, this will only pick Active.

VBA Code:
Option Explicit
Sub forCompareDataOnTwoWorkbooks()

    Dim wbs1 As Workbook: Set wbs1 = Workbooks("mainData.xlsm")
    Dim wbs3 As Workbook: Set wbs3 = Workbooks("transferData.xlsm")
    
    Dim mws1 As Worksheet: Set mws1 = wbs1.Worksheets("Sheet1")
    Dim mws3 As Worksheet: Set mws3 = wbs3.Worksheets("Sheet1")
   
    Dim cell1 As Range, rng1 As Range, lastRow1 As Long
    Dim cell3 As Range, rng3 As Range, lastRow3 As Long
    
    lastRow1 = mws1.Cells(Rows.Count, "B").End(xlUp).Row
    lastRow3 = mws3.Cells(Rows.Count, "C").End(xlUp).Row
    
    Set rng1 = mws1.Range("B2:B" & lastRow1)
    Set rng3 = mws3.Range("C2:C" & lastRow3)

    For Each cell1 In rng1
        For Each cell3 In rng3
        With cell1
            If cell1.Value = cell3.Value And LCase(mws1.Range("F" & cell1.Row).Value) = LCase("Active") Then
                
                mws1.Range("H" & cell1.Row).Value = mws3.Range("D" & cell3.Row).Value
                
            End If
        End With
        Next cell3
    Next cell1
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,795
Members
449,468
Latest member
AGreen17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top