VBA Workbook Syntax

hayden

Board Regular
Joined
Sep 23, 2005
Messages
188
I'm trying to write a macro to create a transpose formula for me.

The transpose formula references another workbook, and worksheet and I can't seem to get the syntax right. Here is my code so far.
VBA Code:
Sub buildformulas()
Dim wbname As String
Dim monthname As String
Dim colletter As String
Dim path1 As String
Dim wb2 As Workbook
Dim wb1 As Workbook
Dim ws2 As Worksheet
'Loop for Formulas
Set wb1 = ActiveWorkbook
path1 = "R:\1200\1255\CapacitySurvey_2020\Acct\"
i = 11
wbname = Sheets("Macroinput").Cells(i, 1).Value
Set wb2 = Workbooks.Open(Filename:=path1 & wbname)

For r = 11 To 57
wb1.Activate
monthname = Sheets("Macroinput").Cells(r, 2).Value
colletter = Sheets("Macroinput").Cells(r, 3).Value
wb2.Activate
Set ws2 = wb2.Worksheets(monthname)
wb1.Activate
Cells(r, 11).Formula = "=Transpose('[" & wb2 & "]" & ws2 & "'!$" & colletter & "$21:$" & colletter & "$100)"
Next
End Sub

The problem line is this one:
VBA Code:
Cells(r, 11).Formula = "=Transpose('[" & wb2 & "]" & ws2 & "'!$" & colletter & "$21:$" & colletter & "$100)"

If I just enter it by hand I get this formula:
Excel Formula:
=TRANSPOSE('[Myron Stratton Spring Run Res YE Oct2016.xlsx]December'!$L$21:$L$100)

What am I missing?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Maybe your variables aren't returning what you think they are.

What happens when you return your built-in formula in a Message Box like this?
VBA Code:
MsgBox "=Transpose('[" & wb2 & "]" & ws2 & "'!$" & colletter & "$21:$" & colletter & "$100)"

Does it return a string that looks EXACTLY like the formula at the bottom of your initial post?
 
Upvote 0
I get this error for ws2.

1712681334761.png
 
Upvote 0
Did you copy and paste what I have EXACTLY in the previous cell?
You should not get that message, unless you made a typo or have an error elsewhere.
 
Upvote 0
Please copy/paste your code here, exactly as you currently have it, using the Code Tags.
 
Upvote 0
Joe. I just wrote an entirely new loop to loop through the rows in the other sheet and transpose those via two nested loops.

Thanks for trying to help. I'll mark this as resolved.

Mods' you could delete this entire thing if you'd like.
 
Upvote 0
So you have it working the way you need now?
If so, you can just post your new code, and then mark that post as the solution.

Thanks
 
Upvote 0
VBA Code:
Sub buildformulas()
Dim wbname As String
Dim monthname As String
Dim colletter As String
Dim path1 As String
Dim wb2 As Workbook
Dim wb1 As Workbook
Dim ws2 As Worksheet
'Loop for Formulas
Set wb1 = ActiveWorkbook
path1 = "R:\1200\1255\CapacitySurvey_2020\Acct\"
i = 11
wbname = Sheets("Macroinput").Cells(i, 1).Value
Set wb2 = Workbooks.Open(Filename:=path1 & wbname)
For r = 6 To 58
wb1.Activate
monthname = Sheets("Macroinput").Cells(r, 2).Value
colletter = Sheets("Macroinput").Cells(r, 3).Value
Application.ScreenUpdating = False
For c = 11 To 67

colref = Cells(5, c).Value
Cells(r, c).Formula = "='[" & wbname & "]" & monthname & "'!$" & colletter & "$" & colref
Next
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,814
Messages
6,181,128
Members
453,021
Latest member
Justyna P

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