vba at each change

panyagak

Active Member
Joined
Feb 24, 2017
Messages
299
Hi all.

Microsoft 2010 in use

This is simplified data (Range to last cell on column A) arranged vertically for ease of presentation.

i would like to Transform the arrangement using vba possibly using "At each change in DIM" convert to this horizontally To This:

DIM 1 2 3 4 5 6
DIM 7 8 9 10 11 12
etc up to last cell.

My data as simplified:
DIM
1
2
3
4
5
6
DIM
7
8
9
10
11
12
DIM
13
14
15
16
17
18
DIM
.
.
.

Thanks as I prepare to sleep

Regards
Patrick
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
How are the Columns set up? Is the DIM 1 2 3 4 5 6 all in one cell, or is the DIM in Column1, 1 in Column2, 2 in Column3, etc.?
 
Upvote 0
How are the Columns set up? Is the DIM 1 2 3 4 5 6 all in one cell, or is the DIM in Column1, 1 in Column2, 2 in Column3, etc.?

DIM in Column1, 1 in Column2, 2 in
Column3, etc.?

Even in one Cell is ok, I can separate.

thanks
 
Upvote 0
I need to know how the data is arranged so the code will work. Look at the examples and tell me how it is.
Book1
AB
1DIM 1 2 3 4 5
Sheet1

Book1
ABCDEF
1DIM12345
Sheet1
 
Upvote 0
See if either of these works for you (both options given)
VBA Code:
Sub In_One_Cell()
    Dim a, b, i As Long, j As Long, s As String
    a = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    s = "DIM": j = 1
    For i = 2 To UBound(a, 1)
        If a(i, 1) <> "DIM" Then
            s = s & " " & a(i, 1)
        Else
            b(j, 1) = s: j = j + 1: s = "DIM"
        End If
    Next i
    
    Range("A1").Resize(UBound(b, 1), 1).Value = b
End Sub

VBA Code:
Sub In_Multi_Cells()
    Dim a, b, i As Long, j As Long, k As Long
    a = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
    ReDim b(1 To UBound(a, 1), 1 To 7)
    
    j = 1: k = 1
    b(j, k) = "DIM": k = k + 1
    For i = 2 To UBound(a, 1)
        If a(i, 1) <> "DIM" Then
            b(j, k) = a(i, 1)
            k = k + 1
        Else
            j = j + 1: k = 1
            b(j, k) = "DIM": k = k + 1
        End If
    Next i
    
    Range("A1").Resize(UBound(b, 1), 7).Value = b
End Sub
 
Upvote 0
See if either of these works for you (both options given)

VBA Code:
Sub In_Multi_Cells()
    Dim a, b, i As Long, j As Long, k As Long
    a = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
    ReDim b(1 To UBound(a, 1), 1 To 7)
  
    j = 1: k = 1
    b(j, k) = "DIM": k = k + 1
    For i = 2 To UBound(a, 1)
        If a(i, 1) <> "DIM" Then
         [B] [B]  b(j, k) = a(i, 1)[/B][/B]
            k = k + 1
        Else
            j = j + 1: k = 1
            b(j, k) = "DIM": k = k + 1
        End If
    Next i
  
    Range("A1").Resize(UBound(b, 1), 7).Value = b
End Sub
On Running there is a Error 9, on Debugging the VBA Line in bold is highlighted
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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