change formula using macro

tintin1012000

Board Regular
Joined
Apr 27, 2011
Messages
237
='\\IEMAFS001\VOL1\APM\Prodqty\Electronic shift book\8882\Line 1\[wk26.xls]Graphs'!$A$46

Hi All,

I have a formula above and I want to change the week number using a macro, How do I go about this. Ideally i want to type 25 into a cell and then run a macro that will change [wk26.xls] to [wk25.xls]
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
='\\IEMAFS001\VOL1\APM\Prodqty\Electronic shift book\8882\Line 1\[wk26.xls]Graphs'!$A$46

Hi All,

I have a formula above and I want to change the week number using a macro, How do I go about this. Ideally i want to type 25 into a cell and then run a macro that will change [wk26.xls] to [wk25.xls]

Code:
sub macro()
 
dim sWK as string
 
sWK = SHeets("Sheet1").range("A1").Value
 
range("a2").formula = [URL="file://\\IEMAFS001\VOL1\APM\Prodqty\Electronic shift book\8882\Line 1\"]\\IEMAFS001\VOL1\APM\Prodqty\Electronic shift book\8882\Line 1\[/URL][wk" & sWK & ".xls]Graphs'!$A$46"

then you can use further code to fill other cells with this formula
 
Upvote 0
If you have 34 in B1, this will change A1 from ...[wkxx.xls]... to ..[xk34.xls]...

Code:
Sub test()
    Dim strFormula As String
    Dim cutPoint As Long
    
    strFormula = Range("A1").FormulaR1C1
    cutPoint = InStr(1, strFormula, "[wk", vbTextCompare) + 2
    
    Range("A1").FormulaR1C1 = Left(strFormula, cutPoint) & Format(Range("B1"), "00") & Mid(strFormula, cutPoint + 3)
End Sub
 
Upvote 0
Thanks mikerickson,
Your formula worked, how do I extend it to multiple cells

I want it to go from b6 to E19, I tried below but it did not work

Thanks again


strFormula = Range("B6:E19").FormulaR1C1
</pre>
 
Upvote 0
Code:
Sub test2()
    Dim oneCell as Range
    Dim strFormula As String
    Dim cutPoint As Long
    
    For Each oneCell in Range("B6:E19"(
        With oneCell
            strFormula = .FormulaR1C1
            cutPoint = InStr(1, strFormula, "[wk", vbTextCompare) + 2
    
            .FormulaR1C1 = Left(strFormula, cutPoint) & Format(Range("B1"), "00") & Mid(strFormula, cutPoint + 3)
        With oneCell
    Next oneCell
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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