0

How can I repeat this process for up to 500 cells of data, displayed down a column?

Each iteration will need to move downward in the respective columns one cell at a time in three places:

  1. The B2 reference will roll to B3, B4, etc. using a relative reference
  2. R[-2]C[-1] will roll to R[-1]C[-1], R[1]C[-1], etc., using a relative reference
  3. The D2 reference will roll to D3, D4, etc. using a relative reference
Sub Email()
    Application.Goto Reference:="Email"
    Sheets("Tickers").Select
    If Range("B2") = Empty Then
        Exit Sub
    End If
    Sheets("Moves").Select
    Range("C4").Select
    ActiveCell.FormulaR1C1 = "=Tickers!R[-2]C[-1]"
    Worksheets("Moves").Activate
    Application.Wait (Now + TimeValue("0:00:10"))
    ActiveSheet.Range("B2:L129").Copy
    
    With Worksheets("Tickers")
        .Activate
        .Range("D2").Select
        .Pictures.Paste
    End With
End Sub

The task is to take a list of tickers (column B on "Tickers", starting with cell B2), insert those tickers one by one on "Moves" in cell C4, and take the resulting table and copy-paste a picture of the table from Moves back into column D of "Tickers" (the table for the ticker in B2 would go in D2, B3 in D3, etc.).
Amit Verma
  • 8,660
  • 8
  • 35
  • 40
gatachompp
  • 15
  • 4
  • 1
    Use a loop. [This QA](https://stackoverflow.com/questions/46412719/how-to-to-loop-through-cells-in-a-column-and-to-find-the-latest-date-of-the-lis) has a bunch of ways to do it. – Warcupine Jan 18 '21 at 15:11

1 Answers1

0

Use a For Each to loop through cells in a range

Read code's comments and adjust it to fit your needs

EDIT: Removed the offset part of this line:

movesFirstCell.Offset(counter, 0).Formula2R1C1 = "=Tickers!R[" & rowReference & "]C[-1]"

so formula is always adjusted in the same cell

Public Sub Email()

    Application.Goto Reference:="Email"

    ' Set a reference to tickers sheet
    Dim tickersSheet As Worksheet
    Set tickersSheet = ThisWorkbook.Worksheets("Tickers")

    ' Set a reference to tickers range
    Dim tickersRange As Range
    Set tickersRange = tickersSheet.Range("B2:B502")

    ' Set a reference to moves sheet
    Dim movesSheet As Worksheet
    Set movesSheet = ThisWorkbook.Worksheets("Moves")
    
    ' Set a reference to moves starting cell
    Dim movesFirstCell As Range
    Set movesFirstCell = movesSheet.Range("C4")
    
    ' Use a counter to make everything else relative
    Dim counter As Long
    counter = 0
    
    ' Loop through cells in tickers sheet starting in B2
    Dim tickersCell As Range
    For Each tickersCell In tickersRange
    
        If tickersCell.Value <> vbNullString Then
            
            
            ' Set row reference
            Dim rowReference As Long
            rowReference = (-2 + counter)
            
            ' Store formula
            movesFirstCell.Formula2R1C1 = "=Tickers!R[" & rowReference & "]C[-1]"
            
            ' Copy range as picture
            movesSheet.Range("B2:L129").CopyPicture
            tickersSheet.Range("D2").Offset(counter, 0).PasteSpecial
            
            ' Increment counter
            counter = counter + 1
        
        End If
    
    Next tickersCell

End Sub

PS: I didn't understand this line: Application.Wait (Now + TimeValue("0:00:10"))

Let me know if it works

Ricardo Diaz
  • 5,658
  • 2
  • 19
  • 30
  • Partly, but the result is incomplete - this code copies B column cells on "Tickers" down C column cells in "Moves", whereas I want B column cells copied one after another into the same C cell (C4). I include the Application.Wait because excel takes several seconds to populate each table that results from copying over the B column link. I've tried to correct the code you've provided (which is excellent absent this one disconnect), but can't figure it out... Thank you immensely for the help. – gatachompp Jan 18 '21 at 21:09
  • Pls provide an example of this `whereas I want B column cells copied one after another into the same C cell (C4)` you can [edit](https://stackoverflow.com/posts/65776800/edit) your question and add it there. Let me know when you do it, in a comment here. – Ricardo Diaz Jan 18 '21 at 21:17
  • I've added some additional context at the bottom of my original question. – gatachompp Jan 18 '21 at 21:24
  • Check the edit and let me know if it's what you're looking for – Ricardo Diaz Jan 18 '21 at 21:27