VBA solution for finding missing data

warren66

New Member
Joined
Nov 16, 2010
Messages
4
Is there a VBAsolution to the following scenario?
I have two columns of numbers, in A & E and i would like them levelling up where values are present on one but not the other, by inserting a cell in either column. When complete, the cells in Cols A & E should the the same, or present in one but not the other.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Can you please show us some sample data, and your expected results?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Before the macro runs, typical data could be:

test.xlsx
ABCDE
111
2264
364309
4100321
5309575
6321577
7575609
8577739
96091000
107392164
1121642182
1221822276
1322762286
1422862303
1523032439
1624392441
1724412455
1824552456
1924562501
2025002533
2125332534
2225342537
2325372549
2425492557
2525572559
2625592561
2725613000
Sheet3



After the macro has run, it should look like this:
test.xlsx
ABCDE
111
22
36464
4100
5309309
6321321
7575575
8577577
9609609
10739739
111000
1221642164
1321822182
1422762276
1522862286
1623032303
1724392439
1824412441
1924552455
2024562456
212500
222501
2325332533
2425342534
2525372537
2625492549
2725572557
2825592559
2925612561
303000
Sheet4
 
Upvote 0
OK, this code seems to do what you want (I verified it returns the desired results you posted):
VBA Code:
Sub FixData()

    Dim r As Long
   
    Application.ScreenUpdating = False
   
'   Set initial value to row 1
    r = 1
   
'   Loop until for boths A and E are blank
    Do

'       Exit if both columns A and E are blank
        If Cells(r, "A") = "" And Cells(r, "E") = "" Then Exit Sub

'       Check to make sure both columns have a value
        If Cells(r, "A") <> "" And Cells(r, "E") <> "" Then
'           Compare columns A and E and insert row where necessary
            If Cells(r, "A") > Cells(r, "E") Then
'               Insert cell in column A
                Cells(r, "A").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            Else
                If Cells(r, "A") < Cells(r, "E") Then
'                   Insert cell in column E
                    Cells(r, "E").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                End If
            End If
        End If
   
'       Incrementer row counter
        r = r + 1

    Loop
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Solution
You are welcome. Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,225,757
Messages
6,186,845
Members
453,379
Latest member
gabriellegonzalez

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