counta and sorting a selecting in VBA

  • Thread starter Thread starter Legacy 93538
  • Start date Start date
L

Legacy 93538

Guest
Hi

I am trying to write a piece of code that first does a counta on how many rows in column A have text and then use that value to do a sort on columns A:AQ and then using the counta value to define the number of rows.

I hope this makes sense. Can anyone help me as i am not sure?

Code:
k = WorksheetFunction.CountA(workbook.Range("A:A"))
workbook.Sheets(1).Range ("A1:B" & k)

this is what i have so far but i dont think its correct.

Thanks

Jeskit
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
It would be like this

Code:
With Sheets(1)
    k = WorksheetFunction.CountA(.Range("A:A"))
    .Range("A1:AQ" & k).Sort 'sort parameters go here
End With
 
Upvote 0
hi

Code:
With Sheets(1)
    k = WorksheetFunction.CountA(.Range("A:A"))
    .Range("A1:AQ" & k).Sort Sort Key1:=Worksheets(1).Columns("B"), Order1:=xlDescending
End With

This is what i have but when i wrote it and click on the next line it came up with an error saying Expected: End of statement and hightlights Key1

But i am not sure why?
 
Upvote 0
You have Sort twice. Try

Code:
With Sheets(1)
    k = WorksheetFunction.CountA(.Range("A:A"))
    .Range("A1:AQ" & k).Sort Key1:=.Columns("B"), Order1:=xlDescending
End With
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

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