Macro to select a column

Last Edited By Krjb Donovan
Last Updated: Mar 05, 2014 09:46 PM GMT

QuestionEdit

Hello

I am using a spreadsheet which contains headings in row 5.

What I am looking for is a macro which will go through row 5 and when it finds the words "Total" then it will select that column.

AnswerEdit

Sub SelectTotalcolumn() Dim r As Range, cell As Range

With ActiveSheet

 Set r = .Range(.Cells(1, 5), .Cells(5, .Columns.Count).End(xlToLeft))

End With For Each cell In r

 If InStr(1, cell, "total", vbTextCompare) > 0 Then
     cell.EntireColumn.Select
     Exit For
 End If

Next

End Sub

there are more efficient ways, but based on what you have described it seemed fairly robust. It assumes that only one cell in row 5 contains the substring "total"

Anyway, it was tested on the activesheet and it worked for me.

Advertisement

©2024 eLuminary LLC. All rights reserved.