• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 114
  • Last Modified:

Abstract this sub

I recorded this. It specifically finds the PM column header.
Then sets the column width.

Sub Set_Col_Wid_PM()
    Range("A1").Select
    Cells.Find(What:="PM", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=True, SearchFormat:=False).Activate
    Columns("E:E").Select
    Selection.ColumnWidth = 5.14
End Sub

Open in new window


I'd like to abstract this so I can pass the column name and desired width. e.g.

= Set_Col_Wid("PM", 5.14)

Open in new window


Is there a way to 'generalize' Columns("E:E").Select so that it knows what column to select, regardless of its location?

Can you help?
0
NVIT
Asked:
NVIT
  • 4
  • 2
1 Solution
 
Martin LissRetired ProgrammerCommented:
Cells.Find(What:="PM").Activate
ActiveCell.EntireColumn.ColumnWidth = 5.14
0
 
Martin LissRetired ProgrammerCommented:
Continuing my post, if there's any possibility of "PM" appearing anyplace else then row 1.

With Rows(1)
    .Cells.Find(What:="PM").Activate
    ActiveCell.EntireColumn.ColumnWidth = 5.14
End With

Open in new window

0
 
NVITAuthor Commented:
That works, Martin.
How to provide for if search failed?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Martin LissRetired ProgrammerCommented:
Sub SetIt()
With Rows(1)
    On Error GoTo ErrorRoutine
    .Cells.Find(What:="PM").Activate
    ActiveCell.EntireColumn.ColumnWidth = 5.14
End With
Exit Sub
ErrorRoutine:
    Dim strCol As String
    strCol = InputBox("Please enter the column letter")
    Columns(strCol).ColumnWidth = 5.14
End Sub

Open in new window

0
 
NVITAuthor Commented:
Awesome! Thank you! Have a nice day/night...
With a few changes, I figured out the abstraction part...

[code]Sub Col_Wid(strFind, nColWidth)
    On Error GoTo ErrNotFound:
    Range("A1").Select
    Cells.Find(What:=strFind).Activate
    ActiveCell.EntireColumn.ColumnWidth = nColWidth
Exit Sub

ErrNotFound:
MsgBox "Col_Wid. Column " & strFind & " not found"

End Sub
[/code]
...so I can use it like:

[code]
Sub SetColWidths()
    Col_Wid "PN", 9.86
    Col_Wid "Project Title", 45
End Sub
[/code]
0
 
Martin LissRetired ProgrammerCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now