Check if Columns match array, report or insert missing columns

Gingertrees

Well-known Member
Joined
Sep 21, 2009
Messages
697
How would I check to see if columns on my sheet match a set array of column names?

Array :
Code:
("Concatenate", "Report Item No", "Area Code", "Phone Start", "Phone End", "Name")

So my sheet ideally looks like this:
[TABLE="class: grid, width: 700"]
<tbody>[TR]
[TD]Concatenate[/TD]
[TD]Report Item No[/TD]
[TD]Area Code[/TD]
[TD]Phone Start[/TD]
[TD]Phone End[/TD]
[TD]Name[/TD]
[TD]ID23[/TD]
[TD]ID33[/TD]
[TD]ID42[/TD]
[/TR]
[TR]
[TD]5615551212BD[/TD]
[TD]1[/TD]
[TD]561[/TD]
[TD]555[/TD]
[TD]1212[/TD]
[TD]BDoe[/TD]
[TD]x[/TD]
[TD]e[/TD]
[TD]y[/TD]
[/TR]
[TR]
[TD]6041238888AT[/TD]
[TD]2[/TD]
[TD]604[/TD]
[TD]123[/TD]
[TD]8888[/TD]
[TD]ATann[/TD]
[TD]r[/TD]
[TD]e[/TD]
[TD]r[/TD]
[/TR]
</tbody>[/TABLE]

I'd like info on which columns are missing. Any chance I could add code to insert missing columns at the right places??

Thanks.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hello,

have hard-coded the requirements. Which will also insert the columns and its heading

Code:
Sub CHECK_COLUMNS()
        If UCase(Cells(1, 1).Value) <> "CONCATENATE" Then
            Columns(1).Insert
            Cells(1, 1).Value = "Concatenate"
        End If
        If UCase(Cells(1, 2).Value) <> "REPORT ITEM NO" Then
            Columns(2).Insert
            Cells(1, 2).Value = "Report Item No"
        End If
        If UCase(Cells(1, 3).Value) <> "AREA CODE" Then
            Columns(3).Insert
            Cells(1, 3).Value = "Area Code"
        End If
        If UCase(Cells(1, 4).Value) <> "PHONE START" Then
            Columns(4).Insert
            Cells(1, 4).Value = "Phone Start"
        End If
        If UCase(Cells(1, 5).Value) <> "PHONE END" Then
            Columns(5).Insert
            Cells(1, 5).Value = "Phone End"
        End If
        If UCase(Cells(1, 6).Value) <> "NAME" Then
            Columns(6).Insert
            Cells(1, 6).Value = "Name"
        End If
End Sub

Does this work as required?
 
Upvote 0
That does work - I am a little concerned that it will go slow when I increase the number of columns, though.
Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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