Link to home
Start Free TrialLog in
Avatar of Jeffrey Smith
Jeffrey SmithFlag for United States of America

asked on

Need VBA Formula to add reference to cells in new worksheet

Hi Experts,

I'm struggling with trying to add a VBA Formula that refers to designated cells on a new worksheet that was added via code earlier in the procedure.  My existing code (below) is returning (on a separate "Summary" worksheet) literal strings like "= strNewWS3 & D4" instead of translating same to the newly added (& renamed) sheet name, followed by an "!" and the referenced Cell value in the new sheet, and then returning the value in the referenced Cell.

Sub add_Sheet()
    
    Dim strNewWS, strNewWS2 As String, strNewWS3 As String
    Dim iLastRow As Long
    Dim i As Integer
    Dim myCheck As Boolean
    
   On Error GoTo add_Sheet_Error

    strNewWS = ActiveSheet.Cells(3, 4).Value
    strNewWS2 = strNewWS
    i = 2
    Do
        myCheck = False
        For Each wsh In Worksheets
            If wsh.Name = strNewWS2 Then
                strNewWS2 = strNewWS & " (" & i & ")"
                myCheck = True
            End If
        Next wsh
        i = i + 1
    Loop Until myCheck = False
    strNewWS = strNewWS2
    
    Sheets("Blank").Copy After:=Sheets(Sheets.Count)
     
    Sheets(Sheets.Count).Name = strNewWS
    
    strNewWS3 = strNewWS & "!"
    
    With Sheets("Summary")
    
    iLastRow = .Range("A" & Rows.Count).End(xlUp).Row
    
    .Range("A" & iLastRow + 1).Formula = "= strNewWS3 & D4"
    .Range("B" & iLastRow + 1).Formula = "= strNewWS3 & D10"
    .Range("C" & iLastRow + 1).Formula = "= strNewWS3 & Q17"
    .Range("D" & iLastRow + 1).Formula = "= strNewWS3 & Q25"
    .Range("E" & iLastRow + 1).Formula = "= strNewWS3 & Q28"
    
    End With
    
    SortWorksheets

   On Error GoTo 0
   Exit Sub

add_Sheet_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure add_Sheet of Module Module1"
    
End Sub

Open in new window


I appreciate any insights.

Jeff
ASKER CERTIFIED SOLUTION
Avatar of nutsch
nutsch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeffrey Smith

ASKER

Thanks, Thomas!

Just what I needed.
Hope you are still watching this, Thomas.  While your suggestion works the first time through, when I add a second employee, the code throws an "Run-time error '1004': Application-defined or object-defined error" on your line of code:

.Range("A" & iLastRow + 1).Formula = "= " & strNewWS3 & "D4"

Can you have a look?

Jeff
Since I'm not sure if nutsch might still be monitoring this closed topic and since I'm hoping for an answer sooner than later, I'm opening a new topic on this follow-up here:

https://www.experts-exchange.com/questions/27592199/Run-time-error-'1004'-Application-defined-or-object-defined-error-on-2nd-running-of-macro.html

"Run-time error '1004': Application-defined or object-defined error" on 2nd running of macro"

Jeff