Copy data from Range in one sheet to another

hip2b2

Board Regular
Joined
May 5, 2003
Messages
117
I can delete the marked line and the code seems to properly run. Is the code formatted as it should be????

VBA Code:
'Delete Consecutive expty Rows
    Dim i As Long, lr1 As Long '<<< This line does not seem to impact the code
    lr1 = Cells(Rows.Count, 2).End(xlUp).Row
    For i = 2 To lr
    If Range("B" & i).Value Like "Require" & "*" & "to run" And Range("B" & i - 1).Value = "" Then
    Cells(i, 2).EntireRow.Delete
    End If
    Next i
    End With
'End

Thanks hip
 

Some videos you may like

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.

sericom

Well-known Member
Joined
Jan 19, 2006
Messages
915
This line

VBA Code:
For i = 2 To lr

should probably be

VBA Code:
For i = 2 To lr1
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
The Dim statements are to reserve memory for the variables. They can affect the code if you try to use one of the variables in a manner different than what you have declared them for. I have taken the liberty of changing some of the code. See red font. When deleting rows, it is better to work from bottom to top with the loop so it will not skip any rows when evaluating the criteria. That said, the thread title was for copying from one sheet to another, but this code does not execute any copy command. You should make sure your thread title represents what your issue is.

Rich (BB code):
Dim i As Long, lr1 As Long '<<< This line does not seem to impact the code
    lr1 = Cells(Rows.Count, 2).End(xlUp).Row
       For i = lr1 To 2 Step -1
           If Range("B" & i).Value Like "Require" & "*" & "to run" And Range("B" & i - 1).Value = "" Then
              Rows(i).Delete
          End If
      Next i
End With
 
Solution

hip2b2

Board Regular
Joined
May 5, 2003
Messages
117
The Dim statements are to reserve memory for the variables. They can affect the code if you try to use one of the variables in a manner different than what you have declared them for. I have taken the liberty of changing some of the code. See red font. When deleting rows, it is better to work from bottom to top with the loop so it will not skip any rows when evaluating the criteria. That said, the thread title was for copying from one sheet to another, but this code does not execute any copy command. You should make sure your thread title represents what your issue is.

Rich (BB code):
Dim i As Long, lr1 As Long '<<< This line does not seem to impact the code
    lr1 = Cells(Rows.Count, 2).End(xlUp).Row
       For i = lr1 To 2 Step -1
           If Range("B" & i).Value Like "Require" & "*" & "to run" And Range("B" & i - 1).Value = "" Then
              Rows(i).Delete
          End If
      Next i
End With
Your revised code works perfectly, I will assume that it is more efficient than the "dog's breakfast" I put together.

On the topic of efficiency, does this looping code start at the bottom of the worksheet data range or at the bottom of the worksheet.

Sorry about the Title mismatch, I started one question and changed it to another and forgot the title. (an explanation not an excuse).

Thanks

hip
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

As written, the bottom of the data range.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,944
Messages
5,627,754
Members
416,273
Latest member
lharnage7331

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