I've searched on this problem for quite awhile and can't find a relevant answer.
I'm trying to select a column on a worksheet, sort by the values in the column (high to low), copy the values in A2:A25 to another worksheet...then move over a few columns, sort by the values there, and copy the values in column A to another worksheet again...etc.
Probably easier to understand with a fictionalized example:
There is text in column A and different values for each country in columns to the right. The idea is to sort the spreadsheet by a country, then copy the text in column A to another place. Repeat for each country.
The issue I'm running into is setting the Range in the Sort.Sortfields.Add.Key command by something other than a direct reference.
I would like to update this range via a variable in a For loop. This is the code at this point:
I see how in other situations you can change a Range reference via a variable, but I haven't gotten any of them to work in this case.
I'm trying to select a column on a worksheet, sort by the values in the column (high to low), copy the values in A2:A25 to another worksheet...then move over a few columns, sort by the values there, and copy the values in column A to another worksheet again...etc.
Probably easier to understand with a fictionalized example:
Code:
Food England France Germany
Apples 10,000 15,000 20,000
Pears 5,000 11,000 8,000
Grapes 11,000 7,000 6,000
There is text in column A and different values for each country in columns to the right. The idea is to sort the spreadsheet by a country, then copy the text in column A to another place. Repeat for each country.
The issue I'm running into is setting the Range in the Sort.Sortfields.Add.Key command by something other than a direct reference.
I would like to update this range via a variable in a For loop. This is the code at this point:
Code:
Sub GeoCount_copy_paste()' Test for copying multiple sorted interests into one column
Dim GeoCount As Integer
Dim Index As Integer
Dim i As Integer
' Count the number of countries on another worksheet
GeoCount = Application.CountA(Worksheets(2).Columns(1))
Index = 1
For i = 1 To (GeoCount - 1) ' Accounts for header row
' ** Sort country by interest **
Sheets(1).Select
Range("A2").Activate
ActiveCell.Offset(0, 5 * Index).Select
' Range("H2").Select
ActiveWorkbook.Worksheets(1).Sort.SortFields.Clear
' This is the command I can't get to work with a variable instead of "F2"
ActiveWorkbook.Worksheets(1).Sort.SortFields.Add Key:=Range( _
"F2"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets(1).Sort
.SetRange Range("A2:DR101")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Copy the top 25 interests
Range("A2:A26").Select
Selection.Copy
' Go to second sheet
Sheets(2).Select
Range("C2").Select
ActiveSheet.Paste
Index = Index + 1 ' Increment the index for the next loop
Next i
End Sub
I see how in other situations you can change a Range reference via a variable, but I haven't gotten any of them to work in this case.
Last edited: