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
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