Macro to print a table filling from a sheet then move to the next row on the sheet

elang

New Member
Joined
Aug 26, 2024
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
I have not used macros much and just getting into them. I know this can be done with a loop and an array I just dont know the syntax or commands to make it do exactly what I want. ANY help would be greatly apperciated

So I need these table datas to correspond to the the row in sheet:
'Table 1'!$C$18 = Name (Sheet1!E)
'Table 1'!$A$24 = System Model (Sheet1!C)
'Table 1'!$D$24 = Asset Tag (Sheet1!B)
'Table 1'!$G$24 = Serial Number (Sheet1!D)

Print 2 copies then move to the next row so
like:

'Table 1'!$C$18 = Name (Sheet1!E1)
'Table 1'!$A$24 = System Model (Sheet1!C1)
'Table 1'!$D$24 = Asset Tag (Sheet1!B1)
'Table 1'!$G$24 = Serial Number (Sheet1!D1)
ActiveSheet.PrintOut , Copies:=2

'Table 1'!$C$18 = Name (Sheet1!E2)
'Table 1'!$A$24 = System Model (Sheet1!C2)
'Table 1'!$D$24 = Asset Tag (Sheet1!B2)
'Table 1'!$G$24 = Serial Number (Sheet1!D2)
ActiveSheet.PrintOut , Copies:=2

'Table 1'!$C$18 = Name (Sheet1!E3)
'Table 1'!$A$24 = System Model (Sheet1!C3)
'Table 1'!$D$24 = Asset Tag (Sheet1!B3)
'Table 1'!$G$24 = Serial Number (Sheet1!D3)
ActiveSheet.PrintOut , Copies:=2

'Table 1'!$C$18 = Name (Sheet1!E4)
'Table 1'!$A$24 = System Model (Sheet1!C4)
'Table 1'!$D$24 = Asset Tag (Sheet1!B4)
'Table 1'!$G$24 = Serial Number (Sheet1!D4)
ActiveSheet.PrintOut , Copies:=2
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
So This is what I have:

Sub PrintMoveToNextLine()

Set Table = Range(Sheet2B2, Range(Sheet2B2).End(xlToRight).End(xlDown)) 'select the range of the data going from the first box over to the left and then all the way down for where there is data

For i = 1 To Table.Rows.Count 'set the number of time to run based on how much data

Cells.Replace (Table1C18 = Row.Cells(i, 4)) 'Name (Sheet1!E)
Cells.Replace (Table1A24 = Row.Cells(i, 2)) 'System Model (Sheet1!C)
Cells.Replace (Table1D24 = Row.Cells(i, 1)) 'Asset Tag (Sheet1!B)
Cells.Replace (Table1G24 = Row.Cells(i, 3)) 'Serial Number (Sheet1!D)
ActiveSheet.PrintOut , Copies:=2

Next i

End Sub

It seems like Cells.Replace isnt the correct command or I have the syntax there wrong?
 
Upvote 0
SOOOOOOOOOOOOOOOOOOOOOO it took some time but I have got it working. My main probable was not initiating everything with Dim and then assigning it with Set
The other was using cell replace when I need to use Value
Its been a fun few hours. But I am all set! Will use this again!!!

VBA Code:
Sub PrintMoveToNextLine()
   
    Dim i As Integer
    Dim NumRows As Integer
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim Table2 As Range
  
    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    Set ws2 = ThisWorkbook.Sheets("Sheet2")
   
    Set Table2 = ws2.Range("B2", ws2.Range("B2").End(xlDown))
   
    NumRows = Table2.Rows.Count
   
    For i = 1 To NumRows
   
        ws1.Range("C18").Value = Table2.Cells(i, 4).Value 'Name (Sheet1!E)
        ws1.Range("A24").Value = Table2.Cells(i, 2).Value 'System Model (Sheet1!C)
        ws1.Range("AssetTag").Value = Table2.Cells(i, 1).Value 'Asset Tag (Sheet1!B)
        ws1.Range("Serial_Number").Value = Table2.Cells(i, 3).Value 'Serial Number (Sheet1!D)
        ws1.PrintOut , Copies:=2

    Next i

End Sub
 
Last edited by a moderator:
Upvote 0
Solution
Glad you figured it out. Thanks for posting your solution.

For the future, when posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags to the solution post for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,224,803
Messages
6,181,055
Members
453,014
Latest member
Chris258

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top