If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Find a column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-10-05, 12:48
Joe1 Joe1 is offline
Registered User
 
Join Date: Nov 2004
Posts: 77
Find a column

The first row includes the headings of the columns.

I have a macro and would like to add a function, that will check the heading columns if there is a heading called MyColumn, if yes, ignore, if not, add a column D and add
the heading MyColumn
Is this possible?

Thank you

Joe
Reply With Quote
  #2 (permalink)  
Old 11-10-05, 15:58
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Howdy.

yes, it is possible. Are you wanting to insert a new column that would be Column D and everything to the right will be pushed one column to the right.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
  #3 (permalink)  
Old 11-10-05, 17:02
Joe1 Joe1 is offline
Registered User
 
Join Date: Nov 2004
Posts: 77
yes, you got it.

please help
Thanks
Reply With Quote
  #4 (permalink)  
Old 12-15-05, 16:58
Sam Landy Sam Landy is offline
Registered User
 
Join Date: May 2004
Location: New York State
Posts: 931
Quote:
Originally Posted by Joe1
The first row includes the headings of the columns.

I have a macro and would like to add a function, that will check the heading columns if there is a heading called MyColumn, if yes, ignore, if not, add a column D and add
the heading MyColumn
Is this possible?

Thank you

Joe
Why not just record a new macro and incorporate the resultant code into your existing macro.

HTH,
Sam
Reply With Quote
  #5 (permalink)  
Old 12-15-05, 22:33
savbill savbill is offline
Registered User
 
Join Date: Feb 2004
Posts: 533
I'd use a loop to check the cell values in your header row. You could record some parts of this like the column insert, but most of this you will need to write code or copy from a good example and modify to meet your requirements. I'm giving 2 examples one checking the column names within a sub procedure and the other example is using a Function to return true/false based on the results. For simplicity used the number 20 to check 1-20 columns or loop until a match is found. You'd need to set a higher number if you have more columns or check the number of used cells and set a variable for the columns used.
Code:
' Example checking column names
' used integer 20 to check through column 20

Sub checkcol()
 Dim numCol As Integer
 Dim strName As String
 Dim bExists As Boolean
 Dim i As Integer

numCol = 20
strName = "MyCol"
bExists = False

For i = 1 To numCol
  colName = ActiveSheet.Cells(1, i)
  If UCase(strName) = UCase(colName) Then
    bExists = True
    Exit For
  End If
Next

If Not bExists Then
    Columns(4).Insert
    With Cells(1, 4)
      .Value = strName
      .Font.Bold = True
    End With
End If

End Sub


' Here is a function you can call from a sub proc.
' Then take action based on true or false return
' bColExists = fColumnExists("MyCol")

Function fColumnExists(strName) As Boolean
  Dim numCol As Integer
  Dim bExists As Boolean
  Dim i As Integer

  numCol = 20
 
  For i = 1 To numCol
    colName = ActiveSheet.Cells(1, i)
    If UCase(strName) = UCase(colName) Then
        fColumnExists = True
      Exit For
    End If
  Next
End Function
__________________
~

Bill
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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