Macro to offset at specific tags and label them based on prior tag

ExcelAssistance

New Member
Joined
Jan 21, 2018
Messages
8
Hi guys,

Just looking for a little vba assistance. I'm sure this one has been asked but wasn't sure how to describe it to search for it. Basically if I had a sheet where Column A is populated as follows:

1: Fruits
2: Apples
2: Bananas
2: Pears
1: Vegetables
2: Broccoli
2: Peas

and what I'd like is for the vba to go through the entire column and everytime it finds something with a 1 preceeding it, it looks at all the 2's below and shifts those 2's into column b and places the same 1 grouping in column a...so the above would appear as follows:

1: Fruits 2: Apples
1: Fruits 2: Bananas
1: Fruits 2: Pears
1: Vegetables 2: Broccoli
1: Vegetables 2: Peas

How would I go about doing that? Everything either has a 1 or a 2 before it (they aren't 1's and 2's but they are tags just the same) to define it and ideally I'd also like to strip the tags off as well (and also eliminate the original thing tagged as 1 since it now appears to the left of the item it defines) so really it would just be column a and b looking like

Fruits Apples
Fruits Bananas
Fruits Pears
Vegetables Bananas
Vegetables Peas

Can anybody let me know how to do this? Thanks in advance for any assistance you can give.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
What are the actual tags?
Also is there a space between the tag & the value?
 
Upvote 0
Hi fluff,

Thanks for responding, the tags are

<supportgroup>
<supportid>

And there is a space between the > and the value I'm looking to maintain.

Thanks again.
 
Upvote 0
As I can't see what your tags are try this
Code:
Sub RearrangeData()
   
   Dim Ar As Areas
   Dim Rng As Range
   
   With Columns(1)
      .Replace "[COLOR=#ff0000]1:[/COLOR] ", "=X", xlPart, , , , False, False
      Set Ar = .SpecialCells(xlConstants).Areas
      .Replace "=X", "", xlPart, , , , False, False
   End With
   
   For Each Rng In Ar
      Rng.Cut Rng.Offset(-1, 1)
      Rng.Offset(, -1).FillDown
   Next Rng
   Columns(1).SpecialCells(xlBlanks).EntireRow.Delete
   Columns(2).Replace "[COLOR=#ff0000]2:[/COLOR] ", "", xlPart, , , , False, False
End Sub
Replacing the values in red, with your tags.
If you have any problems, please repost what your tags are HTML tags or similar put a space before/after < & >
 
Upvote 0
Hi Fluff,

Sorry I didn't respond yesterday I was at work and couldn't get back to you. So the first thing and most important part is that after looking at the data (I was going off memory initially) I realize I didn't give the best explanation. I'm including pictures that captures the data as it appears (however I can't share the actual data since it's work sensitive).
I thought I'd try your code anyway because I figured I could tweak it if it got me close enough but I was running into issues with the Rng.Cut Rng.Offset(-1,1) line. Anyway, any additional help you or somebody else can provide would be greatly appreciated.

Please see below for how the data essentially looks and how it should look. Wasn't sure how to post a pic, and unfortunately I'm rushing out the door for work , really really sorry...I have them as imgur links which I recognize isn't ideal and I apologize.

1) How it currently looks:
https://imgur.com/a/cMMq5

2) How it should look after the code is run:
https://imgur.com/a/nVRFo
 
Upvote 0
OK, how about
Code:
Sub RearrangeData()
   
   Dim Ar As Areas
   Dim Rng As Range
   
   With Columns(1)
      .Replace "</User Group>", "", xlPart, , , , False, False
      .Replace "<User Group>", "=X", xlPart, , , , False, False
      Set Ar = .SpecialCells(xlConstants).Areas
      .Replace "=X", "", xlPart, , , , False, False
   End With
   Ar(1).EntireRow.Delete
   For Each Rng In Ar
      Rng.Cut Rng.Offset(-1, 1)
      Rng.Offset(, -1).FillDown
   Next Rng
   Columns(1).SpecialCells(xlBlanks).EntireRow.Delete
   Columns(2).Replace "<User ID>", "", xlPart, , , , False, False
   Columns(2).Replace "</User ID>", "", xlPart, , , , False, False
End Sub
 
Upvote 0
OK so that code doesn't error out like it was yesterday...but it just deletes everything on the sheet.

I'm assuming I'm still putting the tags inside the first set of quotes after the .replace lines, correct?

Sorry, I really appreciate all your help
 
Upvote 0
Apologies, I didn't notice that the board has stripped them out
Code:
Sub RearrangeData()
   
   Dim Ar As Areas
   Dim Rng As Range
   
   With Columns(1)
      .Replace "< /User Group >", "", xlPart, , , , False, False
      .Replace "< User Group >", "=X", xlPart, , , , False, False
      Set Ar = .SpecialCells(xlConstants).Areas
      .Replace "=X", "", xlPart, , , , False, False
   End With
   
   For Each Rng In Ar
      Rng.Cut Rng.Offset(-1, 1)
      Rng.Offset(, -1).FillDown
   Next Rng
   Columns(1).SpecialCells(xlBlanks).EntireRow.Delete
   Columns(2).Replace "< User ID >", "", xlPart, , , , False, False
   Columns(2).Replace "< /User ID >", "", xlPart, , , , False, False
End Sub
You'll need to remove the spaces
 
Upvote 0
No apologies necessary, appreciate all your help.

I'm getting an application-defined or object defined error on the line

Rng.Cut rng.Offset (-1,1)

?
 
Upvote 0
Helps if I save my code before testing & then closing without saving :banghead:
Try
Code:
Sub RearrangeData()
   
   Dim Ar As Areas
   Dim Rng As Range
   
   With Columns(1)
      .Replace "< /User Group >", "", xlPart, , , , False, False
      .Replace "< User Group >", "=X", xlPart, , , , False, False
      Set Ar = .SpecialCells(xlConstants).Areas
      .Replace "=X", "", xlPart, , , , False, False
   End With
 [COLOR=#ff0000]  Ar(1).EntireRow.Delete[/COLOR]
   For Each Rng In Ar
      Rng.Cut Rng.Offset(-1, 1)
      Rng.Offset(, -1).FillDown
   Next Rng
   Columns(1).SpecialCells(xlBlanks).EntireRow.Delete
   Columns(2).Replace "< User ID >", "", xlPart, , , , False, False
   Columns(2).Replace "< /User ID >", "", xlPart, , , , False, False
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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