Deleting Lines with a Macro Command

Pettor

Board Regular
Joined
Aug 8, 2015
Messages
175
Hello guys,

I have an excel file with some records that are copied/pasted from a website.
Due to the site's structure when I paste the data on excel, some cells cause the line to split (see an example below) and the surname of the person goes to the below line which except this cell all the others are empty.
What I want to do is to create a macro that will get the Surname (in this case "Wick"), will paste it to the last cell of the upper row (besides the name) and will delete the unecessary line afterwards.
This is not happening in all lines so I need also a way excel to figure out which lines are empty (probably based on the empty values of the other cells in this line) and delete them when the paste is done.

Any advice on that? Many thanks!

NicknameAgeName
Thunder30John
Wick

<tbody>
</tbody>
 
Hi Peter,

A. Your code in regards to the text in brackets, indeed erases the FOURTH line, which is the desired outcome and eliminates completely its data. I don't know if they are transfered in the AL2 cell and then are replaced by the data of the line 3. Below I am giving you an example. We can either have a record splitedin two rows (Case A) or splited in thre rows (Case B). The first row includes the headers so it will remain as it is. No need to do anything with it.

So in both cases A & B I need the code to return the data like the table with the desired outcome. Lines 3 or and 4 where it exists to be deleted comlpetely. In the case A the data of the cell AK3 have to be moved to the cell AL2 to have all the data in one line and in the case of a forth line with text in brackets the complete line has to be deleted including also the data in the AK4 cell.


Case A
AIAJAK
1NicknameAgeName
2
Thunder30John
3Wick

<colgroup><col style="mso-width-source:userset;mso-width-alt:4059; width:83pt" width="111" span="4"> </colgroup><tbody>
</tbody>


Case B
AIAJAK
1NicknameAgeName
2Thunder30John
3Wick
4
[Wick]

<colgroup><col style="mso-width-source:userset;mso-width-alt:4059; width:83pt" width="111" span="4"> </colgroup><tbody>
</tbody>


Desired Outcome
AIAJAKAL
1
NicknameAgeNameSurname
2Thunder30JohnWick

<colgroup><col span="4"><col></colgroup><tbody>
</tbody>


B. There are also cases where the AK2 cell is completely blank so in this occassion the line has to be erased because if there is no name there is no reason this libe to exist.

Case C
AIAJAK
1NicknameAgeName
2Thunder30

<colgroup><col style="mso-width-source:userset;mso-width-alt:4059; width:83pt" width="111" span="4"> </colgroup><tbody>
</tbody>


Desired outcome
AIAJAK
1NicknameAgeName

<colgroup><col span="4"></colgroup><tbody>
</tbody>


I hope everything is clear no and sorry for my english but i am not a native speaker...
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Thanks, that is clearer. However, you haven't answered my question:

Then ideally we need to know a column that can safely be used to determine the last row of data in the sheet. I had been using column AK, but if that can be blank it would not be reliable. Is there any column that always has data in every row?
 
Upvote 0
Really sorry, I missed that.

Yes the column that has always data is the column H. Is this ok or you need the last? THe last should be the L.

Thanks!
 
Upvote 0
Yes the column that has always data is the column H.
OK, provided column H has something in it even for rows like Case B rows 3 & 4 and Case C row 2 then try this version

Rich (BB code):
Sub MoveNameDeleteRow_v3()
  Dim r As Long
  
  Application.ScreenUpdating = False
  For r = Range("H" & Rows.Count).End(xlUp).Row To 2 Step -1
    If IsEmpty(Cells(r, "AK").Value) Or Cells(r, "AK").Value Like "[[]*]" Then
      Rows(r).Delete
    ElseIf IsEmpty(Cells(r, "AJ").Value) Then
      Cells(r - 1, "AL").Value = Cells(r, "AK").Value
      Rows(r).Delete
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
It is almost perfect. The ony problem is that it doens't do so in the last row of the data.
The cell AK2 of the bottom row is not moved to the AL1 of the previous row.
 
Upvote 0
It is almost perfect. The ony problem is that it doens't do so in the last row of the data.
The cell AK2 of the bottom row is not moved to the AL1 of the previous row.
Then I'm guessing this was not quite correct?
Yes the column that has always data is the column H.
For the data where the bottom row was not moved, is there something in column H of that bottom row?
 
Upvote 0
No, the line where the AK data that have to be moved are, is completely blank besides this AK cell.
This line is created because of the existance of this cell. That is why I want to move it to the upper line and erase it completely.
 
Upvote 0
No, the line where the AK data that have to be moved are, is completely blank besides this AK cell.
This line is created because of the existance of this cell. That is why I want to move it to the upper line and erase it completely.
OK, so back to my question about whether there is a column that always has data so we can reliably tell where the end of the data is, it seems like the actual answer to that is "No". ;)

In that case, try this instead.

Rich (BB code):
Sub MoveNameDeleteRow_v4()
  Dim r As Long
  
  Application.ScreenUpdating = False
  For r = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row To 2 Step -1
    If IsEmpty(Cells(r, "AK").Value) Or Cells(r, "AK").Value Like "[[]*]" Then
      Rows(r).Delete
    ElseIf IsEmpty(Cells(r, "AJ").Value) Then
      Cells(r - 1, "AL").Value = Cells(r, "AK").Value
      Rows(r).Delete
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Sorry, I thought you were saying about the upper row... Now it is perfect!
Case solved, so let's move to the next one...

Your contribution is amazing!

Thanks again and have a very nice Sunday!
 
Upvote 0
You're welcome. Glad we got there in the end. :)
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,156
Members
448,948
Latest member
spamiki

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