Loop through data and create formulas

jwburritt

New Member
Joined
May 22, 2019
Messages
48
Hello: I have the following data. I want col A to equal the value of the first cell to its right, but retain it in a formula. For example the successful result in A2 would be "=B2" not "1". The following code gets the value in col A but it's a value, not a formula. Thank you!

ABCD
11
22
33

<tbody>
</tbody>

Code:
Sub temp()

    Dim i As Long
    For i = 2 To 4
    
        Cells(i, 1).Formula = Cells(i, 1).End(xlToRight).Value


    Next i


End Sub
 
Last edited:

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,857
Office Version
365
Platform
Windows
Are you really just trying to come up with a formula in column A to return the first value to the right?
If so, just use this formula in cell A2 and copy down.
Code:
=MATCH(TRUE,INDEX(B2:D2<>0,),0)
 

jwburritt

New Member
Joined
May 22, 2019
Messages
48
Thanks so much. Hadn't thought of that! But it's part of a much larger sheet. And depending on in which column the data sits, the formula as to change. Here's an example. The data in col B needs to be negative and put in col a in a formula. The rest are simple formulas.

ABCD
-11
22
33

<tbody>
</tbody>
 
Last edited:

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
9,780
Office Version
2007
Platform
Windows
Returning to your original macro. Try this

Code:
Sub temp()
  Dim i As Long
  For i = 2 To 4
    Cells(i, "A").Formula = "=" & Cells(i, 1).End(xlToRight).Address
  Next i
End Sub
 

jwburritt

New Member
Joined
May 22, 2019
Messages
48
Thanks, Dante. Works fine. But, it produces absolute cell references. I need relative because it's going to get thrown into a table with existing data. Possible? I've tried everything and can't make it work. Ugh.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
9,780
Office Version
2007
Platform
Windows
Thanks, Dante. Works fine. But, it produces absolute cell references. I need relative because it's going to get thrown into a table with existing data. Possible? I've tried everything and can't make it work. Ugh.
Try:

Code:
Sub temp()
  Dim i As Long
  For i = 2 To 4
    Cells(i, "A").Formula = "=" & Cells(i, 1).End(xlToRight)[COLOR=#0000ff].Address(0, 0)[/COLOR]
  Next i
End Sub
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
9,780
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

jwburritt

New Member
Joined
May 22, 2019
Messages
48
Hello again: I just discovered that the address of the cell reference above needs to also return a structured table address so when I dump the data into the table, the formulas match. In the target table, the formula address are in this form:

=[@[Header]]

They are replicated down in the format above because of the excel calculated column feature in tables. I'm willing to workaround by changing all the formulas in the target table to plain relative formula references -- which Dante's solution provides -- but just thought I'd see if there's vba code that can give a structured table address.

Thanks again!
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
9,780
Office Version
2007
Platform
Windows
Hello again: I just discovered that the address of the cell reference above needs to also return a structured table address so when I dump the data into the table, the formulas match. In the target table, the formula address are in this form:

=[@[Header]]

They are replicated down in the format above because of the excel calculated column feature in tables. I'm willing to workaround by changing all the formulas in the target table to plain relative formula references -- which Dante's solution provides -- but just thought I'd see if there's vba code that can give a structured table address.

Thanks again!

I found this way, maybe it will help you.

Code:
Sub Macro1()
  Dim Obj As Object, t As String, i As Long, col As Long, n As String
  Application.AutoCorrect.AutoFillFormulasInLists = False
  For i = 2 To 4
[COLOR=#0000ff]    Set Obj = Range("A" & i).ListObject[/COLOR]
[COLOR=#0000ff]    If Not Obj Is Nothing Then[/COLOR]
[COLOR=#0000ff]      t = Obj.Name[/COLOR]
[COLOR=#0000ff]      col = Cells(i, 1).End(xlToRight).Column[/COLOR]
[COLOR=#0000ff]      n = ActiveSheet.ListObjects(t).ListColumns(col).Name[/COLOR]
[COLOR=#0000ff]      Cells(i, "A").Formula = "=" & t & "[[#This Row],[" & n & "]]"[/COLOR]
    Else
      Cells(i, "A").Formula = "=" & Cells(i, 1).End(xlToRight).Address(0, 0)
    End If
  Next
  Application.AutoCorrect.AutoFillFormulasInLists = True
End Sub
 

Forum statistics

Threads
1,086,220
Messages
5,388,550
Members
402,120
Latest member
dmitrevski

Some videos you may like

This Week's Hot Topics

Top