Copy & Paste via Looping

Magdoulin

Board Regular
Joined
Jan 11, 2013
Messages
63
Hi Guys,

Happy new year



I seek your kind assistance please as I have not mastered that looping thing yet in VBA.

I have range Column: A in Worksheet: Sheet1 in Workbook: file1.xlsm, that got some blank and non-blank cells.

And I have another range Column: B in Worksheet: Sheet2 in Workbook: file2.xlsm, that got some blank and non-blank cells as well.

What I need is a macro that loops copying every non-blank cell from range Column: A in Worksheet: Sheet1 in Workbook: file1.xlsm to paste it as value in the next blank cell in range Column: B in Worksheet: Sheet2 in Workbook: file2.xlsm.

Is there a way to do that without the need to write that huge block of code that I have written to complete this simple task? By looping perhaps?



Thank you.



This post is originally published here:

https://www.reddit.com/r/excel/comments/kob53o
 

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Magdoulin

Board Regular
Joined
Jan 11, 2013
Messages
63
Let's see the code you are referring to.


It is a bit of long confusing one, and the names of the files, sheets and ranges is a bit different, but, here you are


VBA Code:
Sub English_Monthly_Report_Update()



'Update English tab



If ThisWorkbook.Worksheets("Reports Data").Range("AC7") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC7").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC8") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC8").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC9") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC9").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC10") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC10").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC11") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC11").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC12") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC12").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC13") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC13").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC14") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC14").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC15") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC15").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC16") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC16").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC17") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC17").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC18") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC18").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC19") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC19").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC20") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC20").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC21") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC21").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC22") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC22").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC23") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC23").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC24") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC24").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC25") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC25").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC26") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC26").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC27") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC27").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC28") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC28").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC29") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC29").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC30") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC30").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC31") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC31").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC32") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC32").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC33") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC33").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC34") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC34").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC35") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC35").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC36") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC36").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



If ThisWorkbook.Worksheets("Reports Data").Range("AC37") <> "" Then

ThisWorkbook.Worksheets("Reports Data").Range("AC37").Copy

Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Else: Workbooks("File2.xlsm").Worksheets("Eng").Range("S1").End(xlDown).Offset(1, 0).Value = ""

End If



End Sub
 
Last edited by a moderator:

Watch MrExcel Video

Forum statistics

Threads
1,126,986
Messages
5,621,990
Members
415,872
Latest member
ReignEternal

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
Top