Consulting

Results 1 to 3 of 3

Thread: Column counting and Loop

  1. #1
    VBAX Newbie
    Joined
    Jun 2010
    Posts
    3
    Location

    Column counting and Loop

    I am trying to define my range dependant on the number of filled cells in each column below is current code

    [VBA]
    Private Sub Rangedefiner()
    'Redefines dynamic range
    Application.DisplayAlerts = False 'Turn off warnings
    Range("A2", Range("A65536").End(xlUp)).Select
    Selection.CreateNames Top:=True, Left:=False, Bottom:=False, Right:=False
    Application.DisplayAlerts = True 'Turn on warnings
    End Sub
    [/VBA]


    I need to move along a column at a time recreating my new ranges but I am struggling to find the code to move column by column.

    Tried using cells(2,1) but found I was keeping the previous column in my selection so obviously doing it wrong.

    Heeelp

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Rangedefiner()
    Dim col As Long
    Dim i As Long

    Application.DisplayAlerts = False 'Turn off warnings

    For i = 1 To 13 'A-M change to suit

    'Redefines dynamic range
    Range(Cells(2, i), Cells(Rows.Count, i).End(xlUp))CreateNames Top:=True
    Next i

    Application.DisplayAlerts = True 'Turn on warnings
    End Sub
    [/vba]
    Last edited by Bob Phillips; 06-29-2010 at 03:41 AM.
    ____________________________________________
    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
    VBAX Newbie
    Joined
    Jun 2010
    Posts
    3
    Location
    Brilliant thanks modified code slightly. just for consistency.

    [VBA]Private Sub Rangedefiner()
    Dim col As Long
    Dim i As Long

    Application.DisplayAlerts = False 'Turn off warnings

    For i = 1 To 13 'A-M change to suit

    'Redefines dynamic range
    Range(Cells(2, i), Cells(Rows.Count, i).End(xlUp)).CreateNames Top:=True
    Next i

    Application.DisplayAlerts = True 'Turn on warnings
    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
  •