Results 1 to 3 of 3

Thread: Get values from Certain Cells in Excel Document

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Get values from Certain Cells in Excel Document

    I want to retrieve the value from example Cell C4 and dim it as String and then retrieve Cell D8 and dim as string etc.

    How do i do this the easy way?

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Get values from Certain Cells in Excel Document

    You will have to make references to the Excel library and use the Excel.WorkSheet.Range method to access the cells. I'm sure RobDog888's signature shows some simple examples of how to do this.

  3. #3
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Get values from Certain Cells in Excel Document

    Oddly enough, I just wrote something similar for debugging, maybe you can just adapt this.

    You need;
    Imports System.Runtime.InteropServices
    and add a reference for;
    Microsoft Excel 10.0 Object Library

    note sure if you actually need references for these, but these are set in my project;
    Microsoft Office 10.0 Object Library
    Microsoft Visual Basic for Applications Extensibility 5.3

    Code:
        Private Sub ReadExcelFields(ByVal FileName As String)
    
            Dim xlApp As Excel.Application = CreateObject("Excel.Application")
            xlApp.DisplayAlerts = False
            xlApp.Visible = False
    
            Dim xlBook As Excel.Workbook = xlApp.Workbooks.Open(FileName, False, True)
            Dim xlSheet As Excel.Worksheet = xlBook.Worksheets(1) 'Worksheet number
    
            Dim LastRow As Integer = xlSheet.UsedRange.Rows(xlSheet.UsedRange.Rows.Count).Row
            Dim LastCol As Integer = xlSheet.UsedRange.Columns(xlSheet.UsedRange.Columns.Count).Column
    
            With xlSheet.Application
                For row As Integer = 1 To LastRow
                    For col As Integer = 1 To LastCol
                        If Not IsNothing(.Cells(row, col).Value) Then
                            'Cell has a value;
                            MessageBox.Show(.Cells(row, col).Value.ToString())
                        Else
                            'Blank Cell
                        End If
                    Next
                Next
                .Quit()
            End With
    
            'Get rid of Excel objects
            If Not IsNothing(xlSheet) Then
                Marshal.ReleaseComObject(xlSheet)
                xlSheet = Nothing
            End If
            If Not IsNothing(xlBook) Then
                Marshal.ReleaseComObject(xlBook)
                xlBook = Nothing
            End If
            If Not IsNothing(xlApp) Then
                Marshal.ReleaseComObject(xlApp)
                xlApp = Nothing
            End If
    
        End Sub


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

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