Assigning a variable to a column, based on header

David2

New Member
Joined
Jan 13, 2018
Messages
39
I'm searching for a macro that would go through headers (A1, B1, C1 ... P1), search for a text ("AAA" and "BBB") and then assign a variable to the column that contains the text (Set columnA=ColumnContaningTextAAAinHeaders and columnB=ColumnContaningTextBBBinHeaders)


I'm guessing I would first have to set a range for the headers, then loop through that and then assign a variable.

Any ideas?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Do you want to find the column number of the columns containing "AAA" and "BBB" and assign that number to a variable?
 
Upvote 0
Do you want to find the column number of the columns containing "AAA" and "BBB" and assign that number to a variable?

Aha, that works too. Just a clarification - some columns will contain AAA (their variable will be columnA) and some BBB (their variable will be columnB) but never both.
 
Last edited:
Upvote 0
Is there more than one occurrence of AAA and BBB? If so, how do you want to name each variable?
 
Upvote 0
Try:
Code:
Sub David2()
    Dim columnA As Long, columnB As Long
    columnA = Rows(1).Find("AAA").Column
    columnB = Rows(1).Find("BBB").Column
End Sub
 
Upvote 0
Seems to be working great.

Two more things - what error handling should I have if AAA (or BBB) is not found? Currently I get "variable not set"?
And how can I delete all other columns except columnA and columnB?
 
Upvote 0
Try:
Code:
Sub David2()
    Application.ScreenUpdating = False
    Dim lCol As Long, x As Long, columnA As Long, columnB As Long, AAA As Range, BBB As Range
    lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Set AAA = Rows(1).Find("AAA", LookIn:=xlValues, lookat:=xlWhole)
    If Not AAA Is Nothing Then
        columnA = AAA.Column
    Else
        MsgBox ("AAA not found.")
    End If
    Set BBB = Rows(1).Find("bbb", LookIn:=xlValues, lookat:=xlWhole)
    If Not BBB Is Nothing Then
        columnB = BBB.Column
    Else
        MsgBox ("BBB not found.")
    End If
    For x = lCol To 1 Step -1
        If x <> columnA And x <> columnB Then
            Columns(x).Delete
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
One thing that I still don't understand - how do I reference the columns?

For instance how do I write this
Code:
Range("A1", Range("B" & Rows.Count).End(xlUp).Address).Sort Key1:=Range("A2")
using those variables?

Code:
Range((ColumnA & "1"), Range(ColumnB & Rows.Count).End(xlUp).Address).Sort Key1:=Range(ColumnA  & "2")
Doesn't seem to work -"Object variable or With block variable not set" error
 
Last edited:
Upvote 0
"ColumnA" and "ColumnB" variables are the column numbers. "Range" does not accept column numbers to define a range. You have to use column letters. I think that it would be easier to help and test possible solutions if I could work with your actual file which includes any macros you are currently using. Perhaps you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Include a detailed explanation of what you would like to do using a few examples from your data and referring to specific cells, rows, columns and worksheets. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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