VBA - Copy values in variable and transpose

cozzagiorgi

New Member
Joined
Jun 27, 2018
Messages
41
Hi

I want to stock my range (lets say A6:A13) in to a variable. But the size of this range can change according to what the user puts in.
Later on I will take this variable, transpose it and paste it in the range A1:Z40

The code I have is:

Code:
Sub AuswahlErstellt()


Dim Proben() As Variant


varProben = Range("A6:A13").Value
Range("A17:Z17") = Application.WorksheetFunction.Transpose(varProben)


End Sub

Of course, the range A17:Z17 is much bigger than what I would need so it gives me #NV errors. How can i do that without those errors? I am sure there is a better solution for what I am doing... :-)
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
Sub AuswahlErstellt()
Dim Proben As Range: Set Proben = Range("A6:A13")
Range("A17").Resize(, Proben.Cells.Count) = Application.WorksheetFunction.Transpose(Proben)
End Sub
 
Upvote 0
That solves the problem of the #NV , thank you!
But what if I don't know how big the range A6:A13 will be? Can I set a "dynamic" range?
 
Upvote 0
But what if I don't know how big the range A6:A13 will be? Can I set a "dynamic" range?

It can be any size (in single column).
This handles it :

Range("A17").Resize(, Proben.Cells.Count) = Application.WorksheetFunction.Transpose(Proben)
 
Upvote 0
I am not clear, sorry.
The first line is: Dim Proben As Range: Set Proben = Range("A6:A13")
This way the code will only look at A6:A13, but what if that range gehts bigger?
 
Upvote 0
I am not clear, sorry.
The first line is: Dim Proben As Range: Set Proben = Range("A6:A13")
This way the code will only look at A6:A13, but what if that range gehts bigger?

You can set the range to whatever you want.
If you mean how to set a dynamic range :
Set rng=Range([A6],Cells(Rows.Count,"A").End(xlUp))

Also, if the range might cover more than one column then :
Code:
Sub AuswahlErstellt()
Dim Proben As Range: Set Proben = Range("A6:B13")
Range("A17").Resize(Proben.Columns.Count, Proben.Rows.Count) = Application.WorksheetFunction.Transpose(Proben)
End Sub
 
Last edited:
Upvote 0
Sorry if I still don't get it.
Of course I could set my range whatever I want. But I will not know how big this range will be. So instead of writing A6:A13 I need Excel to adapt to the number of used cells.
 
Upvote 0

Forum statistics

Threads
1,223,713
Messages
6,174,038
Members
452,542
Latest member
Bricklin

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