Add formula to dynamic range

Edgarvelez

Board Regular
Joined
Jun 6, 2019
Messages
197
Office Version
  1. 2016
Platform
  1. Windows
Hi all, never done this before with formulas and need some help.

I have 2 sheets Sh1. & Sh2.
In Sh2. I have a dynamic range in H and I need to apply a formula to the values
The formula is the value in each cell multiplied by 2204.6244202 (*2204.6244202)
I am using case and if in Sh1. D10 from the dropdown is YES then I want to add the formula if the current value of the cell x 2204.6244202

I started some of the code already but have never done this adding a formula to a range.

VBA Code:
    Case "Metric Ton|GW|Single Line|Single Line Weight"
    Select Case sh1.Range("D10").Value
    Case "No"
    sh2.Cells(Rows.Count, 8).End(xlUp)(2).Resize(sh1.Range("D30").Value).NumberFormat = "0.000"
    sh1.Range("D26").Copy
    sh2.Cells(Rows.Count, 8).End(xlUp)(2).Resize(sh1.Range("D30").Value).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    
    Case "Yes"




    sh1.Range("A1").Select
    sh1.Range("D30:D33").Select
    Selection.ClearContents
    sh1.Range("A1").Select
 

Attachments

  • Sheet 2A.JPG
    Sheet 2A.JPG
    80.9 KB · Views: 16
  • Sheet 2B.JPG
    Sheet 2B.JPG
    79.9 KB · Views: 15

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
VBA Code:
Case "Yes"
for each cell in Range("H2:H" & cells(rows.count,"H").end(xlup).row)
cell.value = cell.value * 2204.6244202
next cell
 
Upvote 0
VBA Code:
Case "Yes"
for each cell in Range("H2:H" & cells(rows.count,"H").end(xlup).row)
cell.value = cell.value * 2204.6244202
next cell
Hi,

Added the code as follows:
VBA Code:
    Case "Metric Ton|GW|Single Line|Single Line Weight"


    sh2.Cells(Rows.Count, 8).End(xlUp)(2).Resize(sh1.Range("D30").Value).NumberFormat = "0.000"
    sh1.Range("D26").Copy
    sh2.Cells(Rows.Count, 8).End(xlUp)(2).Resize(sh1.Range("D30").Value).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    
    Select Case sh1.Range("D10").Value
    Case "Yes"
    For Each cell In Range("H2:H" & Cells(Rows.Count, "H").End(xlUp).Row)
    cell.Value = cell.Value * 2204.6244202
    Next cell
    End Select
    sh1.Range("A1").Select
    sh1.Range("D30:D33").Select
    Selection.ClearContents
    sh1.Range("A1").Select

No errors but it did not change the values. Do you think we are missing Sh2. somewhere in the code?
I think we are close though.
 

Attachments

  • Sheet 2.JPG
    Sheet 2.JPG
    67.5 KB · Views: 8
Upvote 0
I just noticed that it works but the results are in sh1. and need this to happen in sh2.
Any help anybody.
 
Upvote 0
How about
VBA Code:
Case "Yes"
   With sh2.Range("H2", sh2.Range("H" & Rows.Count).End(xlUp))
      .Value = sh2.Evaluate("if(" & .Address & "="""",""""," & .Address & "*2204.62442)")
   End With
End Select
 
Upvote 0
Fluff,
VBA Code:
    sh2.Cells(Rows.Count, 8).End(xlUp)(2).Resize(sh1.Range("D30").Value).NumberFormat = "0.000"
Can I add * 2204.62442 to the end of this line and make this line like this
VBA Code:
    sh2.Cells(Rows.Count, 8).End(xlUp)(2).Resize(sh1.Range("D30").Value).* 2204.62442
 
Upvote 0
Ok I get it, here's the problem.
I keep adding to the range as I keep using the macro and this line gets me to the exact part of the range.
Your code worked to a point but let me show you the results.
 
Upvote 0
What is the value in D30, also did you try the code I suggested?
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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