VBA - rounding amounts (multiple cols) AND 'ignore text'

dmj120

Active Member
Joined
Jan 5, 2010
Messages
308
Office Version
  1. 365
  2. 2019
  3. 2010
Sooo.....this is a follow-on to a previous post. I'm trying to modify the below to:
  • Round across H4:O, instead of Range("I2:I6000")
  • Ignore any errors - the below give a #value error if text is entered, which is fine (in that workbook, lol)
I tried to simply change the range to Set rng = Intersect(Target, Range("H4:O")) which errors out.

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ErrTrap

    Dim rng As Range

    Set rng = Intersect(Target, Range("I2:I6000"))

    If Not rng Is Nothing Then

        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        If Len(Target) > 0 Then
            Target.Value = Evaluate("ROUND(" & Target.Address & "/365,2)*365")
        End If



        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With

    End If

Exit Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Round across H4:O, instead of Range("I2:I6000")
I2:I6000 is a range of cells. H4:O is not. You need an ending row number to make it a range, e.g. H4:O5000
 
Upvote 0
Changing to Set rng = Intersect(Target, Range("H4:O70001")) gives a compile error.

1686106122944.png



I2:I6000 is a range of cells. H4:O is not. You need an ending row number to make it a range, e.g. H4:O5000
 
Upvote 0
Changing to Set rng = Intersect(Target, Range("H4:O70001")) gives a compile error.

View attachment 93068
The change had nothing to do with it. It would have given you that error if you had left the original range in place. Each subroutine must end with the statement End Sub and yours does not.
 
Upvote 0
I think this is enough

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

'Do nothing if range does not change OR text value OR cell is deleted
If Intersect(Target, Range("H2:O6000")) Is Nothing Or _
    Not IsNumeric(Target) Or IsEmpty(Target) Then Exit Sub

With Application
    .EnableEvents = False
    Target.Value = Evaluate("ROUND(" & Target.Address & "/365,2)*365")
    .EnableEvents = True
End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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