VBA-looping

Anna314

New Member
Joined
Sep 30, 2017
Messages
5
Hi

It is my first Excel related post ever but at this stage I'm desperate.
I've searched many websites and forums but can't find solution for the problem.

I have Command button in sheet 1 that is responsible for copying data from table in Sheet 2, pasting in specific cells in sheet 1 and printing Sheet 1.
Then it goes to the next row in Sheet 2, copying,pasting to Sheet 1 and printing Sheet1.
The problem is that it doesn't stop and prints empty cells too despite Do While Loop.
I tries CountA and Offset too but nothing works.

Option Explicit

Private Sub CommandButton1_Click()

Call DoWhile_Loop

End Sub


Sub DoWhile_Loop()

Dim BlankFound As Boolean
Dim x As Long

'Loop until a blank cell is found in Column A


Do While BlankFound = False
x = x + 1

If Cells(x, "A").Value = "" Then
BlankFound = True
End If



Worksheets("Sheet2").Range("A2").Copy
Worksheets("Sheet1").Range("B7").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("B2").Copy
Worksheets("Sheet1").Range("B11").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("C2").Copy
Worksheets("Sheet1").Range("D7").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("D2").Copy
Worksheets("Sheet1").Range("D3").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("E2").Copy
Worksheets("Sheet1").Range("D11").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("G2").Copy
Worksheets("Sheet1").Range("B3").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("F2").Copy
Worksheets("Sheet1").Range("B19").PasteSpecial Paste:=xlPasteFormulas
ActiveSheet.PrintOut


'After copying,paste printing row 2 in Sheet 2 it goes to next row in sheet 2:
Worksheets("Sheet2").Range("A3").Copy
Worksheets("Sheet1").Range("B7").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("B3").Copy
Worksheets("Sheet1").Range("B11").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("C3").Copy
Worksheets("Sheet1").Range("D7").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("D3").Copy
Worksheets("Sheet1").Range("D3").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("E3").Copy
Worksheets("Sheet1").Range("D11").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("G3").Copy
Worksheets("Sheet1").Range("B3").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("F3").Copy
Worksheets("Sheet1").Range("B19").PasteSpecial Paste:=xlPasteFormulas
ActiveSheet.PrintOut

Loop


'Report out first blank cell found in Column A
MsgBox " All backhauls printed!"


End Sub

I would be appreciate for help.
Regards

anna
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Are you saying you want it to run until the last row of data in column A or is there data after the first blank cell in column A?
 
Upvote 0
Are you saying you want it to run until the last row of data in column A or is there data after the first blank cell in column A?

I want to run until the last row of data in column A. There will be no blank cells between rows
 
Upvote 0
Does the code below give the correct results?

Code:
Private Sub CommandButton1_Click()

Dim x As Long

For x = 2 To Cells(Rows.Count, "A").End(xlUp).Row

Worksheets("Sheet2").Range("A" & x).Copy
Worksheets("Sheet1").Range("B7").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("B" & x).Copy
Worksheets("Sheet1").Range("B11").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("C" & x).Copy
Worksheets("Sheet1").Range("D7").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("D" & x).Copy
Worksheets("Sheet1").Range("D3").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("E" & x).Copy
Worksheets("Sheet1").Range("D11").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("G" & x).Copy
Worksheets("Sheet1").Range("B3").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("F" & x).Copy
Worksheets("Sheet1").Range("B19").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet1").PrintOut

Next

'Report out first blank cell found in Column A
 MsgBox " All backhauls printed!"


End Sub
 
Upvote 0
It works. Thank you so much for your time. I'm so grateful and appreciate that you helped me. I think this is the feeling that everyone has once the issue is sorted. Thank you so much again.



Does the code below give the correct results?

Code:
Private Sub CommandButton1_Click()

Dim x As Long

For x = 2 To Cells(Rows.Count, "A").End(xlUp).Row

Worksheets("Sheet2").Range("A" & x).Copy
Worksheets("Sheet1").Range("B7").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("B" & x).Copy
Worksheets("Sheet1").Range("B11").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("C" & x).Copy
Worksheets("Sheet1").Range("D7").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("D" & x).Copy
Worksheets("Sheet1").Range("D3").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("E" & x).Copy
Worksheets("Sheet1").Range("D11").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("G" & x).Copy
Worksheets("Sheet1").Range("B3").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet2").Range("F" & x).Copy
Worksheets("Sheet1").Range("B19").PasteSpecial Paste:=xlPasteFormulas

Worksheets("Sheet1").PrintOut

Next

'Report out first blank cell found in Column A
 MsgBox " All backhauls printed!"


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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