Macro to paste and transpose data

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 ?
ceneiqeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Glenn RayExcel VBA DeveloperCommented:
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
ceneiqeAuthor Commented:
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
Glenn RayExcel VBA DeveloperCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

ceneiqeAuthor Commented:
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
Glenn RayExcel VBA DeveloperCommented:
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
ceneiqeAuthor Commented:
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
Glenn RayExcel VBA DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ceneiqeAuthor Commented:
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
Glenn RayExcel VBA DeveloperCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.