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:
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.
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.