VBA by column name and not column number reference

gripper

Board Regular
Joined
Oct 29, 2002
Messages
143
Hi,

I have a long sub that goes column by column and does various formatting. In the below section of code it conditionally formats a column based on the column number. Unfortunate when the raw data is imported into the sheet it will sometimes not line up correctly but the column names will be consistent. So I would like to migrate away from a hard location and instead dynamically find the column name and do the formatting based on that.

In the below code the column name will always be "Long".

Thank you in advance for your assistance.


Code:
Columns("AM:AM").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=251"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
 

Some videos you may like

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,578
Office Version
  1. 365
Platform
  1. Windows
How about
Code:
Sub gripper()
   Dim Fnd As Range
   
   Set Fnd = Range("1:1").Find("long", , , xlWhole, , , False, , False)
   If Fnd Is Nothing Then Exit Sub
   With Fnd.EntireColumn
      .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=251"
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      With .FormatConditions(1).Font
         .Color = -16383844
         .TintAndShade = 0
      End With
      With .FormatConditions(1).Interior
         .PatternColorIndex = xlAutomatic
         .Color = 13551615
         .TintAndShade = 0
      End With
      .FormatConditions(1).StopIfTrue = False
   End With
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,109,423
Messages
5,528,681
Members
409,829
Latest member
CFreeamaz

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top