cba copy and sort calling a sub

ciskoh

New Member
Joined
Mar 23, 2015
Messages
1
Hi all,
VBA newbie here.
I am trying to copy a range (three columns, varying number of rows) in a second worksheet without blanks and sorting the data according to one of the columns (integers). I am trying to do this with 1 sub to define the ranges and a second to actually do the sorting. However I always run into an "error 1004". I guess there is an error in the setting of range but I cannot spot it. Already tried using autofilter and with for the worksheet but with no results. Thanks for any suggestion

Code:
Private Sub Worksheet_Deactivate()


'sort and copy values from q21 to result_2


Dim wB As Workbook
Dim ws1 As Worksheet  '<--source worksheet
Dim ws2 As Worksheet  '<--dest. worksheet
Dim r1 As Range   '<--source range to copy
Dim r2 As Range   '<--dest range
Dim r21 As Range    '<--key for sorting
Set wB = Workbooks("Resilience-analysis.xlsm")


With wB
Set ws1 = .Sheets("RQ section 2")
Set ws2 = .Sheets("Results 2- Evolutions")
End With




Set r1 = ws1.Range("AJ6:AM28")




Set r2 = ws2.Range("B9")
Set r21 = ws2.Range("D9")




cas ws1:=ws1, ws2:=ws2, r1:=r1, r2:=r2, r21:=r21
End Sub




Private Sub cas(ws1 As Worksheet, ws2 As Worksheet, r1 As Range, r2 As Range, r21 As Range)




'macro for actual sorting of tables




 
    ws1.Activate
    r1.Select
    Selection.Copy
    
    'End With
    
    
    ws2.Activate
    ws2.AutoFilterMode = False
    r2.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=True, Transpose:=False
    Selection.Sort key1:=r21
    Selection.WrapText = True
    Selection.AutoFit
    


    
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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