Excel - Combine Line One with Line Two

RudeBoy

Active Member
Joined
Feb 2, 2003
Messages
376
When I run the Macro it deletes all the data here is my data
It was working last week but this time it is deleting everything but the first two lines?
Am using this code:
Sub combineRows()
Dim ws As Worksheet, r As Long, lr As Long, cell As Range
Set ws = ActiveSheet
lr = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
Application.ScreenUpdating = False
For r = lr To 3 Step -1
Set cell = ws.Cells(r, "A")
If cell = "" Then
cell.Offset(-1, 2) = cell.Offset(, 1)
cell.EntireRow.Delete
End If
Next r
End Sub


Sheet1
AB
Incidents Project - Jobs Converted-k.xlsm
1EDIBCBD9
2228 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBD9,
3EDIBCBM9
4316 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBM9,
5EDIBCBW9
6316 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBW9,
7EDIBCBY9
8315 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBY9,
9EDIBC601
1083 //BADRC EXEC EDIP0022,JOBNAME=EDIBC601,
11EDIBC604
12959 //BADRC EXEC EDIP0022,JOBNAME=EDIBC604,
13EDIBC605
1464 //BADRC EXEC EDIP0022,JOBNAME=EDIBC605,
15EDIBC606
16145 //BADRC EXEC EDIP0022,JOBNAME=EDIBC606,
17EDIBC607
18103 //BADRC EXEC EDIP0022,JOBNAME=EDIBC607,
19EDIBC608
20357 //BADRC EXEC EDIP0022,JOBNAME=EDIBC608,
21EDIBC609
2242 //BADRC EXEC EDIP0022,JOBNAME=EDIBC609,
23EDIBC611
2458 //BADRC EXEC EDIP0022,JOBNAME=EDIBC611,
CellConditionCell FormatStop If True
Cells with Conditional Formatting
A:ACell Value="N"textNO
A:ACell Value="Y"textNO
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
There is nothing basically wrong with the code. The problem is likely that the code was being run on a sheet with many blank cells in column A. The criteria to delete is if column A is blank.
 
Upvote 0
You have this loop...

For r = lr To 3 Step -1

which stops at Row 3... maybe that is why Rows 1 and 2 are not processed. However, I do not think you can just change the 3 to 1 because you have this inside the loop...

cell.Offset(-1, 2) = cell.Offset(, 1)

which is offsetting from the cell at Row "r" by negative one which would take it off the worksheet and raise an error when "r" reaches 1.
 
Upvote 0
You have this loop...

For r = lr To 3 Step -1

which stops at Row 3... maybe that is why Rows 1 and 2 are not processed. However, I do not think you can just change the 3 to 1 because you have this inside the loop...

cell.Offset(-1, 2) = cell.Offset(, 1)

which is offsetting from the cell at Row "r" by negative one which would take it off the worksheet and raise an error when "r" reaches 1.

The End Result I am looking for is this:
Incidents Project - Jobs Converted-l.xlsm
ABCDEFGH
1nEDIBCBD9228 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBD9,
2nEDIBCBM9316 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBM9,
3nEDIBCBW9316 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBW9,
4nEDIBCBY9315 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBY9,
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A:ACell Value="N"textNO
A:ACell Value="Y"textNO
 
Upvote 0
There is nothing basically wrong with the code. The problem is likely that the code was being run on a sheet with many blank cells in column A. The criteria to delete is if column A is blank.
The End Result I am looking for is this:
Sheet1
ABCDEFGH
Incidents Project - Jobs Converted-l.xlsm
1nEDIBCBD9228 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBD9,
2nEDIBCBM9316 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBM9,
3nEDIBCBW9316 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBW9,
4nEDIBCBY9315 //BADRC EXEC EDIP0022,JOBNAME=EDIBCBY9,
CellConditionCell FormatStop If True
Cells with Conditional Formatting
A:ACell Value="N"textNO
A:ACell Value="Y"textNO
 
Upvote 0
In the OP the worksheet image posted shows column A with blank cells. If comlumn A has blank cells the code will delete every row on that sheet that does not have a value in column A, except roiws 1 and 2, as Rick pointed out. I ran the code on my system and as long as there is data in column a of a row, it will not delete that row, but it does delete the rows where columna A is blank. So, I suggest you try to run the code on a sheet where column A has data and see if it still deletes all the rows except the first two.
 
Upvote 0
In the OP the worksheet image posted shows column A with blank cells. If comlumn A has blank cells the code will delete every row on that sheet that does not have a value in column A, except roiws 1 and 2, as Rick pointed out. I ran the code on my system and as long as there is data in column a of a row, it will not delete that row, but it does delete the rows where columna A is blank. So, I suggest you try to run the code on a sheet where column A has data and see if it still deletes all the rows except the first two.
When I have data in column A nothing happens I wonder if it is the format of the file as the excel is a .xlsm file?
 
