VBA: Headers missing not match

vbanewbie68

Board Regular
Joined
Oct 16, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Dear Sir or Madam

Please see the screenshot below.

I created a new workbook as a macro. The 1st row has the correct headers as previously agreed with the developer.

When I capture the data from the report that is linked to a platform connected to a website, I expected the data with the headers to match the 1st row.

However when I exported the data from the platform to the macro workbook I have noticed it does not match, as the 2nd headers are missing the Postcode.

The question is how do I insert a new column on the 2nd row to flag up to display a blank, showing there is data missing or that does not match.

Hope this makes sense?

Thank you

V

1643885205744.png
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
This code will compare row 2 to row 1, and if the data isn't the same in a column, it shifts the data right for the unmatched data. However, it is simple code. There is no room for "close" data. For example, in your data, the result might not be what you expect because, besides missing PostCode, the second row doesn't match "addr 4" that I can tell. In this case, the code will shift addr and the rest of the data completely to column R (the end of row 1). Additional code would need to be added to try to account for these differences where one column is off, but the rest of the data is okay.

VBA Code:
Sub CompareHeaders()
    Dim i As Integer
    i = 1
    While Cells(1, i).Value <> ""
        If Cells(2, i).Value <> Cells(1, i).Value Then
            Cells(2, i).Insert xlToRight
        End If
        i = i + 1
    Wend
End Sub
 
Upvote 0
Solution
This code will compare row 2 to row 1, and if the data isn't the same in a column, it shifts the data right for the unmatched data. However, it is simple code. There is no room for "close" data. For example, in your data, the result might not be what you expect because, besides missing PostCode, the second row doesn't match "addr 4" that I can tell. In this case, the code will shift addr and the rest of the data completely to column R (the end of row 1). Additional code would need to be added to try to account for these differences where one column is off, but the rest of the data is okay.

VBA Code:
Sub CompareHeaders()
    Dim i As Integer
    i = 1
    While Cells(1, i).Value <> ""
        If Cells(2, i).Value <> Cells(1, i).Value Then
            Cells(2, i).Insert xlToRight
        End If
        i = i + 1
    Wend
End Sub

Thank you for this. I am pleased it worked at my end. Thank you so much of your time on this matter. Much appreciated. :)

Best regards

V
 
Upvote 0

Forum statistics

Threads
1,215,650
Messages
6,126,014
Members
449,280
Latest member
Miahr

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