Consulting

Results 1 to 2 of 2

Thread: Range References in VBA when Wks Rows/Columns Deleted

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Range References in VBA when Wks Rows/Columns Deleted

    I fear I know what the answer to this question is going to be but I am inclined to ask anyway. When rows and columns are deleted, references in worksheet formulas are automatically adjusted accordingly. However, that does not seem to be the case in VBA. I am curious as to whether there is a way to define ranges in VBA to avoid range references becoming obsolete when the structure of a worksheet is changed? Case in point:

    [VBA]
    Dim col As Integer: For col = 1 To gCols

    Select Case col
    Case 1, 2, 9, 10, 19, 20, 21, 22
    Case Else
    Columns(col).EntireColumn.Hidden = True
    End Select
    Next col

    [/VBA]

    If Column 19 is deleted, only is the above case statement effected but all other functions and subroutines that reference Columns(19) are effected.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use defined names for the ranges, and use those names in VBA.
    ____________________________________________
    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

Posting Permissions

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