Consulting

Results 1 to 5 of 5

Thread: Solved: How do I create a range based on a column name?

  1. #1
    VBAX Regular
    Joined
    Apr 2009
    Posts
    15
    Location

    Solved: How do I create a range based on a column name?

    Hi there,

    I am very very new to VBA but am loving what it can do, albeit very basic stuff I am doing at the moment.

    I do a lot of importing into excel from another programme(Primavera P6).
    The fields from the imports vary depending on the fileds open on the layout, so do their column positions. The first row of data holds the column headings, There are two date coulmns that I am interested in they are always called "start" and "Finish", I need to do a some tidying up to get them turned into proper date fields as some come in as text strings, and others dates. ( I have sub routing to do this bit)

    What I am looking at doing in VB is looking to the right to find the last column, then work backwards to find Start & finish and select them as a range before I run my routine to tidy them up. Can anyone help my with some code to find "Start", and select all the data below as a range, then the same for "finish"

    Thanks in advance for your help

    Rob

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Off the top, untested

    [vba]

    mpStartCol = Application.Match("Start", Rows(1), 0)
    mpEndCol = Application.Match("Finish", Rows(1), 0)

    Set rng = Columns(mpStartCol).Resize(, mpEndCol - mpStartCol + 1)
    [/vba]
    Last edited by Bob Phillips; 04-19-2009 at 02:45 PM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I'm reading the question differenttly so
    [VBA]
    Sub SetFormat()
    Dim c As Range
    Dim MyRange As Range
    Set c = Rows(1).Find("Start")
    Set MyRange = Range(c.Offset(1), Cells(Rows.Count, c.Column).End(xlUp))
    MyRange.NumberFormat = "dd/mm/yyyy"
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    Apr 2009
    Posts
    15
    Location
    Brilliant, thanks for the quick reply guys, I will have a little play with both options.

  5. #5
    VBAX Regular
    Joined
    Apr 2009
    Posts
    15
    Location
    okay guys, here is my final answer & it works thanks for your help, I am sure it is not the nicest looking code you have ever seen.
    [VBA]Sub FormatP6dates()

    Dim c As Range
    Dim d As Range
    Dim MySRange As Range
    Dim MyFRange As Range
    Set c = Rows(1).Find("Start")
    Set d = Rows(1).Find("Finish")

    Set MySRange = Range(c.Offset(1), Cells(Rows.Count, c.Column).End(xlUp))
    MySRange.Select

    Set MyFRange = Range(d.Offset(1), Cells(Rows.Count, d.Column).End(xlUp))
    MyFRange.Select

    Selection.Replace What:=" A", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

    'This is Part 2 of the code, its does the text to columns conversion to sort out the dates.

    Selection.TextToColumns Destination:=MySRange, DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 4), TrailingMinusNumbers:=True

    Selection.TextToColumns Destination:=MyFRange, DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 4), TrailingMinusNumbers:=True

    End Sub[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •