Code to find column by name

sandy40

New Member
Joined
Mar 8, 2017
Messages
1
Hi,

I have a scenario but not able to start .. If we have a Column called as Salary in Excel and this Column for the First Time will be in A1 then next time can be in Z1 so I need to write a code to find the Column Name ' Salary' Can some one please help me with this
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Re: How to find a column in Excel by writing a Code - If there is a column called as Salary where in Salary Column can change i.e for the First Time the Salary Column Name will be in A1 then it will be in Z1 if that being the case how to find ColumnN

Maybe this

Code:
Sub MM1()
Dim c As Integer
For c = 1 To Columns.Count
    If Cells(1, c).Value = "Salary" Then
        Cells(1, c).EntireColumn.Select
    End If
Next c
End Sub
 
Upvote 0
Re: How to find a column in Excel by writing a Code - If there is a column called as Salary where in Salary Column can change i.e for the First Time the Salary Column Name will be in A1 then it will be in Z1 if that being the case how to find ColumnN

Greetings and welcome to the site Sandy :-)

Another way would be to use the .Find method.

Sheet1[TABLE="class: html-maker-worksheet"]
<thead>[TR]
[TH][/TH]
[TH]A[/TH]
[TH]B[/TH]
[TH]C[/TH]
[TH]D[/TH]
[TH]E[/TH]
[TH]F[/TH]
[TH]G[/TH]
[/TR]
</thead><tbody>[TR]
[TH]1[/TH]
[TD]juliette[/TD]
[TD]afroasiatic[/TD]
[TD]blackthorn[/TD]
[TD]synaeresis[/TD]
[TD]salary[/TD]
[TD]windcheater[/TD]
[TD]sacculation[/TD]
[/TR]
</tbody>[/TABLE]
Excel 2010

In a Standard Module:
Rich (BB code):
Option Explicit
  
Sub example()
Dim rngSalary As Range
  
  'Using the .Find method (which I happen to use a wrapper function for my preferred defaults), either
  'retun a Range object if found, or Nothing if not
  Set rngSalary = RangeFound(ThisWorkbook.Worksheets("Sheet1").Rows(1), "Salary", , , xlWhole, , xlNext)
  
  'Test to see if we found it and proceed.
  If Not rngSalary Is Nothing Then
    MsgBox "Found at " & rngSalary.Address
  Else
    MsgBox "Not found"
  End If


End Sub
  
Function RangeFound(SearchRange As Range, _
                    Optional ByVal FindWhat As String = "*", _
                    Optional StartingAfter As Range, _
                    Optional LookAtTextOrFormula As XlFindLookIn = xlValues, _
                    Optional LookAtWholeOrPart As XlLookAt = xlPart, _
                    Optional SearchRowCol As XlSearchOrder = xlByRows, _
                    Optional SearchUpDn As XlSearchDirection = xlPrevious, _
                    Optional bMatchCase As Boolean = False) As Range
    
    If StartingAfter Is Nothing Then
        Set StartingAfter = SearchRange.Cells(1)
    End If
    
    Set RangeFound = SearchRange.Find(What:=FindWhat, _
                                      After:=StartingAfter, _
                                      LookIn:=LookAtTextOrFormula, _
                                      LookAt:=LookAtWholeOrPart, _
                                      SearchOrder:=SearchRowCol, _
                                      SearchDirection:=SearchUpDn, _
                                      MatchCase:=bMatchCase)
End Function
 
Upvote 0

Forum statistics

Threads
1,223,427
Messages
6,172,039
Members
452,444
Latest member
ShaImran193

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