Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Maco question

Hello

I have a worksheet with several columns. In my macro, I have written code to
identify column number for each heading. e.g. If there is a column with
heading "City", then I have written cl_city=Application.Match("City",
Rows(1),0).
There are several such columns. However, I want to notify user is any of the
column is not existing. So if "City" column is not existing, then macro
should throw a message to user.
How can I do this. I have around 50 columns.

Any help is highly appreciated.

Thank You

Regards,
prm
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Maco question


Dim cl_City

cl_City = Application.Match("City", Rows(1), 0)
If IsError(cl_City) Then
MsgBox "City does not exist as a header"
End If

--
Regards,

OssieMac


"Pawan" wrote:

Hello

I have a worksheet with several columns. In my macro, I have written code to
identify column number for each heading. e.g. If there is a column with
heading "City", then I have written cl_city=Application.Match("City",
Rows(1),0).
There are several such columns. However, I want to notify user is any of the
column is not existing. So if "City" column is not existing, then macro
should throw a message to user.
How can I do this. I have around 50 columns.

Any help is highly appreciated.

Thank You

Regards,
prm

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Maco question

Thanks OssieMac,

But then I need to write this for all 50 headers. Is there any other short
way to do this?


"OssieMac" wrote:


Dim cl_City

cl_City = Application.Match("City", Rows(1), 0)
If IsError(cl_City) Then
MsgBox "City does not exist as a header"
End If

--
Regards,

OssieMac


"Pawan" wrote:

Hello

I have a worksheet with several columns. In my macro, I have written code to
identify column number for each heading. e.g. If there is a column with
heading "City", then I have written cl_city=Application.Match("City",
Rows(1),0).
There are several such columns. However, I want to notify user is any of the
column is not existing. So if "City" column is not existing, then macro
should throw a message to user.
How can I do this. I have around 50 columns.

Any help is highly appreciated.

Thank You

Regards,
prm

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Maco question

Somewhere you will need a list of the 50 headers. Is this code in a separate
workbook to the ones you are testing? If so, you could have the list on a
worksheet within the workbook containing the code and I can then give you
code to loop through the list and test for all.

Can you give me a worksheet name and range where the list can be permanently
stored.

--
Regards,

OssieMac


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Maco question

The code is in the same workbook. Few of the headers are "City", "Address",
"Code1", "Code2", "Post". If you can give me code for these, then I will add
remaining headers in it.

Thank you once again. :)

"OssieMac" wrote:

Somewhere you will need a list of the 50 headers. Is this code in a separate
workbook to the ones you are testing? If so, you could have the list on a
worksheet within the workbook containing the code and I can then give you
code to loop through the list and test for all.

Can you give me a worksheet name and range where the list can be permanently
stored.

--
Regards,

OssieMac




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Maco question

Try the following.

Create or re-name a worksheet in the workbook and call it "Headers".

In cell A1 of the Headers worksheet insert "Valid Headers" (or any name you
like as a column header for the validation list).

In cell A2, A3, A4 etc enter the names of your headers. Note you are going
down the column; not across the worksheet. You can have as many or few as you
like and can add and remove them later because the code tests for the last
cell containing data.

You may choose to hide the headers worksheet if you like.

Copy the following code into the VBA editor. Note the comments.

Note that a space and underscore at the end of a line is a line break in an
otherwise single line of code.


Sub ValidateHeaders()

Dim wsHeaders As Worksheet
Dim rngHeaders As Range
Dim cel As Range
Dim cl_City As Variant 'Must be Variant

'Assign headers worksheet to a variable
Set wsHeaders = ThisWorkbook.Sheets("Headers")

With wsHeaders
'Test to ensure that at least one entry
'in the valid headers list
If IsEmpty(.Range("A2")) Then
MsgBox "No headers entered in validation list"
Exit Sub
End If

'Assign range of valid headers to a variable
Set rngHeaders = .Range(.Cells(2, "A"), _
.Cells(.Rows.Count, "A").End(xlUp))
End With

'Edit "Sheet1" to match the sheet you are testing
With Sheets("Sheet1")
'Loop through the valid headers
For Each cel In rngHeaders
cl_City = Application.Match(cel.Value, .Rows(1), 0)
If IsError(cl_City) Then
MsgBox cel.Value & " does not exist as a header"
'Exit Sub 'If you want to exit and insert header
End If
Next cel
End With

End Sub


--
Regards,

OssieMac

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Maco code doesn't work. Why? Joe M. Excel Discussion (Misc queries) 2 January 13th 10 07:13 PM
Maco to copy data from wks to wks Tammy H Excel Programming 1 July 11th 07 11:04 PM
A simple maco . . . Steve New Users to Excel 3 January 2nd 05 10:36 PM
A simple maco for someone, but not for me. Bill Wilson[_3_] Excel Programming 2 September 21st 04 03:45 PM
closing form w/ Maco jeffP Excel Programming 4 June 19th 04 10:29 PM


All times are GMT +1. The time now is 07:46 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"