How to determine if the user has selected more than one column?

JR1412

Board Regular
Joined
Jan 29, 2005
Messages
62
I would like to ensure that the user has only selected cells in one column before launching the macro shown below. Is there an easy way to determine if the range is more than one column?

Code:
    Dim strCell As String
    Dim c As Range
    Dim rng As Range
    '
    Set rng = Selection.Cells
    For Each c In rng
        strCell = c.Value
        Call RegExp(strCell)
        c.Value = strCell
    Next c
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Maybe inserting this as the first code-line

Code:
If Selection.Columns.Count > 1 Then Exit Sub

HTH

M.
 
Upvote 0
You could add suggest the range to the user (of the first column of the active selection only) as a default, which gives them the chance to accept the default, change the range or exit the code

You may also want to consider using a variant array to run what appears to be a regular expression sub over the range

Cheers

Dave

Code:
Sub UserPrompt()
    Dim rng1 As Range
    On Error Resume Next
    Set rng1 = Application.InputBox("Please select range to work on", , Intersect(Selection, Selection.Columns(1)).Address, , , , , 8)
    On Error GoTo 0
    If rng1 Is Nothing Then Exit Sub
    For Each c In rng1
        strCell = c.Value
        Call RegExp(strCell)
        c.Value = strCell
    Next c
    MsgBox "Done"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

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