Loop VBA macro

jcbetanzos

New Member
Joined
Apr 24, 2017
Messages
5
Hello,

I need help with the following Macro:

Sheets("Sheet1").Activate
ActiveSheet.Range("E2:H2").Select
Selection.Copy
ActiveSheet.Range("E3").Activate
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Activate
Loop
Selection.PasteSpecial xlPasteAll

So basically, this macro copy the content for the range E2 to H2, then every time is active paste the content in column E, starting in the cell E3 only if the cell is empty.

The only thing is that I need to click on the macro to copy and paste the content of the range E2 to H2 and I want to know a way to do this automatically.

Something like:

if column A has values from the cell A2 to the A9, then copy the range E2 to H2 and paste it from the cell E3 to E9,
depending how many values have column A, if have values from cell A2 to A1788, then copy the range E2 to H2 and paste it starting in the cell E3 to E1788.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
The code checks E3 and down to find empty cells then copy and paste but you need now check column A to find non-empty cells for copying and pasting? Just want to make sure before spending time working on it.
 
Upvote 0
Hi & welcome to MrExcel
Is this what you want
Code:
Sub CopyPaste()
   Range("E2:H2").Copy Range("E3", Range("A" & Rows.Count).End(xlUp).Offset(, 4)).SpecialCells(xlBlanks)
End Sub
 
Upvote 0
Yes, I created a different VBA code for this:

Code:
Sub hn()
'unprotect the workbook
    ActiveSheet.Unprotect
'
Application.ScreenUpdating = False
'
'
    Sheets("EXTERNAL LABELS").Activate
If Range("B2") = "" Then
    msgbox "Please add a valid Part Number and try Again"
    '
    ElseIf Range("B3") = "" Then
    msgbox "Please add a valid Part Number and try Again"
    '
    Else
    Range("E2:H2").AutoFill Destination:=Range("(E2:H2):E" & Range("B" & Rows.Count).End(xlUp).Row)
End If
        '
        '
'Delete all empty rows.
If Not IsEmpty(ActiveCell.Value) Then
    '   Find last row on sheet
    myLastRow = Range("B2").SpecialCells(xlLastCell).Row
    '   Find all blank values in column B and delete those rows
    Range("B2:B" & myLastRow).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End If
'
Application.ScreenUpdating = True
'Activate protection
'
    ActiveSheet.Protect
    ActiveWorkbook.Protect Structure:=True, Windows:=False
'save the changes made
    ActiveWorkbook.Save
'
End Sub

fWy9q_v5dnmXN_iQ7VB74l3cL_oeke0JmSSijpzaRHazXBYDNwtTsPTllzbD0Ef9eLe5bvW_c-w0LEJlmtzL=w1360-h633-rw

So basically what it does is autofill the range E2:H2 to the last active cells from the column B, then search for all the empty cells in column B and delete all the empty row.
But what I want is when I click "copy" only to copy the range E2:H2 in the active cells of column B.
Any Idea?
 
Upvote 0
Try
Code:
Sub CopyPaste()
   range("E2:H2").Copy range("B3", range("A" & Rows.Count).End(xlUp).Offset(, 1)).SpecialCells(xlBlanks).Offset(, 3)
End Sub
 
Upvote 0
Try
Code:
Sub CopyPaste()
   range("E2:H2").Copy range("B3", range("A" & Rows.Count).End(xlUp).Offset(, 1)).SpecialCells(xlBlanks).Offset(, 3)
End Sub

Thank You for the help, however, the code didn't work :(

this was the one that I found and is working fine!

Code:
Code:
Sub hn()
Range("E2:H2").Copy Destination:=Range("(E2:H2):E" & Range("B" & Rows.Count).End(xlUp).Row)
End Sub

The only problem is if I run the code without any value in cell B2 it copy the range (E2:H2) to (E1:H1) and mess everything.
To avoid this I add this IF statement:
Code:
Code:
If Range("B2") = "" Then
    MsgBox "Please add a valid Part Number in cell B2 and try Again"
    Exit Sub
    '
    Else
    Range("E2:H2").Copy Destination:=Range("(E2:H2):E" & Range("B" & Rows.Count).End(xlUp).Row)
End If

Please let me know if you guys have a better idea to improve this code :)
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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