Excel print loop

M

misaeldv

Hi, I have a workbook with two worksheets; which are mainly static,
except for sheet1C12 which gets values from sheet2A2:A30 (or better yet
till it hits a blank). Now what I want to do is create a loop that will
assing the value of Sheet2A2 into Sheet1C12 (then print); then
increment and assing the value of Sheet2A3 into Sheet1C12 (then print
again) and so on till a last predefined sheet2Ax or till it hits a
blank, I have been reading but cannot find anything similar. Here's
what I have so far

Sub PrintMacro()

Worksheets("Sheet1").Range("C12").Value =
Worksheets("Sheet2").Range("A2").Value
ActiveSheet.PrintPreview

End Sub

I know I have to change ActiveSheet.PrintPreview to
ActiveSheet.PrintOut, in order to print, but what I need to know is a
way to increment A2 into A3, A4.... ect.
Is there a for loop in VBA?
Thanks in advance
 
R

Ron de Bruin

Try this for A2 to A10

Sub PrintMacro()
Dim I As Integer
For I = 2 To 10
Worksheets("Sheet1").Range("C12").Value = _
Worksheets("Sheet2").Range("A" & I).Value
ActiveSheet.PrintPreview
Next I
End Sub
 
M

misaeldv

Thanks, this works like a charm, now I need to find find a way to do it
until it hits a blank cell; I think that will be a while loop, or maybe
an if clause.
Thanks
 
R

Ron de Bruin

You can use this

Sub PrintMacro2()
Dim cell As Range
For Each cell In Sheets("Sheet2").Range("A2:A" & Rows.Count) _
.SpecialCells(xlCellTypeConstants)
Worksheets("Sheet1").Range("C12").Value = cell.Value
ActiveSheet.PrintPreview
Next cell
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Macro doesn't refresh 4
Print function parsing help 5
Looping through a Range in Excel 2
VBA: Activate sheets within a loop 3
Loop within a loop 1
Code to Concat a row in loop 2
For/Next Loop 2
loop 2

Top