Loop through data and create formulas

jwburritt

New Member
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
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
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
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
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
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
 

jwburritt

New Member
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
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
 

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...
Top