|
-
Jan 6th, 2025, 04:16 PM
#1
Thread Starter
Junior Member
Sorting excel in VB.net
I've done this in Excel macros but can't seem to get it to work in VB.net.
I get the following error: system.Runtime.InteropService.COMException: 'This can't be done on multiple range selection. Select a single range and try again.'
I tried a single range with the same results. Here is the code:
Code:
myRangeMakeup = "D" & StartScoreRow & ",F" & LastScoreRow
mySortKey = "D" & StartScoreRow
Debug.Print(myRangeMakeup) ' Prints D1065,F1123
Debug.Print(mySortKey) 'Prints D1065
myRange = xlWs.Range(myRangeMakeup)
' IS: D1065,F1123 and valid
myRange.Select()
myRange.Sort(Key1:=myRange.Range(mySortKey),
Header:=XlYesNoGuess.xlNo,
Order1:=XlSortOrder.xlAscending,
MatchCase:=False,
Orientation:=XlSortOrientation.xlSortColumns)
Debug prints the appropriate ranges.
Had one comment about a , instead of a : between the ranges but tried both with same results.
Also, the continuation underscore keeps disappearing when I put it in.
-
Jan 7th, 2025, 06:37 AM
#2
Re: Sorting excel in VB.net
you can define a Range in Excel like this
xlSt is your Worksheet
Code:
'....
'search in Column A
'Dim rngSearchValue As Range = xlSt.Range("A:A")
'or in Columns A to E
Dim rngSearchValue As Range = xlSt.Range("A:E")
'......
or use OLEDB with Sql which is easier IMO
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Jan 7th, 2025, 07:05 AM
#3
Thread Starter
Junior Member
Re: Sorting excel in VB.net
I understand that ChrisE but that isn't what I'm trying to do. I build the sort range based on what is before it on the spreadsheet so I don't know how many rows are involved until this point. The problem is that this is almost an exact copy of a sample that I was copying but it doesn't work because of the error I noted. Don't understand the error and why.
-
Jan 7th, 2025, 07:07 AM
#4
Re: Sorting excel in VB.net
Also, the continuation underscore keeps disappearing when I put it in.
That is the nature of Visual Studio now.
Please remember next time...elections matter!
-
Jan 7th, 2025, 07:58 AM
#5
Re: Sorting excel in VB.net
 Originally Posted by JimReid
I understand that ChrisE but that isn't what I'm trying to do. I build the sort range based on what is before it on the spreadsheet so I don't know how many rows are involved until this point. The problem is that this is almost an exact copy of a sample that I was copying but it doesn't work because of the error I noted. Don't understand the error and why.
it is irrelevant how many rows there are if you create the range like I pointed out.
what does your Sheet look like and what is to happen with the filtered/sorted Data ?
display in a Datagridview perhaps?
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Jan 7th, 2025, 08:28 AM
#6
Thread Starter
Junior Member
Re: Sorting excel in VB.net
ChrisE,
It is important the number of rows. I have 1000+ rows before the group I want to sort. This is why I build a range from 10?? to 11?? for the range of rows. The answer to my question was in the Key1 field. It should have been xlWs.range and not what I had.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|