Help!!! Find column and define with name

excel199

New member
Joined
Jun 1, 2015
Messages
1
Reaction score
0
Points
0
I have a problem with my code. First off I am new to coding. Researched different sites online and the code I wrote is the best I came up with.
What I am trying to do with my code is search for the column name in Row 1 of each column (A1, B1, C1, ect..). After it finds the first two or three characters of the name of the column. Select that column and define that column by the name I supplied. So I can use that new define name in a formula I created. The problem that I have is I have two column with similar names and it always define the one that I do not want. One column is (B1) "KbTVD" the one I dont want defined and (F1) "TVD" the one I need. Every time I run my code it selects and defines B1 and I need F1 to be defined instead. Also I need my code to search different row 1 columns just for "TVD" because it is not a guarantee that "TVD" will always be in F1.

Code:

Code:
Sub DATA()  

    Set fnd = Range("1:1").Find("on*")   
    ActiveWorkbook.Names.Add "ono", fnd.EntireColumn, True

    Set fnd = Range("1:1").Find("tvd")
    ActiveWorkbook.Names.Add "tvd", fnd.EntireColumn, True

End Sub

this code selects and defines B1

Code:
Sub DATA()  

    Set fnd = Range("1:1").Find("on*")   
    ActiveWorkbook.Names.Add "ono", fnd.EntireColumn, True
    
    Set fnd = Range("1:1").Find("tvd", , , , , , True) ' 
    ActiveWorkbook.Names.Add "tvd", fnd.EntireColumn, True


End Sub

I tried to rewrite it and I get a error

run-time error '91':

object variable or with block variable not set


Also do I need to Dim something in my code?
 
Maybe have a look at this page and note you can specify the find method to look for whole or part words.

Also note the third line in the remarks section about what unspecified settings get used.
 
Back
Top