macro needed!

chaju

Board Regular
Joined
Mar 24, 2002
Messages
82
Thanks to those who take a look at this and help!

Suppose the file consists of 2 worksheets

For the both sheets:
Cell A1 have date: 25-Mar-02

Can I have a Macro button so that it inputs (when pressed) this formula into Cell C1 (BOTH Sheets): =C:\test\[Valuation 25-Mar-02.xls]Ingenium'!$E$11

Note: the date within the file name corresponds to the date in Cell A1 for both sheet.

Suppose both sheet's Column A has already been filled with date. The next step I wish the macro to do is whenever I press the button it will fill the formula mention above into C2 (for both sheets). I must ensure Cell C2 formula (in both sheets)have the file name with the date correspond to their sheet's Cell A2's date.

when I press the button it will do the same to cell C3 with both formula reading the dates in cell A3 (for both sheets)

As I am very new to macro Please asist step by step of how to create a macro button and where to put the VB code...

Many Many Many thanks to the helper,,,
Deeply appreciated
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi chaju!

'Please copy both of codes into a standard module.

Sub InputsFormula()
Dim sh(0 To 1) As Worksheet, cnt As Integer, lngRow As Long

Set sh(0) = Sheets("Sheet1") 'change to a sheet name to input Formula
Set sh(1) = Sheets("Sheet2") 'same as above

lngRow = Selection.Row

For cnt = 0 To 1
With sh(cnt)
.Cells(lngRow, 3).Formula = _
"'=C:\\test\\[Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$11"
End With
Next
End Sub

'Please active the sheet with which you want to make a button,
'and execute "Sub MakeBottonOnActiveSheet".

Sub MakeBottonOnActiveSheet()
Dim rngBt As Range, objBt As Button
Set rngBt = Application.InputBox _
("Select range where you want to make Button", Type:=8)
With rngBt
Set objBt = ActiveSheet.Buttons.Add(.Left, .Top, .Width, .Height)
End With
With objBt
.OnAction = "InputsFormula"
.Text = "Click"
End With
End Sub
 
Upvote 0
Please reduce the number of "/" in the formula.
It seems that the character became strange Because, my Excel is a Japanese version.
Regards,
 
Upvote 0
Thanks very much! but I find a mistake in my content..

In the 4th paragraph I have:
"Can I have a Macro button so that it inputs (when pressed) this formula into Cell C1 (BOTH Sheets): =C:\\test\\[Valuation 25-Mar-02.xls]Ingenium'!$E$11 "

it should be:
"Can I have a Macro button so that it inputs (when pressed) this formula into Cell C1 (1st Sheet): =C:test[Valuation 25-Mar-02.xls]Ingenium'!$E$11, and input this formula into Cell C1 (2nd Sheet): =C:test[Valuation 25-Mar-02.xls]Ingenium'!$E$4"

Sorry about that,, But I guess I should can the code much..

So, I look forward to see the amended code..

Many thanks
 
Upvote 0
Hello again, the amended code is following...


Sub InputsFormula()
Dim lngRow As Long
lngRow = Selection.Row
With Sheets("Sheet1")
.Cells(lngRow, 3).Formula = _
"=C://test//[Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$11"
End With
With Sheets("Sheet2")
.Cells(lngRow, 3).Formula = _
"=C://test//[Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$4"
End With
End Sub
 
Upvote 0
The characters "//" in above code mean "".
Thanks,
 
Upvote 0
Thanks I have the code now.. But it doesn't work ... I think is the sheet name problem.

Suppose My 1st Sheet have name MonMkt
Is it right that the code should have:

With Sheets("MonMkt") instead of the suggested:
With Sheets("Sheet1")
That's what I have,, but not working.. pls advise.. sorry to bother again.
 
Upvote 0
Hi.
Does any error message come out?
I think, how to write sheet name is no problem as you wrote.
You should select "Row" before click button.
If you want to formula into Cell C1 then please select any cells in row1.
Do I make myself clear? Sorry, I am not an English native.
Regards,
 
Upvote 0
Thanks Colo,, I have modified the code you gave me, now my sheets have 10sheets, only 8 sheets are needed to perform action with that button. You can see the 8 sheets name below. After editing the code, I press F5 (in VB) (F5 means Run Sub/UserForm) and error message pop up say: "RUN time error '9': Subscript out of range"

Can you pls check my modified code and help me please........ please, we are nearly there!! Code as follow:

Sub InputsFormula()
Dim lngRow As Long
lngRow = Selection.Row
With Sheets("MonMkt")
.Cells(lngRow, 5).Formula = _
"='I:VULYEAR 20022nd QtrValuation[Fortune Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$4"
End With
With Sheets("GEqty")
.Cells(lngRow, 5).Formula = _
"='I:VULYEAR 20022nd QtrValuation[Fortune Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$11"
End With
With Sheets("GBond")
.Cells(lngRow, 5).Formula = _
"='I:VULYEAR 20022nd QtrValuation[Fortune Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$6"
End With
With Sheets("USEqty")
.Cells(lngRow, 5).Formula = _
"='I:VULYEAR 20022nd QtrValuation[Fortune Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$9"
End With
With Sheets("USBond")
.Cells(lngRow, 5).Formula = _
"='I:VULYEAR 20022nd QtrValuation[Fortune Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$5"
End With
With Sheets("EuEqty")
.Cells(lngRow, 5).Formula = _
"='I:VULYEAR 20022nd QtrValuation[Fortune Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$10"
End With
With Sheets("AsianEqty")
.Cells(lngRow, 5).Formula = _
"='I:VULYEAR 20022nd QtrValuation[Fortune Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$8"
End With
With Sheets("HKEqty")
.Cells(lngRow, 5).Formula = _
"='I:VULYEAR 20022nd QtrValuation[Fortune Valuation " & .Cells(lngRow, 1).Text & ".xls]Ingenium'!$E$7"
End With
End Sub



Sub MakeBottonOnActiveSheet()
Dim rngBt As Range, objBt As Button
Set rngBt = Application.InputBox _
("Select range where you want to make Button", Type:=8)
With rngBt
Set objBt = ActiveSheet.Buttons.Add(.Left, .Top, .Width, .Height)
End With
With objBt
.OnAction = "InputsFormula"
.Text = "Click"
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,556
Messages
6,114,284
Members
448,562
Latest member
Flashbond

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