Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort data in a range with VBA
#1
Example:

Sub SortData()
    Dim sortRange As Range

    'Set the range to be sorted (assuming data starts from A1)
    Set sortRange = Range("A1").CurrentRegion

    'Sort the range in ascending order based on values in column A
    sortRange.Sort Key1:=sortRange.Columns(1), Order1:=xlAscending, Header:=xlYes
End Sub

Explanation:

The code defines a subroutine named SortData.
It declares a variable sortRange, which represents the range to be sorted. In this example, the range is set to the data starting from cell A1 using the CurrentRegion property.
The sortRange.Sort method is used to sort the range in ascending order.
Key1:=sortRange.Columns(1) specifies that the sorting should be based on values in column A (the first column of sortRange).
Order1:=xlAscending sets the sorting order to ascending.
Header:=xlYes indicates that the range has a header row, and sorting should include the header.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Format cells in a range with bold font and yellow background color Qomplainerz 0 782 07-27-2023, 11:26 AM
Last Post: Qomplainerz
  Copy data to another worksheet Qomplainerz 0 411 07-27-2023, 11:24 AM
Last Post: Qomplainerz
  Concatenate data in columns Qomplainerz 0 358 07-27-2023, 11:01 AM
Last Post: Qomplainerz
  Find a specific value in a range and replace it with a new value Qomplainerz 0 396 07-27-2023, 10:38 AM
Last Post: Qomplainerz
  Create a pivot table based on data in a worksheet with VBA Qomplainerz 0 424 07-27-2023, 09:48 AM
Last Post: Qomplainerz
  Filter data in a worksheet Qomplainerz 0 391 07-27-2023, 09:46 AM
Last Post: Qomplainerz
  Create a chart from data with VBA Qomplainerz 0 388 07-27-2023, 09:44 AM
Last Post: Qomplainerz
  Insert a new row and fill it with data in VBA Qomplainerz 0 354 07-27-2023, 09:11 AM
Last Post: Qomplainerz
  Add data validation to a cell with VBA Qomplainerz 0 363 07-27-2023, 09:10 AM
Last Post: Qomplainerz
  Copy data from one workbook to another with VBA Qomplainerz 0 341 07-27-2023, 08:02 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 2 Guest(s)