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: 14
  • 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: 14
Last edited by a moderator:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
It is hard to work with pictures. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
Hi, post some actually data and I will look at it. Or if you find all my post, I may have done something similar that you can build your code from. Good luck.
 
Upvote 0
Workbook 1 has:

ID Name DOB STATUS
1 Johns 15/12/1972 Active
2 Alvis 06/04/1973 Inactive
3 Paul 27/08/1977 Unknow
4 Stevens 28/08/1980 Active

Workbook 2 has:
ID NO Name Totalleft
1 AA01 Johns
2 AA02 Alvis
3 AA03 Paul
4 #N/A Stevens 0.5424


I managed to do the first part, were it find the active cells. However, when it paste the rows it will look like

ID Name DOB STATUS
1 Johns 15/12/1972 Active


4 Stevens 28/08/1980 Active

Instead of

ID Name DOB STATUS
1 Johns 15/12/1972 Active
4 Stevens 28/08/1980 Active

I managed to remove the empty rows but it is a seperate macro, not within the same code using

Sub DeleteBlankRows()
On Error Resume Next
Range("A3:A1000000").Select
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub
 
Upvote 0
The new merged table should look like

1 Johns 15/12/1972 Active
4 Stevens 28/08/1980 Active 0.5424

and because there is an empty cell, the macro should be able to report it
Could you please help me?
 
Upvote 0
The new merged table should look like

1 Johns 15/12/1972 Active
4 Stevens 28/08/1980 Active 0.5424

and because there is an empty cell, the macro should be able to report it
Could you please help me?


As I just want the total left from the second workbook, and the total of it?
 
Upvote 0
Please follow the instructions in Post #2.
 
Upvote 0
Book1
ABCD
1IDNameDOBSTATUS
21Johns15/12/1972Active
32Alvis6/4/1973Inactive
43Paul27/08/1977Unknow
54Stevens28/08/1980Active
6
7
8
9IDNONameTotalleft
101AA01Johns
112AA02Alvis
123AA03Paul
134#N/AStevens0.5424
Sheet1


Cool, I see how it works now.
 
Upvote 0
Unfortunately, that didn't work out well. Select all the data in the sheet and then in the XL2BB add-in select "mini sheet".
 
Upvote 0
It is hard to work with pictures. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0

Forum statistics

Threads
1,215,347
Messages
6,124,421
Members
449,157
Latest member
mytux

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