Copy data from Range in one sheet to another

hip2b2

Board Regular
Joined
May 5, 2003
Messages
135
Office Version
  1. 2019
Platform
  1. Windows
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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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
 
Upvote 0
Solution
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
 
Upvote 0
As written, the bottom of the data range.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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