Get each selected row number in separated variables

yaninja

New Member
Joined
Sep 7, 2017
Messages
6
Good morning all,

I wish you could help me on this matter.

I am looking for getting all selected row number in different variables through VBA.

The selected rows may be separated from each other by using #control button#

For example If i select 1:1 - 5:5 -9:9
I want to get the result like
Var(1) = 1
Var(2)= 5
Var(3)= 9

Is there any easy way to code such a macro?

Thank you very much in advance for your help!

Yaninja
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Code:
Sub getRows()
Dim rA As Range, sTmp As String, iX As Long, varRows As Variant
    If TypeOf Selection Is Excel.Range Then
        sTmp = ","
            For Each rA In Selection.Areas
                For iX = rA.Cells(1, 1).Row To rA.Cells(rA.Rows.Count, 1).Row
                    If InStr(1, sTmp, "," & iX & ",") = 0 Then sTmp = sTmp & iX & ","
                Next iX
            Next rA
        [B]varRows = Split(Mid$(Left$(sTmp, Len(sTmp) - 1), 2), ",")[/B]
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top