Solved

Macro to paste and transpose data

Posted on 2014-10-01
9
795 Views
Last Modified: 2014-10-07
I have recorded a macro and the code is as follows:

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+z
'
    Windows("My file.xls").Activate
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
End Sub

Open in new window


How do i make the above code generic as in it can be used for pasting and transposing data in all files, ie whatever i copy when i use the macro i can paste and transpose data in any other files of different names ?
0
Comment
Question by:ceneiqe
  • 5
  • 4
9 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40355287
Sub PasteTranspose()
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub

Open in new window


I HIGHLY RECOMMEND that you assign this to another shortcut because [Ctrl]+[z] is the Undo function - very important!

Also, note that if you use this macro to do a transposed paste, no Undo will be possible as the macro clears the Undo stack.

Frankly, it's better to just memorize and use the keyboard shortcut sequence:
Excel 2010/2013: [Alt],[H],[V],[T]
Excel 2003/2007: [Alt],[E],[S],[E]

Regards,
-Glenn
0
 

Author Comment

by:ceneiqe
ID: 40356242
Thanks.
I will change the shortcut to Ctrl +D

Excel 2010/2013: [Alt],[H],[V],[T]
- How does this work ?
I tried using this but it does not work, it pasted and transpose zeros "0" for me instead of the data.

I would prefer a macro in this case as it is also very tedious to use 4 keys (ALT H V T).
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40356273
Hi, you just press those four keys in succession (no simultanous keys, like [Ctrl]+[C] for example).

One macro:  two keystrokes, no Undo
Shortcut keys; four keystrokes, Undo available

Take your pick! :-)
0
 

Author Comment

by:ceneiqe
ID: 40363025
Excel 2010/2013: [Alt],[H],[V],[T]

nothing happens when i press the 4 keys together.

i tried to work around by having 4 modules of the same macro - assigning the same short cut key Ctrl+a but in the macro i place different file name so that whenever the file is active, it should do the job.
But it doesn't work.

For example, even thought file P.xls is activated, the error is at :

Windows("P.xls").Activate

Did i do something wrong?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40363040
You don't press the four keys together, you press them in succession - one at a time.

If you want this transposition macro to be universally available, you would save it in your Personal Macro Workbook (Personal.xlsb) and you would not include any code that specifically refers to any other workbook (ex. Windows("P.xls").Activate).

Regards,
-Glenn
0
 

Author Comment

by:ceneiqe
ID: 40363837
ops sorry. overlooked what you have typed.

Yes i want the transposition macro to be universally available and that's what i did - save it in my Personal Macro Workbook.

But when i use the same code for different worksheet, the macro doesn't recognise the active worksheet, so I rename the active worksheet in the code and it works.

Then I created the 4 macro to save time in future as I will be using the same P T V W files again, using the same short cut ctrl+a.

1.
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+a
'
    Windows("P.xls").Activate
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
End Sub
            

Open in new window


2.

Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+a
'
    Windows("T.xls").Activate
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
End Sub

Open in new window


3.
Sub Macro3()
'
' Macro3 Macro
'
' Keyboard Shortcut: Ctrl+a
'
    Windows("V.xls").Activate
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
End Sub

Open in new window



4.
Sub Macro4()
'
' Macro4 Macro
'
' Keyboard Shortcut: Ctrl+a
'
    Windows("W.xls").Activate
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
End Sub 

Open in new window



I tested T.xls file.
but when i enter ctrl+a in T.xls, the following error occur:

Run-time error '9' :
Subscript out of range

when clicked Debug, the following is highlighted.

Windows("P.xls").Activate

Open in new window


which means, the program doesn't recognise the macro as it jumped to macro 2 (refering to P.xls) and not macro3 (for T.xls)

I suspect this is due to the same shortcut key that i used, but i don't wish to use 4 different shortcut keys.


Then i tried the alt H V T method in a macro by recording the macro when I use the short cut keys

Sub Macro6()
'
' Macro6 Macro
'
' Keyboard Shortcut: Ctrl+d
'
    Range("A29").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
End Sub

Open in new window


Ok it worked but not what i wanted.
It will stick to A29 as previously i transposed at A29.

What i want:
-Transpose
-Paste as Values
-able to Transpose and paste as values in any active file
-only 1 or minimal key strokes, ie (Alt F8, choose a specific macro to run) OR ctrl+[some alphabet in this case is 'a' since 'z' is for undo. 'd' is the next best alternative i chose as i am running tests concurrently as mentioned above.]
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40363990
Okay, you should only have this one subroutine in your Personal Macro Workbook:
Sub PasteTranspose()
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub

Open in new window


Note that there is no reference to any workbook or worksheet; it only executes the paste-transpose method.  You can put this in one module (remove any extras):
personal macro workbook - transpose sub
In order to run this code properly the following must be true:
1) You have to have already selected a range of cells and copied them
2) You have to have already positioned your cursor where you want to paste the transposed data.  You should only select one cell to guarantee a transposed result.

I also recommend - instead of [Ctrl]+[o] (shortcut for opening a new file) - use [Ctrl]+[Shift]+[T] (as in "T" for Transpose).  After adding the file to the PMW, you can re-assign the shortcut like so:
assign new shortcut keys
-Glenn
0
 

Author Comment

by:ceneiqe
ID: 40367604
Thanks. The code is all i need.

Just 1 question: Why do you recommend a Ctrl shift T ? and not Ctrl + a which I originally used?
What is the impact of using Ctrl+a (since i didn't use z as it is also a shortcut for undo) ?
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40367681
Hi,

The shortcut key combination of [Ctrl]+[a] is a built-in shortcut to "select all."  This is contextual, meaning you will select all objects in different aspects of the workbook (usually it selects all cells in a sheet).

Here is a very good reference to Microsoft shortcuts; I find it invaluable and use it regularly.
Keyboard shortcuts in Excel 2010

-Glenn
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now