Excel VBA to tell if a column has moved?

Phil Payne

Board Regular
Joined
May 17, 2013
Messages
131
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have a worksheet (.xls) which is provided by an external group from their database.
When we run a macro to update our worksheet it opens the supplied worksheet and takes the data from its column "H".

I need to provide a warning to our group members (msgbox) that column H has been moved or omitted the instant our macro has been launched and then end the macro safely.

Points to note:

  • Column H heading is "Cumulative to date" but the heading may be in any row of that column H.
  • We have no way of influencing how the output from the database is produced.

Can anyone tell me the vba code that can determine if Column H has been moved or omitted?

Thanks in advance.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Code:
Sub FindHeader()
Dim cfound As Range
Set cfound = Nothing


       Set cfound = Columns("H:H").Find(What:="Cumulative to date", After:=Range("H1"), LookIn:=xlValues, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False)
        If cfound Is Nothing Then MsgBox "Column H has moved": Exit Sub
        
        


End Sub
 
Upvote 0
Maybe something like this...

Code:
    [color=darkblue]If[/color] IsError(Application.Match("Cumulative to date", ActiveSheet.Range("H:H"), 0)) [color=darkblue]Then[/color]
        MsgBox "'Cumulative to date' is not in column column H. ", vbExclamation, "Process Aborted"
        [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
 
Upvote 0
Thank you Comfy, this worked well.

I'd mark this as solved but cant see how!

Regards,

Code:
Sub FindHeader()
Dim cfound As Range
Set cfound = Nothing


       Set cfound = Columns("H:H").Find(What:="Cumulative to date", After:=Range("H1"), LookIn:=xlValues, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False)
        If cfound Is Nothing Then MsgBox "Column H has moved": Exit Sub
        
        


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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