VBA help

sandejos

New Member
Joined
May 26, 2015
Messages
7
Hi Friends,
Hi need small help from you. I have a data in column only but i need to bifurcate in different columns please see below example and help me how to write the code in VBA.
My Input Output Would be
Column AColumn BColumn CColumn DColumn EColumn E
NameNameContact 1Contact 2Designation
Contact 1ABC91253925399859233617Manager
Contact 2CDE91254477219921378251Sr Manager
DesignationGHI97236445749423165959Director
ABC
9125392539
9859233617
Manager
CDE
9125447721
9921378251
Sr Manager
GHI
9723644574
9423165959
Director

<tbody>
</tbody>

Thanks,
Sandeep
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
This example assumes:
1) Data on Sheet2, change as needed.
2) Data starts at A1

Code:
Sub bifurcator()
Dim rowloop As Long
Dim colloop As Long
rowloop = 2
colloop = 3
lastrow = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
For x = 1 To lastrow
'make column headers first
If x < 5 Then
Sheet2.Cells(1, x + 2) = Sheet2.Cells(x, 1)
End If
'move data to new columns
If x >= 5 Then
Sheet2.Cells(rowloop, colloop) = Sheet2.Cells(x, 1)
colloop = colloop + 1
If colloop > 6 Then
colloop = 3
rowloop = rowloop + 1
End If
End If
Next x
End Sub
 
Upvote 0
Another approach...

Code:
Sub TransposeColumns()
Dim i As Long, j As Long, k As Long, Steps As Long
Dim arr() As Variant

On Error GoTo errHandler
arr = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
Steps = InputBox(prompt:="Number of rows to transpose in steps:", Default:=4)
j = 1
k = 1
For i = LBound(arr, 1) To UBound(arr, 1)
    For j = 3 To 3 + Steps - 1
        Cells(k, j) = arr(i, 1)
        i = i + 1
    Next j
    i = i - 1
    k = k + 1
Next i
errHandler:
    If Err.Number <> 0 Then Debug.Print Err.Number & ", " & Err.Description
    Columns.AutoFit
End Sub

Cheers,

tonyyy
 
Last edited:
Upvote 0
sandejos,

Here is another macro solution for you to consider, that uses two arrays in memory, and, will adjust to the number of raw data rows, and, should be very fast.

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

Sample raw data, and, results:


Excel 2007
ABCDEF
1NameNameContact 1Contact 2Designation
2Contact 1ABC91253925399859233617Manager
3Contact 2CDE91254477219921378251Sr Manager
4DesignationGHI97236445749423165959Director
5ABC
69125392539
79859233617
8Manager
9CDE
109125447721
119921378251
12Sr Manager
13GHI
149723644574
159423165959
16Director
17
Sheet1



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, 01/17/2017, ME986085
Dim a As Variant, i As Long, lr As Long, nlr As Long
Dim o As Variant, j As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  nlr = Application.Ceiling(lr, 4)
  a = .Range("A1:A" & nlr)
  ReDim o(1 To (nlr / 4), 1 To 4)
  For i = 1 To UBound(a, 1) Step 4
    j = j + 1
    o(j, 1) = a(i, 1): o(j, 2) = a(i + 1, 1): o(j, 3) = a(i + 2, 1): o(j, 4) = a(i + 3, 1)
  Next i
  .Cells(1, 3).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns(3).Resize(, 4).AutoFit
End With
Application.ScreenUpdating = True
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
You're welcome. Glad it worked out...
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,894
Members
449,477
Latest member
panjongshing

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