0
votes

I am new to vba and I have vba code for activecell with offset codition. The following code selects few cells based on offset condtion. I want to get selected same number of cells if more than one cell or multiple cells are selected. please help me. Thanks in advance

ActiveCell.Offset(, 0).Resize(1, 2).Select

If i select cell b1, then on running the code it selects b1 and c1. I want solution for multiple random cells if selected in column b, the corresponding value should be selected.

1
I want to get selected same number of cells if more than one cell or multiple cells are selected. - clarify please, some example would be helpful. Btw, ActiveCell.Offset(, 0) the same as ActiveCell. - Dmitry Pavliv
HI simoco, thanks for your response. - user3340121
HI simoco, thanks for your response.If select a1 and run the code it will select a1 and b1. If I select a1 and a3 and a7 then cells a1b1 and a3b3 and a7b7 should be selected. - user3340121
what should be if you select, say, A1:B2 and C1:D2? - Dmitry Pavliv
Actually, I want to make selections only in column B. The selections should be made only in column B. It may be multiple cells likd b1 or b2 or b7. If these cells are selected the corresponding rows have to be selected on running code. - user3340121

1 Answers

0
votes

If select a1 and run the code it will select a1 and b1. If I select a1 and a3 and a7 then cells a1b1 and a3b3 and a7b7 should be selected.

UPDATED:

Follow up from comments:

Sub test1()
    Dim rng As Range
    Dim ar As Range
    Dim rngAE As Range, cell As Range

    With Selection
        For Each ar In .Areas
            If rng Is Nothing Then
                Set rng = ar.Resize(1, 2)
            Else
                Set rng = Union(rng, ar.Resize(1, 2))
            End If
        Next ar

        Set rngAE = Intersect(.Cells, Range("A:E"))
        If Not rngAE Is Nothing Then
            For Each cell In rngAE
                Set rng = Union(rng, Range("A" & cell.Row & ":E" & cell.Row))
            Next cell                
        End If
    End With

    rng.Select
End Sub