Help!!! Find column and define with name

excel199

New Member
Joined
Jun 1, 2015
Messages
3
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?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
abc1abc1abc
456
abc5
formula giving 5
=OFFSET(I38,1,MATCH(H43,J38:L38,0))

<colgroup><col width="64" span="9" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
I do not understand this code I believe that it did not come out correct on your end. can you resend or anyone else can you please help?
 
Upvote 0
offset means go down some then along some

so you start from (my example) I38, you go down 1, how far along is match(text in H43 with range J38:L38

you are looking for a column header, these are unique, so if one is apple and one ia apple1 if you match for apple you will find apple
 
Upvote 0
offset means go down some then along some

so you start from (my example) I38, you go down 1, how far along is match(text in H43 with range J38:L38

you are looking for a column header, these are unique, so if one is apple and one ia apple1 if you match for apple you will find apple

ok I understand the offset function, but what I am not understanding is the range you picked. The string data will only be in row 1 of every column, and the data that I receive will not always be in the same column when I open it in different spreadsheets. And I will not know the length of the columns for each spreadsheet. I was trying to write a code that will specifically look for "TVD" only in row 1 of each column. and skip the string "KBTVD" until it sees the "TVD".
 
Upvote 0
so the offset will be from $A$1, down=up to you, along = match("TVD",$B$1:$CZ$1,0)

so today tvd is in column C tomorrow in column E you will find it ok wherever it is
 
Upvote 0

Forum statistics

Threads
1,203,727
Messages
6,056,983
Members
444,901
Latest member
Teal

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