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
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Column A[/TD]
[TD]Column B[/TD]
[TD]Column C[/TD]
[TD]Column D[/TD]
[TD]Column E[/TD]
[TD]Column E [/TD]
[/TR]
[TR]
[TD]Name[/TD]
[TD][/TD]
[TD]Name[/TD]
[TD]Contact 1[/TD]
[TD]Contact 2[/TD]
[TD]Designation[/TD]
[/TR]
[TR]
[TD]Contact 1[/TD]
[TD][/TD]
[TD]ABC[/TD]
[TD]9125392539[/TD]
[TD]9859233617[/TD]
[TD]Manager[/TD]
[/TR]
[TR]
[TD]Contact 2[/TD]
[TD][/TD]
[TD]CDE[/TD]
[TD]9125447721[/TD]
[TD]9921378251[/TD]
[TD]Sr Manager[/TD]
[/TR]
[TR]
[TD]Designation[/TD]
[TD][/TD]
[TD]GHI[/TD]
[TD]9723644574[/TD]
[TD]9423165959[/TD]
[TD]Director[/TD]
[/TR]
[TR]
[TD]ABC[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9125392539[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9859233617[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Manager[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]CDE[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9125447721[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9921378251[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Sr Manager[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]GHI[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9723644574[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9423165959[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Director[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Thanks,
Sandeep
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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