Excel

Transpose Data Without Paste Special; Create Link to Original Data

Ease of Use

Intermediate

Version tested with

2000 

Submitted by:

mdmackillop

Description:

Enters formulae to link transposed data to original rows or columns. 

Discussion:

There are any number of reasons you might want to use this macro; everyone wants to transpose data for one reason or another. But this lets you link it too! Requires a Userform with two RefEdit boxes and a Command Button. 

Code:

instructions for use

			

Option Explicit Private Sub CommandButton1_Click() Dim Source$, Destination$, MyPos!, MyEndStr!, MySht$ Dim ChkSht1$, ChkSht2$ Dim SR$, SC$, ER$, EC$, i!, j! 'Check that Source is valid On Error Resume Next Source = Range(RefEdit1).AddressLocal If Err <> 0 Then MsgBox "You have entered an invalid starting range.", vbCritical, "Input Error" SendKeys "{tab}" Exit Sub End If On Error GoTo 0 'Check that Destination is valid On Error Resume Next Destination = Range(RefEdit2).AddressLocal If Err <> 0 Then MsgBox "You have entered an invalid destination range.", vbCritical, "Input Error" SendKeys "{tab}{tab}" Exit Sub End If On Error GoTo 0 'Check source is greater than one cell MyPos = InStr(Source, ":") If MyPos = 0 Then MsgBox "You must select more than one cell for the Source Range.", vbCritical, "Input Error" SendKeys "{tab}" Exit Sub End If 'Check destination is not greater than one cell If InStr(Destination, ":") > 0 Then MsgBox "You must select only one cell for the Destination.", vbCritical, "Input Error" SendKeys "{tab}{tab}" Exit Sub End If 'Get Source sheet name if Destination on a different sheet ChkSht1 = Left(RefEdit1, InStr(RefEdit1, "!")) ChkSht2 = Left(RefEdit2, InStr(RefEdit2, "!")) MySht = "" If ChkSht1 <> ChkSht2 Then MySht = ChkSht1 'Get Row and Column numbers for start and end of Source MyEndStr = Len(Source) - MyPos SR = Range(Left(Source, MyPos - 1)).Row() SC = Range(Left(Source, MyPos - 1)).Column() ER = Range(Right(Source, MyEndStr)).Row() EC = Range(Right(Source, MyEndStr)).Column() 'Write formulae into new transposed location For j = 0 To (ER - SR) For i = 0 To (EC - SC) Range(RefEdit2).Offset(i, j).FormulaR1C1 = "=" & MySht & "R" & SR + j & "C" & SC + i Next Next Unload UserForm1 End Sub '////////////////////////////////////////////////////////////////////////////////////////////////////////// 'Place this routine in your Personal.xls file standard module Sub ShowTrans() UserForm1.Show End Sub '//////////////////////////////////////////////////////////////////////////////////////////////////////////

How to use:

  1. Place the 'ShowTrans' routine in a module in Personal.xls
  2. Create a UserForm in Personal.xls with two RefEdit boxes and a Command Button; Copy and paste the above code into the UserForm module.
  3. Create a Button or create a shortcut to run ShowTrans.
  4. Open the UserForm, select the source data to be transposed.
  5. Select the second RefEdit box, then select the top-left cell of the target range
  6. Click the Command button to insert formula into the target area, referring back to the source.
 

Test the code:

  1. See sample file.
  2. Tools | Macro | Macros - Select ShowTrans and press Run.
 

Sample File:

transpose.zip 13.73KB 

Approved by mdmackillop


This entry has been viewed 465 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express