User Macro causing problem when entire worksheet is selected (all cells)

anthonya2369

Active Member
Joined
Mar 10, 2005
Messages
321
Ok, I am struggling with a solution to this. I write a lot of code in VBA. I have a current macro that rounds the cells selected by the user by the number of digits selected. I also limited it to the cells that have values that were numeric. Everything runs fine if you select just the range you definitely need. However, some users decided that they want to select the entire worksheet to run the code which makes Excel run through all cells and locks up. Here is my code:

Code:
Sub AddRounding(ByVal dblNumber As Double)

    Dim iCell   As Range
    Dim nCount  As Integer

    For Each iCell In Selection
        If IsEmpty(iCell) = False Then
            If IsNumeric(iCell.Value) Then
                If Left(iCell.Formula, 1) = "=" Then
                    iCell.Formula = "=Round(" & Right(iCell.Formula, Len(iCell.Formula) - 1) & "," & dblNumber & ")"
                Else
                    iCell.Formula = "=Round(" & iCell.Formula & "," & dblNumber & ")"
                End If
            Else
                nCount = nCount + 1
            End If
        End If
    Next iCell

    If nCount > 0 Then
        MsgBox nCount & " cell(s) were skipped due to the cell value not being numeric.", vbCritical
    End If

End Sub

What I need to do is, if the user has the entire worksheet selected (all cells even those not being used), I do not allow the code to run. How can I determine this or is there a standard to prevent a cell by cell macro from running when the entire worksheet is selected? Any input is greatly appreciated.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Have you thought of using the UsedRange property??
Code:
For Each iCell in ActiveSheet.UsedRange

lenze
 
Upvote 0
Maybe stick the following code at the start...

If Selection.Count> 100000 Then 'choose appropriate number based on size of used range
Msgbox "Please just select the relevant cells"
Exit Sub
End If
 
Upvote 0
Selection.Count will tell you how many cells have been selected. You could wrap you code with an if statement that doesn't let it run if more than 'X' cells are selected. Of course you can vary the value of 'X'.
 
Upvote 0
Thanks for all the replies. Learned something new today! I used the code provided by VoG since it utilizes the usedrange within the selection only. If I chose just the usedrange without the intersect, i believe it would have done all cells and not the ones within the selection.

Code:
For Each icell In Intersect(ActiveSheet.UsedRange, Selection)

Another reason I chose this instead of the selection.count item, is that I didn't have to have it stop the macro when it got to a predetermined number, etc. This makes it more user friendly.

Again, thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,278
Members
452,902
Latest member
Knuddeluff

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