Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a worbook name 'student' it has 1 to 60 sheets. I have selected cell No. B2 of all sheets. i want to change formula in B2 cells of all 1-60 sheets. The formula in sheet1 cell B2 is =[sub.xlsx]Sheet1!$B$2

in sheet3 the formula should change =[sub.xlsx]Sheet1!$B$3

in sheet4 cell B2 the formula should change =[sub.xlsx]Sheet1!$B$4
in sheet5 cell B2 the formula should change =[sub.xlsx]Sheet1!$B$5
in sheet6 cell B2 the formula should change =[sub.xlsx]Sheet1!$B$6

do this how many sheets in this workbook
its like as link cells i.e press = then enter , but its a bore work.

a code frovided for this but it was not worked
So plese provide a code or formula once again

Very Very thanks in Advance
Posted
Updated 21-Mar-13 3:16am
v2
Comments
balkha7 19-Mar-13 10:52am    
please i am waiting very eagerly
joshrduncan2012 19-Mar-13 11:00am    
What have you tried? Where is your code? Where are you stuck?

You were provided a solution to your earlier post[^] - If you had used the 'Have a Question or Comment' button Maciej Los could have helped you directly.

As it is, the code he provided probably fails at this point ...
VB
'if workbook is already open
Set dstWbk = Application.Workbooks("Book2.xls")


If you replace "Book2.xls" with "student" - without the .xls bit then it should work
 
Share this answer
 
v2
Comments
Maciej Los 19-Mar-13 12:18pm    
Good point, my 5!
balkha7 23-Mar-13 5:49am    
I have a worbook name 'student' it has 1 to 60 sheets. I have selected cell No. B2 of all sheets. i want to change formula in B2 cells of all 1-60 sheets. The formula in sheet1 cell B2 is =[sub.xlsx]Sheet1!$B$2

in sheet3 the formula should change =[sub.xlsx]Sheet1!$B$3

in sheet4 cell B2 the formula should change =[sub.xlsx]Sheet1!$B$4
in sheet5 cell B2 the formula should change =[sub.xlsx]Sheet1!$B$5
in sheet6 cell B2 the formula should change =[sub.xlsx]Sheet1!$B$6

do this how many sheets in this workbook
its like as link cells i.e press = then enter , but its a bore work.
balkha7 20-Mar-13 4:32am    
sir, i replace it but invain
Now i have once aggain posted a que
CHill60 20-Mar-13 5:37am    
When you say in vain ... what is the error?
balkha7 21-Mar-13 9:27am    
please sir provide again a Formula or code.
it should done auto when i open new sheet or insert sheet
Ok based on all the comments across all the posts here is an adaptation of Maciej Los' solution on your other post... this could be run from a button on the main sheet
VB
Sub AddLinks()
    Dim srcWbk As Workbook, dstWbk As Workbook
    Dim srcWsh As Worksheet, dstWsh As Worksheet
    Dim i As Integer, j As Integer
    Dim s As String

    On Error GoTo Err_AddLinks

    Set srcWbk = ThisWorkbook
    Set srcWsh = srcWbk.Worksheets("Sheet1")
    Set dstWbk = Application.Workbooks("student")

    For i = 1 To dstWbk.Worksheets.Count
        Set dstWsh = dstWbk.Worksheets(i)
        If dstWsh.Name <> "Sheet1" Then
            s = "=Sheet1!B" + CStr(i + 2) 'This strange formula based on OP notes
            dstWsh.Range("B2").Formula = s
        End If
    Next

Exit_AddLinks:
    On Error Resume Next
    Set srcWbk = Nothing
    Set srcWsh = Nothing
    Set dstWbk = Nothing
    Set dstWsh = Nothing
    Exit Sub

Err_AddLinks:
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_AddLinks

End Sub


But you also mentioned (somwhere) that you would like this to run automatically everytime you add a new sheet to the workbook in which case something like this added to the ThisWorkbook code page would work
VB
Private Sub Workbook_NewSheet(ByVal Sh As Object)

    On Error GoTo Err_NewSheet

    'Note that Worksheets.Count has already been incremented for the new sheet
    s = "Sheet1!B" + CStr(ThisWorkbook.Worksheets.Count + 2)
    Sh.Range("B2").Formula = "=" & s
    Exit Sub

Err_NewSheet:
    MsgBox Err.Description, vbExclamation, Err.Number
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900