Insert Single Cell for Filtered Rows

Argonous

New Member
Joined
Sep 23, 2004
Messages
15
Office Version
  1. 2019
Platform
  1. Windows
Hello All,

I am trying to create a macro that will take an .xml file that is opened in Excel and convert the data into a single lines of useable data. I have the simple stuff like removing the formatting in place, however I am at a loss at how to insert and delete cells based on the value of certain cells.

The first thing that the macro needs to do is insert a single cell (like a tab over) after a cell that contains a specific value. The 2nd table below shows how a single cell needs to be inserted to the right of the "1L" value in the "ID" column and 2 cells inserted to the right of the "X" value in the same column. After that, I need for the macro to delete a range of cells for the value in the "ID" column and move the data from the row below upward. Finally, any row with "1E" in the "ID" column needs to be deleted. An example of how the final line of data should look is in the 3rd table below.

Hopefully someone here can help me out. Thanks in advance!


Original Data:
KeyIDValueDateTime
7​
LCJ02/09/2021
11:53:09​
8​
1L11102/09/2021
11:53:09​
9​
X818R1245402/09/2021
11:53:09​
10​
1EOK02/09/2021
11:53:09​

Insert Single Cell:
KeyIDValueDateTime
7​
LCJ02/09/2021
11:53:09​
8​
1L11102/09/2021
11:53:09​
9​
X818R1245402/09/2021
11:53:09​
10​
1EOK02/09/2021
11:53:09​
* Inserts cell or cells to the right of the "ID" column based on the data in each cell.

Final Result:
KeyIDBuildingRoomAssetDateTime
7​
LCJ111818R1245402/09/2021
11:53:09​
*Deletes a range of cells and moves the data up.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
can you share what the raw data looks like for more than one example, i.e. eight lines of raw data.
 
Upvote 0
See if this works for you

VBA Code:
Sub arnge()
Dim i As Long, lr As Long

lr = Cells(Rows.Count, 1).End(xlUp).Row
For i = lr To 2 Step -1
    Cells(i - 3, 6).Value = Cells(i - 2, 4).Value
    Cells(i - 3, 7).Value = Cells(i - 2, 5).Value
    Cells(i - 3, 4).Value = Cells(i - 2, 3).Value
    Cells(i - 3, 5).Value = Cells(i - 1, 3).Value
    Rows(i & ":" & i - 2).Delete shift:=xlDown
    i = i - 3
Next i
Columns(4).NumberFormat = "0"
[A1:G1].Value = Array("Key", "ID", "Building", "Room", "Asset", "Date", "Time")
End Sub
 
Upvote 0
Here is the complete raw data file.

Book5
ABCDEF
1keydataidvaluedatetimesource
21S00195B2FF607010814080310020B180002/09/20219:43:08AUTO
333Uadf_ncccs_v6x.xml 1 mac02/09/20219:43:08AUTO
454U6.1.002/09/20219:43:08AUTO
57LCJ02/09/202111:53:09KEY
681L11102/09/202111:53:09KEY
79X818R1245402/09/202111:53:093 OF 9
8101EOK02/09/202111:53:09ACMT
912LCJ02/09/202111:53:10DEFAULT
10131L11102/09/202111:53:10DEFAULT
1114X818R1245502/09/202111:53:103 OF 9
12151EOK02/09/202111:53:10ACMT
1317LCJ02/09/202111:53:12DEFAULT
14181L11102/09/202111:53:12DEFAULT
1519X818R1245602/09/202111:53:123 OF 9
16201EOK02/09/202111:53:12ACMT
1722LCJ02/09/202111:53:14DEFAULT
18231L11102/09/202111:53:14DEFAULT
1924X818R1245702/09/202111:53:143 OF 9
20251EOK02/09/202111:53:14ACMT
2127LCJ02/09/202111:53:15DEFAULT
22281L11102/09/202111:53:15DEFAULT
2329X818R1245802/09/202111:53:153 OF 9
24301EOK02/09/202111:53:15ACMT
2532LCJ02/09/202111:53:17DEFAULT
26331L11102/09/202111:53:17DEFAULT
2734X818R1245902/09/202111:53:173 OF 9
28351EOK02/09/202111:53:17ACMT
2937LCJ02/09/202111:53:20DEFAULT
30381L11102/09/202111:53:20DEFAULT
3139X818R1246002/09/202111:53:203 OF 9
32401EOK
Sheet1
 
Last edited:
Upvote 0
Thanks Maniac! That worked great. However, I had left off a final column (source) that I need to keep for the "X" ID. Can you modify the code to accommodate this?

Thanks again!
 
Upvote 0
Try

VBA Code:
Sub arnge()
Dim i As Long, lr As Long

lr = Cells(Rows.Count, 1).End(xlUp).Row
For i = lr To 2 Step -1
    Cells(i - 3, 6).Value = Cells(i - 2, 4).Value
    Cells(i - 3, 7).Value = Cells(i - 2, 5).Value
    Cells(i - 3, 8).Value = Cells(i - 1, 6).Value
    Cells(i - 3, 4).Value = Cells(i - 2, 3).Value
    Cells(i - 3, 5).Value = Cells(i - 1, 3).Value
    Rows(i & ":" & i - 2).Delete shift:=xlDown
    i = i - 3
Next i
Columns(4).NumberFormat = "0"
[A1:H1].Value = Array("Key", "ID", "Building", "Room", "Asset", "Date", "Time", "Source")
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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