Help with a Macro

KiloHotel

New Member
Joined
Feb 4, 2018
Messages
42
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
Hi Guys,

I have a worksheet with alot of data that was given to me with all the information "Glued" together instead of separated into tabs. :warning:

I have basically the macro figured out that everytime it goes down the sheet and its met with the Keyword SOB that it cuts and copies to the next sheet, That's fine but my issue is that it doesnt "Cut" at the bottom, So as a result the next sheet has the correct data at the top but drags across whatever is below it from the original sheet into it and its not ideal.

Another way of explaining this would be :

I have the original data as this:

[TABLE="width: 200"]
<tbody>[TR]
[TD]SOB[/TD]
[TD]DATA 1[/TD]
[/TR]
[TR]
[TD]TEST1[/TD]
[TD]TEST1[/TD]
[/TR]
[TR]
[TD]TEST2[/TD]
[TD]TEST3[/TD]
[/TR]
[TR]
[TD]SOB[/TD]
[TD]DATA 2[/TD]
[/TR]
[TR]
[TD]TEST3[/TD]
[TD]TEST3[/TD]
[/TR]
[TR]
[TD]TEST4[/TD]
[TD]TEST4[/TD]
[/TR]
</tbody>[/TABLE]

All glued together and the macro will drag SOB DATA1 to the next sheet and then all data below it and then subsequently the next sheet after drags Data 2 then everything else after... So i just need it to Cut from the top and then the next time it hits SOB.

This is the Macro I am using:


Sub Splitdata()


Dim Ws As Worksheet
Dim Ar As Areas
Dim i As Long

Set Ws = ActiveSheet
Ws.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "SOB"
With Ws.Range("D1", Ws.Range("D" & Rows.Count).End(xlUp))
.Replace "SOB", "=XXSOB", xlWhole, , False, , False, False
Set Ar = .SpecialCells(xlFormulas, xlErrors).Areas
.Replace "=XX", "", xlPart, , False, , False, False
For i = 1 To Ar.Count - 1
Worksheets.Add , Sheets(Sheets.Count)
Ar(i).Resize(Ar(i + 1).Row - 1).EntireRow.Copy ActiveSheet.Range("A1")
Next i
End With
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I have removed the quote of your first post from the second as it does not add any further information and just makes the post harder to navigate/read.

<del>To your question: I can't really see what is going wrong with your current code. What columns is that sample data from post#1 in? If there is also data in other columns, can you dummy up a another small set of sample data and show us what the results are and what they should be so we can understand.</del>

Edit: Never mind, I can now see the problem. I'll be back shortly.
 
Last edited:
Upvote 0
Try replaceing the first line below with the second.
Rich (BB code):
<del>Ar(i).Resize(Ar(i + 1).Row - 1).EntireRow.Copy ActiveSheet.Range("A1")</del>
Ar(i).Resize(Ar(i + 1).Row - Ar(i).Row).EntireRow.Copy ActiveSheet.Range("A1")
 
Last edited:
Upvote 0
Perfect! I knew it was something very silly! Just i needed a fresh pair of eyes! Thanking you sir!
 
Upvote 0

Forum statistics

Threads
1,224,837
Messages
6,181,255
Members
453,028
Latest member
letswriteafairytale

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