Results 1 to 6 of 6

Thread: Sorting excel in VB.net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Location
    Southern New Jersey
    Posts
    29

    Unhappy 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.

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Location
    Southern New Jersey
    Posts
    29

    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.

  4. #4
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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!

  5. #5
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Sorting excel in VB.net

    Quote Originally Posted by JimReid View Post
    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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Location
    Southern New Jersey
    Posts
    29

    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
  •  



Click Here to Expand Forum to Full Width