Upvote 0
Well Lets Start over = I have this data:
Incidents Project - Jobs Converted-l.xlsm
AB
6nEDIBC601
7n83 //BADRC EXEC EDIP0022,JOBNAME=EDIBC601,
8nEDIBC604
9n959 //BADRC EXEC EDIP0022,JOBNAME=EDIBC604,
10nEDIBC605
11n64 //BADRC EXEC EDIP0022,JOBNAME=EDIBC605,
12nEDIBC606
13n145 //BADRC EXEC EDIP0022,JOBNAME=EDIBC606,
14nEDIBC607
15n103 //BADRC EXEC EDIP0022,JOBNAME=EDIBC607,
16nEDIBC608
17n357 //BADRC EXEC EDIP0022,JOBNAME=EDIBC608,
18nEDIBC609
19n42 //BADRC EXEC EDIP0022,JOBNAME=EDIBC609,
20nEDIBC611
21n58 //BADRC EXEC EDIP0022,JOBNAME=EDIBC611,
22nEDIBC612
23n116 //BADRC EXEC EDIP0022,JOBNAME=EDIBC612,
24nEDIBC613
25n84 //BADRC EXEC EDIP0022,JOBNAME=EDIBC613,
26nEDIBC614
27n79 //BADRC EXEC EDIP0022,JOBNAME=EDIBC614,
28nEDIBC624
29n689 //BADRC EXEC EDIP0022,JOBNAME=EDIBC624,
30nEDIBFD42
31n35 //BADRC EXEC EDIP0022,JOBNAME=EDIBFD42,
32nEDIBW700
33n94 //BADRC EXEC EDIP0022,JOBNAME=EDIBW700, * EDIP0022 SETS RC=999
34nEDIF0565
35n100 //BADRC EXEC EDIP0022,JOBNAME=EDIF0565,
36nEDIF1133
37n101 //BADRC EXEC EDIP0022,JOBNAME=EDIF1133,
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A:ACell Value="N"textNO
A:ACell Value="Y"textNO


I want to grab line two and place it in cell C right above the line that has EDI job names like this is what the result should be:
Incidents Project - Jobs Converted-l.xlsm
ABC
6nEDIBC60183 //BADRC EXEC EDIP0022,JOBNAME=EDIBC601,
7nEDIBC604959 //BADRC EXEC EDIP0022,JOBNAME=EDIBC604,
8nEDIBC60564 //BADRC EXEC EDIP0022,JOBNAME=EDIBC605,
9nEDIBC606145 //BADRC EXEC EDIP0022,JOBNAME=EDIBC606,
10nEDIBC607103 //BADRC EXEC EDIP0022,JOBNAME=EDIBC607,
11nEDIBC608357 //BADRC EXEC EDIP0022,JOBNAME=EDIBC608,
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A:ACell Value="N"textNO
A:ACell Value="Y"textNO


Thats all I need so how can I accomplish that using a Macro? Thanks for any & all Help - Stay Safe EVERYONE!!
 
Upvote 0
This is based on he images in Post #7. But it will not accomodate entries like those on row 33 which vary in format.

VBA Code:
Sub t()
Dim i As Long
    With Sheets("Sheet1") 'Edit sheet name
        For i = .Cells(Rows.Count, 2).End(xlUp).Row To 7 Step -1
            If Mid(.Cells(i, 2).Value, InStr(.Cells(i, 2), "=") + 1) = .Cells(i - 1, 2).Value & "," Then
                .Cells(i - 1, 3) = .Cells(i, 2).Value
                .Rows(i).Delete
            End If
        Next
        .Columns("B:C").AutoFit
    End With
End Sub
 
Upvote 0
This is based on he images in Post #7. But it will not accomodate entries like those on row 33 which vary in format.

VBA Code:
Sub t()
Dim i As Long
    With Sheets("Sheet1") 'Edit sheet name
        For i = .Cells(Rows.Count, 2).End(xlUp).Row To 7 Step -1
            If Mid(.Cells(i, 2).Value, InStr(.Cells(i, 2), "=") + 1) = .Cells(i - 1, 2).Value & "," Then
                .Cells(i - 1, 3) = .Cells(i, 2).Value
                .Rows(i).Delete
            End If
        Next
        .Columns("B:C").AutoFit
    End With
End Sub

Ok I ran that and it ran from row 7 down. Is there away to have it run from Row 1?
Thanks for your help - Take Care & Stay Safe
 
Upvote 0

Forum statistics

Threads
1,215,517
Messages
6,125,290
Members
449,218
Latest member
Excel Master

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