Macro code - help!

markster

Well-known Member
Joined
May 23, 2002
Messages
575
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi guys

I have a problem in that I have some really large spreadsheets in the format of Sheet 1 shown below. I’m looking for some macro code to convert it easily into the format of Sheet 2 (in sheet 2 a new spreadsheet). I’m a real novice when it comes to VBA code so need something I can manipulate easily so I can change column ranges etc as need to reorder numerous sheets. I can do it manually but obviously it will take ages.

Can anyone help?
Thanks
M
Capital Ambition Financial Monitoring (Prototype).xls
ABCDEF
2Sheet1-Current
3P.BoardP.BoardM.BoardM.Board
4RefOrgName25/01/200627/03/200622/05/200624/07/2006
5114OrganisationA20,00020,000
6123OrganisationB20,000
7097OrganisationC30,000
8
9Sheet2-RequiredOutput
10RefOrganisationBoardTypeDateAmountApproved
11114OrganisationAP.Board25/01/0620,000
12114OrganisationAM.Board22/05/0620,000
13123OrganisationBP.Board27/03/0620,000
14097OrganisationCP.Board27/03/0620,000
Sheet1
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
markster,

Before the macro:


Excel Workbook
ABCDEF
3P.BoardP.BoardM.*BoardM.*Board
4RefOrg*Name25/01/200627/03/200622/05/200624/07/2006
5114Organisation*A*20,00020,000
6123Organisation*B*20,000
797Organisation*C*30,000
Sheet1



Excel Workbook
ABCDE
1RefOrganisationBoard*TypeDateAmount*Approved
2
3
4
5
Sheet2



After the macro:


Excel Workbook
ABCDE
1RefOrganisationBoard*TypeDateAmount*Approved
2114Organisation*A*P.Board25/01/200620,000
3114Organisation*A*M.*Board22/05/200620,000
4123Organisation*B*P.Board27/03/200620,000
597Organisation*C*P.Board27/03/200630,000
Sheet2




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).

Press and hold down the 'ALT' key, and press the 'F11' key.

On the 'Insert' menu, click 'Module'.

Copy the below code, and paste it into the Module (on the right pane).


Code:
Option Explicit
Sub MoveData()
  Dim LR As Long, NR As Long, i As Long, j As Long, k As Long
  Dim rng As Range
  Dim ws1 As Worksheet, ws2 As Worksheet
  Application.ScreenUpdating = False
  Set ws1 = Sheets("Sheet1")
  Set ws2 = Sheets("Sheet2")
  NR = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1
  With ws1
    .Select
    LR = .Range("B" & Rows.Count).End(xlUp).Row
    For i = 5 To LR Step 1
      Set rng = .Range("C" & i & ":F" & i)
      j = Application.WorksheetFunction.CountA(rng)
      If j <> 0 Then
        .Range("A" & i & ":B" & i).Copy ws2.Range("A" & NR & ":B" & NR + j - 1)
        For k = 3 To 6 Step 1
          If .Cells(i, k) <> "" Then
              .Range(Cells(3, k), Cells(4, k)).Copy
              With ws2.Cells(NR, 3)
                .PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
              End With
              .Cells(i, k).Copy ws2.Cells(NR, 5)
              NR = NR + 1
          End If
        Next k
      End If
    Next i
  End With
  With ws2
    .Select
    .Range("F1").Select
  End With
  Application.ScreenUpdating = True
End Sub


Then run the "MoveData" macro.


Have a great day,
Stan
 
Upvote 0
Hi there. One thing - I provided a shortened version here but some of my data has 50 columns. I need to expand the ranges. Will try to work how how to do this myself but any help would be appreciated.

Thanks again.

M
 
Upvote 0
markster,

I was wondering if you had more columns. This would require a change to the inner loop.

See my Private Message to you (top right hand corner of MrExcel, Welcome, markster., "Your Notifications:".


Have a great day,
Stan
 
Upvote 0
markster,

Sent your workbook back with the newly updated macro.

The new "MoveData" macro will work with "varying numbers of columns and rows of source data".

Code:
Option Explicit
Sub MoveData()
  Dim LR As Long, NR As Long, i As Long, j As Long, k As Long, LC As Long
  Dim ColName As String
  Dim rng As Range
  Dim ws1 As Worksheet, ws2 As Worksheet
  Application.ScreenUpdating = False
  Set ws1 = Sheets("Sheet1")
  Set ws2 = Sheets("Sheet2")
  NR = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1
  With ws1
    .Select
    LR = .Range("B" & Rows.Count).End(xlUp).Row
    LC = Cells(4, Columns.Count).End(xlToLeft).Column
    ColName = Replace(Cells(1, LC).Address(0, 0), 1, "")
    For i = 5 To LR Step 1
      Set rng = .Range("C" & i & ":" & ColName & i)
      j = Application.WorksheetFunction.CountA(rng)
      If j <> 0 Then
        .Range("A" & i & ":B" & i).Copy ws2.Range("A" & NR & ":B" & NR + j - 1)
        For k = 3 To LC Step 1
          If .Cells(i, k) <> "" Then
              .Range(Cells(3, k), Cells(4, k)).Copy
              With ws2.Cells(NR, 3)
                .PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
              End With
              .Cells(i, k).Copy ws2.Cells(NR, 5)
              NR = NR + 1
          End If
        Next k
      End If
    Next i
  End With
  With ws2
    .Select
    .Range("F1").Select
  End With
  Application.ScreenUpdating = True
End Sub


Have a great day,
Stan
 
Upvote 0

Forum statistics

Threads
1,203,465
Messages
6,055,577
Members
444,799
Latest member
CraigCrowhurst

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