Results 1 to 2 of 2

Thread: Sort compiles but won't run

  1. #1

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

    Sort compiles but won't run

    I have a sort in VB.net that tries to sort two columns in Excel speadsheet. It compiles fine but won't execute. I get the error on selecting the range of the sort.

    Dim myRange As Excel.Range
    Dim sortstart As String
    Dim sortend As String
    sortstart = "E" & LastRow + 2 'E93
    sortend = "F" & LastScore 'F98
    myRange = xlWs.Range(sortstart, sortend)
    myRange.Select() '<--- Error
    myRange.Sort(Key1:=myRange.Range(sortend),
    Order1:=Excel.XlSortOrder.xlDescending,
    Orientation:=Excel.XlSortOrientation.xlSortColumns)

    Error: System.Runtime.InteropServices.comException: Select method of range class failed

    It looks like the "myRange.Select()" isn't working but the two valules that make up myRange are valid and good.

    Anyone have any ideas what I'm missing? Thanks

    Additionally here are the references: Imports System.Configuration
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Interop.Excel
    Imports Excel = Microsoft.Office.Interop.Excel
    Last edited by JimReid; Feb 1st, 2025 at 06:54 AM.

  2. #2
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    491

    Re: Sort compiles but won't run

    This worked okay for me, I used .net 8, you didn't specify your target and I have Office Professional 2021

    Code:
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Interop.Excel
    Imports Excel = Microsoft.Office.Interop.Excel
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            ' open Excel file, sort the range close and save it
            Dim xlApp As New Excel.Application
            Dim xlWB As Excel.Workbook
            Dim xlWS As Excel.Worksheet
            Dim xlSortRange As Excel.Range
    
            Try
                xlWB = xlApp.Workbooks.Open("F:\Helping people online\NamesToSort.xlsx")
                xlWS = xlWB.Sheets("Sheet1")
                xlSortRange = xlWS.Range("A2:B5")
                xlSortRange.Select()
                xlSortRange.Sort(Key1:=xlWS.Range("A2"),
                             Order1:=Excel.XlSortOrder.xlDescending,
                             Orientation:=Excel.XlSortOrientation.xlSortColumns)
    
                xlWB.Save()
    
                MessageBox.Show("Sort has finished")
            Catch ex As Exception
                MessageBox.Show($"An error has occured: {ex.Message}")
            Finally
                If xlWB IsNot Nothing Then xlWB.Close()
                xlApp.Quit()
    
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWS)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWB)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
            End Try
    
        End Sub
    End Class
    Before and after screenshots
    Attached Images Attached Images   

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