0

I would like to reference each sheet in this formula by specifying the sheet name with the variable, "sheetname". Does anyone know how to do this?

Sub PopulateRow()

Dim WS_Count As Integer
Dim I As Integer
Dim sheetname As String

WS_Count = ActiveWorkbook.Worksheets.Count

For I = 1 To WS_Count

    Worksheets(I).Activate
        sheetname = ActiveSheet.Name
    Worksheets(1).Cells(I, 1).Formula = "=sum('sheetname'!d:d)"

Next I

End Sub

1 Answer 1

6

You would pull the variable outside the quotes and concatenate with &.

Sub PopulateRow()

Dim WS_Count As Integer
Dim I As Integer


WS_Count = ActiveWorkbook.Worksheets.Count

For I = 1 To WS_Count

    Worksheets(1).Cells(I, 1).Formula = "=sum('" & Worksheets(I).Name & "'!d:d)"

Next I

End Sub

As a note I removed the sheet.Activate. It will slow down the code and is not needed if done properly.

Sign up to request clarification or add additional context in comments.

1 Comment

@Topher please mark as correct by clicking the check mark by the answer. It is something only you can do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.