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:

[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[TD]K[/TD]
[TD]L[/TD]
[TD]M[/TD]
[TD]N[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]text[/TD]
[TD]data1[/TD]
[TD]data2[/TD]
[TD]data3[/TD]
[TD]data4[/TD]
[TD]data5[/TD]
[TD]data6[/TD]
[TD]data7[/TD]
[TD]data8[/TD]
[TD]data9[/TD]
[TD]text[/TD]
[TD]data10[/TD]
[TD]data11[/TD]
[TD]data12[/TD]
[/TR]
</tbody>[/TABLE]




In a new Worksheet after macro:

[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]text[/TD]
[TD]data1[/TD]
[TD]data2[/TD]
[TD]data3[/TD]
[TD]data4[/TD]
[TD]data5[/TD]
[TD]data6[/TD]
[TD]data7[/TD]
[TD]data8[/TD]
[TD]data9[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]text[/TD]
[TD]data10[/TD]
[TD]data11[/TD]
[TD]data12[/TD]
[TD]data13[/TD]
[TD]data14[/TD]
[TD]data15[/TD]
[TD]data16[/TD]
[TD]data17[/TD]
[TD]data18[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]text[/TD]
[TD]data19[/TD]
[TD]data20[/TD]
[TD]data21[/TD]
[TD]data22[/TD]
[TD]data23[/TD]
[TD]data24[/TD]
[TD]data25[/TD]
[TD]data26[/TD]
[TD]data27[/TD]
[/TR]
</tbody>[/TABLE]



Any help would be appreciated!
Thanks,
Stan
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,223,712
Messages
6,174,031
Members
452,542
Latest member
Bricklin

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