Results 1 to 3 of 3

Thread: [RESOLVED] How to modify Selection range in Excel

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Resolved [RESOLVED] How to modify Selection range in Excel

    In Excel if a user selects multiple non-contiguous columns like "B" and "D", this code

    Code:
       
    Dim rng As Range
    Dim r As Range
    Dim rngCol As Range
    
    For Each rngCol In Selection.Columns
            Set r = Intersect(Selection, rngCol)
            If r.Address = rngCol.EntireColumn.Address Then
                If rng Is Nothing Then
                    Set rng = r
                Else
                    Set rng = Union(rng, r)
                End If
            End If
        Next
    creates rng with the address of $B:$B,$D:$D. How can I adjust that code so that the address produced is $B2:$B2,$D2:$D2 or $B2,$D2 ?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to modify Selection range in Excel

    specifically row 2?

    Code:
    For Each rngcol In Selection.Columns
            Set r = Intersect(Selection, rngcol)
            If r.Address = rngcol.EntireColumn.Address Then
                If rng Is Nothing Then
                    Set rng = Cells(2, r.Column)
                Else
                    Set rng = Union(rng, Cells(2, r.Column))
                End If
            End If
        Next
    note in this case r is always = to rngcol, so isn't really needed

    ?rng.address
    $A$2,$D$2
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

Posting Permissions

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



Click Here to Expand Forum to Full Width