Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA - transpose formulas in vertical array to horizontal array

Tags:

excel

vba

I have googled all sorts of phrases for an answer to my question but I'm having a hard time locating a solution that works. It likely involves combination of a few different solutions, or a method I have yet to think of; so any help would be appreciated.

Say I have formulas in cells A1, A2, A3, and A4. Let's say I want those EXACT formulas moved to the right one column.

In VBA I can say:

Range("B1:B4").Formula = Range("A1:A4").Formula

What I'm looking to do is something like this:

Range("B1:E1").Formula = Range("A1:A4").Formula

See how my B:E range is horizontal verses the vertical range of A1:A4.

I have tried all sorts of transpose options but I can't find any that work because I want the EXACT formula's to transfer.

Any thoughts?

like image 825
J Taylor Avatar asked Mar 27 '26 12:03

J Taylor


2 Answers

You could try something like:

Sub PivotRangeFormulas()
    Dim rngSrc As Range: Set rngSrc = ActiveSheet.Range("A1:A4")
    Dim rngTgt As Range: Set rngTgt = ActiveSheet.Range("B1:E1")
    Dim i As Long: For i = 1 To rngSrc.Rows.Count
        Application.Index(rngTgt, i).Formula = Application.Index(rngSrc, i).Formula
    Next i
End Sub

You could also use an Offset function from the first cell in each range

like image 72
Tragamor Avatar answered Mar 31 '26 12:03

Tragamor


Range("B1:E1").Formula = WorksheetFunction.Transpose(Range("A1:A4").Formula)
like image 34
user3598756 Avatar answered Mar 31 '26 11:03

user3598756



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!