Modify VBA code

dmj120

Active Member
Joined
Jan 5, 2010
Messages
286
Office Version
  1. 365
  2. 2019
  3. 2010
I'm trying to modify code that bebo021999 helped with here.

The original code works perfectly, but now I'm trying to modify it so I can assign it to a 'button' (ie excel shape) that will look at all cells in the range and update any number to the correct rounding.

I've tried a few variations, and here's where I'm at (the original code after).
VBA Code:
Private Sub Rnd()
    Dim Target As Range
    Set Target = ThisWorkbook.Sheets("MS.combined").Range("H6:H25000")
    
    Application.EnableEvents = False
    
    For Each Cell In Target
        If IsNumeric(Cell.Value) And Not IsEmpty(Cell.Value) Then
            Cell.Value = Evaluate("ROUND(" & Cell.Address & "/365,2)*365")
        End If
    Next Cell
    
    Application.EnableEvents = True
End Sub

Original code:
VBA Code:
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("I2:I6000")) 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
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi
Try
VBA Code:
Private Sub Rnd()
    Dim Target_ As Range
    Dim cel
    Set Target_ = ThisWorkbook.Sheets("MS.combined").Range("H6:H" & Cells(Rows.Count, "H").End(xlUp).Row)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    For Each cel In Target_
        If IsNumeric(cel) And cel.Value <> "" Then cel.Value = Evaluate("ROUND(" & cel.Address & "/365,2)*365")
    Next
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
After pasting this into module 1, when I try to assign the macro the popup window is blank.

What am I doing wrong?

Hi
Try
VBA Code:
Private Sub Rnd()
    Dim Target_ As Range
    Dim cel
    Set Target_ = ThisWorkbook.Sheets("MS.combined").Range("H6:H" & Cells(Rows.Count, "H").End(xlUp).Row)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    For Each cel In Target_
        If IsNumeric(cel) And cel.Value <> "" Then cel.Value = Evaluate("ROUND(" & cel.Address & "/365,2)*365")
    Next
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Absolutely right MARKS858
VBA Code:
Sub Rnd()
    Dim Target_ As Range
    Dim cel
    Set Target_ = ThisWorkbook.Sheets("MS.combined").Range("H6:H" & Cells(Rows.Count, "H").End(xlUp).Row)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    For Each cel In Target_
        If IsNumeric(cel) And cel.Value <> "" Then cel.Value = Evaluate("ROUND(" & cel.Address & "/365,2)*365")
    Next
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Works perfectly, thanks!! Time to read up on why removing private works.

Absolutely right MARKS858
VBA Code:
Sub Rnd()
    Dim Target_ As Range
    Dim cel
    Set Target_ = ThisWorkbook.Sheets("MS.combined").Range("H6:H" & Cells(Rows.Count, "H").End(xlUp).Row)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    For Each cel In Target_
        If IsNumeric(cel) And cel.Value <> "" Then cel.Value = Evaluate("ROUND(" & cel.Address & "/365,2)*365")
    Next
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,111
Messages
6,123,159
Members
449,098
Latest member
Doanvanhieu

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