How to copy multiple rows using If

Diving_Dan

Board Regular
Joined
Oct 20, 2019
Messages
161
Hi all,

I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need to replicate this for the 19 rows beneath this. Do I need to Keep writing the same code for each row or can I write a code to cover all the rows.

Thanks


Code:
Sub Copy_To_Email()

    If Not Range("B5") = "" Then
        Range("E5:H5,N5").Copy
        
    End If
        
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
If I also didn't want a row to be copied if B5 or the cells below held "RESERVE LIST" do I need to amend the 5th line of code or do I need a new line of code? I've tried playing around with it but I don't really know what i'm doing.

Thanks

With the following approach you will not have the issue: it leaves the dotted moving outline around the last data copied

Code:
Sub Copy_To_Email()
  Dim i As Long
  Sheets("Email").Range("A2:D" & Rows.Count).ClearContents
  For i = 5 To Range("B" & Rows.Count).End(xlUp).Row
    If Not Range("B" & i) = "" Then
      Range("E" & i & ":H" & i & ",N" & i).Copy Sheets("Email").Range("A" & Rows.Count).End(xlUp)(2)
    End If
  Next
End Sub
 
Upvote 0
Im confused, what do you want the macro to do it it finds "RESERVE LIST" in a cell in Column B??
 
Upvote 0
If Column B is either blank OR has RESERVE LIST I don't want that row of cells to be copied. If Column B has anything else in it then i want the other cells to be copied.

I hope that is clearer, I think i'm getting myself confused too.
 
Upvote 0
If Column B is either blank OR has RESERVE LIST I don't want that row of cells to be copied. If Column B has anything else in it then i want the other cells to be copied.

I hope that is clearer, I think i'm getting myself confused too.


Try this

Code:
Sub Copy_To_Email()
  Dim i As Long
  Sheets("Email").Range("A2:D" & Rows.Count).ClearContents
  For i = 5 To Range("B" & Rows.Count).End(xlUp).Row
    If Range("B" & i) <> "" And UCase(Range("B" & i)) <> UCase("RESERVE LIST") Then
      Range("E" & i & ":H" & i & ",N" & i).Copy Sheets("Email").Range("A" & Rows.Count).End(xlUp)(2)
    End If
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,048
Messages
6,122,862
Members
449,097
Latest member
dbomb1414

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