Results 1 to 3 of 3

Thread: How to Get Text Values from Multiple User Selected Cells in Excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2016
    Location
    Arizona
    Posts
    12

    How to Get Text Values from Multiple User Selected Cells in Excel

    Hello,
    I've looked online and found ways to get data from a range of cells, but what I need is to be able to get values from user selected cells. The selected cells may not be in the same column or row as each other. The user could select cells A5, B2, C7, and D3.
    How would I be able to let them select different cells like that?

    I'm using VB.Net in Visual Studio 2022, Windows 11.

    Thanks

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,525

    Re: How to Get Text Values from Multiple User Selected Cells in Excel

    Untested AI search results:

    Code:
    Imports Microsoft.Office.Interop.Excel
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim excelApp As Excel.Application = Nothing
            Dim workbook As Excel.Workbook = Nothing
            Dim selectedRange As Excel.Range = Nothing
    
            Try
                ' Get the running instance of Excel
                excelApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"), Excel.Application)
    
                ' Get the active workbook
                workbook = excelApp.ActiveWorkbook
    
                ' Get the selected range
                selectedRange = excelApp.Selection
    
                If selectedRange IsNot Nothing Then
                    For Each cell As Excel.Range In selectedRange
                        ' Get the value of the cell
                        Dim cellValue As Object = cell.Value
    
                        ' Display the value (e.g., in a ListBox or Console)
                        ListBox1.Items.Add(cellValue.ToString())
                    Next
                Else
                    MessageBox.Show("No cells are selected in Excel.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
    
            Catch ex As Exception
                MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Finally
                ' Release COM objects to prevent memory leaks
                If selectedRange IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(selectedRange)
                If workbook IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
                ' Note: Do not release excelApp if you want to keep Excel running.
                ' If you want to close Excel, uncomment the following:
                ' If excelApp IsNot Nothing Then excelApp.Quit()
                ' If excelApp IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
            End Try
        End Sub
    End Class
    Explanation:
    Imports: The Imports Microsoft.Office.Interop.Excel statement is crucial for accessing the Excel Object Model.
    Get Excel Application: System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") retrieves a reference to an already running instance of Excel. If Excel is not running, this will raise an error.
    Get Active Workbook: excelApp.ActiveWorkbook gets the currently active workbook in Excel.
    Get Selected Range: excelApp.Selection returns a Range object representing the currently selected cells in Excel.
    Iterate and Extract Values: The For Each cell As Excel.Range In selectedRange loop iterates through each cell within the selected range. cell.Value then retrieves the value of the individual cell.
    Error Handling and Cleanup: The Try...Catch...Finally block handles potential errors and ensures proper release of COM objects using System.Runtime.InteropServices.Marshal.ReleaseComObject to prevent memory leaks.
    Before running:
    Ensure you have a reference to the "Microsoft Excel Object Library" in your VB.NET project. You can add this by going to Project -> Add Reference -> COM -> Microsoft Excel Object Library.
    Have an Excel workbook open with some cells selected before running your VB.NET application.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2016
    Location
    Arizona
    Posts
    12

    Re: How to Get Text Values from Multiple User Selected Cells in Excel

    Hello, and thank you for responding. I tried the code you provided, but it keeps stopping at the "If selectedRange IsNot Nothing Then..."
    I have 4 cells selected, but it doesn't seem to get any info from the cells.

    "System.NullReferenceException: 'Object reference not set to an instance of an object.'

    cellValue was Nothing."

    Also, it had a problem with
    excelApp = CType(Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"), Excel.Application)
    The error shows "GetActiveObject is not a member of Marshal"

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