Add a dynamic range for code

Edgarvelez

Board Regular
Joined
Jun 6, 2019
Messages
197
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I have this code, works great for what I need but I need it to work for a dynamic range staring in B7.


VBA Code:
    Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
    Set sh1 = Sheets("DashBoard")
    Set sh2 = Sheets("Sheet1")
    Set sh3 = Sheets("BOL")

    sh1.Range("B7").Select

    
Dim rng As Range
Dim formulaCell As Range
Set rng = Selection
For Each formulaCell In rng
If formulaCell.HasFormula Then
formulaCell.Formula = formulaCell.Value
End If
Next formulaCell

End Sub

1653797541905.png
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Maybe this way
VBA Code:
Sub MM1()
 Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
    Set sh1 = Sheets("DashBoard")
    Set sh2 = Sheets("Sheet1")
    Set sh3 = Sheets("BOL")
With sh1.Range("B7:B" & Cells(Rows.Count, "B").End(xlUp).Row)
    .Value = .Value
End With
End Sub
 
Upvote 0
Solution
Your way worked great
.. but it will only reliably work great if 'DashBoard' is the active sheet.

To be sure it works great no matter which sheet is active, then it needs more. One way would be

Rich (BB code):
With sh1.Range("B7:B" & sh1.Cells(Rows.Count, "B").End(xlUp).Row)
    .Value = .Value
End With

If DashBoard will certainly be the active sheet when the code runs (which looks like it may be the case from your original code) then you don't need any mention at all of sh1 in that part of the code.

VBA Code:
With Range("B7", Range("B" & Rows.Count).End(xlUp))
    .Value = .Value
End With
 
Upvote 0
Hello Peter,
Good point, I will check into this right away and make the changes if the case.
As always, thank you for your help also.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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