If cell contains "specific text", then cut that specific cell and the next 9 cells to new row

mac888

New Member
Joined
Jan 5, 2011
Messages
3
Hi Everyone,

I am new to writing macros.
I am trying to figure out a macro to cut a specific cell and the next 9 cells to a new row.
Tried the macro recorder but don't know how to get it to continue to the end of the row until there is no more data.

The scenario is a sheet with 1 row that contains hundreds of columns with data.
In that row, there is a "common text" in a cell and then 9 more cells of data, and this pattern reoccurs.

The worksheet before:

ABCDEFGHIJKLMN
1textdata1data2data3data4data5data6data7data8data9textdata10data11data12

<tbody>
</tbody>




In a new Worksheet after macro:

ABCDEFGHIJ
1textdata1data2data3data4data5data6data7data8data9
2textdata10data11data12data13data14data15data16data17data18
3textdata19data20data21data22data23data24data25data26data27

<tbody>
</tbody>



Any help would be appreciated!
Thanks,
Stan
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Code:
r = 1: c = 1
For i = 1 To Worksheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
    Worksheets("Sheet2").Cells(r, c) = Worksheets("Sheet1").Cells(1, i)
    c = c + 1
    If c = 11 Then
        r = r + 1
        c = 1
    End If
Next

Rename Sheet1 and Sheet2 if needed.
 
Upvote 0
mac888,

Here is another macro for you to consider that will adjust for the varying number of columns in row 1 in the raw data worksheet.

You can change the raw data worksheet name in the macro.

After the macro (using two arrays in memory) in a new worksheet Results:


Excel 2007
ABCDEFGHIJK
1textdata1data2data3data4data5data6data7data8data9
2textdata10data11data12data13data14data15data16data17data18
3textdata19data20data21data22data23data24data25data26data27
4
Results


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgData()
' hiker95, 03/17/2015, ME842984
Dim w1 As Worksheet, wr As Worksheet
Dim a As Variant, o As Variant
Dim i As Long, j As Long
Dim lc As Long, nlc As Long, n As Long, c As Long
Set w1 = Sheets("Sheet1")     '<-- change the raw data worksheet name here
With w1
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  n = 10
  nlc = Application.Ceiling(lc, n)
  a = .Range(.Cells(1, 1), .Cells(1, nlc))
  ReDim o(1 To UBound(a, 2) / n, 1 To 10)
End With
For c = 1 To UBound(a, 2) Step 10
  j = j + 1
  o(j, 1) = a(1, c): o(j, 2) = a(1, c + 1)
  o(j, 3) = a(1, c + 2): o(j, 4) = a(1, c + 3)
  o(j, 5) = a(1, c + 4): o(j, 6) = a(1, c + 5)
  o(j, 7) = a(1, c + 6): o(j, 8) = a(1, c + 7)
  o(j, 9) = a(1, c + 8): o(j, 10) = a(1, c + 9)
Next c
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Sheets("Results")
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Cells(1, 1).Resize(UBound(o, 1)).Font.Bold = True
  .Columns(1).Resize(, 10).AutoFit
  .Activate
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorgData macro.
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,794
Members
449,048
Latest member
greyangel23

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