VBA to sort particular column in ascending order after I get the data from another sheet

ameerafi

New Member
Joined
Feb 11, 2020
Messages
3
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I need to get data from another worksheet with multiple criteria so I wrote a VBA. Now once I get all the required data in the new sheet "FI" then the Column D from D2 till last row has to be sorted in Ascending order. Please help me with the code.

/code

Private Sub CommandButton1_Click()

lastRow = Worksheets("E-Dashboard").Range("A" & Rows.Count).End(xlUp).Row

For r = 2 To lastRow

If (Worksheets("E-Dashboard").Range("AA" & r).Value = "1" Or Worksheets("E-Dashboard").Range("AA" & r).Value = "2" Or Worksheets("E-Dashboard").Range("AA" & r).Value = "3") Then

Worksheets("E-Dashboard").Range("B" & r & ":C" & r & ":D" & r & ":E" & r & ",G" & r & ",J" & r & ":K" & r & ":L" & r & ",AI" & r & ":AJ" & r & ":AK" & r).Copy

Worksheets("FI").Activate

lastrowFI = Worksheets("FI").Range("A" & Rows.Count).End(xlUp).Row

Worksheets("FI").Range("A" & lastrowFI + 1).Select

ActiveSheet.Paste

End If

Next r

End Sub

/code
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello Ameerafi,

I hope you don't mind but I've re-written your code with the criteria placed in an array and then filtering on the array as follows:-

VBA Code:
Private Sub CommandButton1_Click()

Dim wsE As Worksheet: Set wsE = Sheets("E-Dashboard")
Dim wsFI As Worksheet: Set wsFI = Sheets("FI")
Dim ar As Variant

ar = Array(1, 2, 3)
 
Application.ScreenUpdating = False

 For i = 0 To UBound(ar)
        With wsE.[A1].CurrentRegion
                .AutoFilter 27, ar(i)
                Union(.Columns("B:E"), .Columns("G"), .Columns("J:L"), .Columns("AI:AK")).Offset(1).Copy
                wsFI.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlValues
                .AutoFilter
        End With
 Next i
  
wsFI.Range("A2", wsFI.Range("K" & wsFI.Rows.Count).End(xlUp)).Sort wsFI.[D2], 1

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub

You should find this more efficient than your loop type code especially if you have a large data set.

The code will also sort the pasted data (entire rows) in Sheet FI based on Column D, ascending.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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