none
excel range in a datagrid RRS feed

  • Question

  • need help trying to show an especific cell range like A2:C2 till last row in a datagrid
    Monday, January 14, 2019 11:11 PM

Answers

  • Do you want a solution using OleDb or another avenue to perform this task?

    If OleDb look at my MSDN code sample which shows how to read a range with OleDb. I did a visual representation with controls but the magic is solely in the SELECT statement construction. To get the last row you need to read a subset or all data (it depends on your prior knowledge of the sheets or no knowledge of the sheets). 

    If using Excel automation the following shows a simple example for reading a range (see more after the code sample)

    Option Strict On
    Imports Excel = Microsoft.Office.Interop.Excel
    Module ExcelDemoIteratingData_2
        Public Sub DemoGettingDates()
    
    
            Dim dt As DataTable = OpenExcelAndIterate(
                IO.Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    "GetDatesFromB.xlsx"),
                "Sheet1",
                "B1",
                "B10")
    
            Dim SomeDate As Date = #12/1/2013#
    
            Dim Results =
                (
                    From T In dt
                    Where Not IsDBNull(T.Item("SomeDate")) AndAlso T.Field(Of Date)("SomeDate") = SomeDate
                    Select T
                ).ToList
    
            If Results.Count > 0 Then
                For Each row As DataRow In Results
                    Console.WriteLine("Row [{0}] Value [{1}]",
                                      row.Field(Of Integer)("Identifier"),
                                      row.Field(Of Date)("SomeDate").ToShortDateString)
                Next
            End If
    
        End Sub
        Public Function OpenExcelAndIterate(
            ByVal FileName As String,
            ByVal SheetName As String,
            ByVal StartCell As String,
            ByVal EndCell As String) As DataTable
    
            Dim dt As New DataTable
    
            If IO.File.Exists(FileName) Then
    
                Dim Proceed As Boolean = False
                Dim xlApp As Excel.Application = Nothing
                Dim xlWorkBooks As Excel.Workbooks = Nothing
                Dim xlWorkBook As Excel.Workbook = Nothing
                Dim xlWorkSheet As Excel.Worksheet = Nothing
                Dim xlWorkSheets As Excel.Sheets = Nothing
                Dim xlCells As Excel.Range = Nothing
    
                xlApp = New Excel.Application
                xlApp.DisplayAlerts = False
                xlWorkBooks = xlApp.Workbooks
                xlWorkBook = xlWorkBooks.Open(FileName)
    
                xlApp.Visible = False
    
                xlWorkSheets = xlWorkBook.Sheets
    
    
                '
                ' For/Next finds our sheet
                '
                For x As Integer = 1 To xlWorkSheets.Count
                    xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet)
    
                    If xlWorkSheet.Name = SheetName Then
                        Proceed = True
                        Exit For
                    End If
    
                    Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet)
                    xlWorkSheet = Nothing
    
                Next
    
                If Proceed Then
    
                    dt.Columns.AddRange(
                        New DataColumn() _
                        {
                            New DataColumn With {.ColumnName = "Identifier", .DataType = GetType(Int32), .AutoIncrement = True, .AutoIncrementSeed = 1},
                            New DataColumn With {.ColumnName = "SomeDate", .DataType = GetType(Date)}
                        }
                    )
    
                    Dim xlUsedRange = xlWorkSheet.Range(StartCell, EndCell)
    
                    Try
    
                        Dim ExcelArray(,) As Object = CType(xlUsedRange.Value(Excel.XlRangeValueDataType.xlRangeValueDefault), Object(,))
    
                        If ExcelArray IsNot Nothing Then
                            ' Get bounds of the array.
                            Dim bound0 As Integer = ExcelArray.GetUpperBound(0)
                            Dim bound1 As Integer = ExcelArray.GetUpperBound(1)
    
                            For j As Integer = 1 To bound0
                                If (ExcelArray(j, 1) IsNot Nothing) Then
                                    dt.Rows.Add(New Object() {Nothing, ExcelArray(j, 1)})
                                Else
                                    dt.Rows.Add(New Object() {Nothing, Nothing})
                                End If
                            Next
                        End If
                    Finally
                        ReleaseComObject(xlUsedRange)
                    End Try
    
                Else
                    MessageBox.Show(SheetName & " not found.")
                End If
    
                xlWorkBook.Close()
                xlApp.UserControl = True
                xlApp.Quit()
    
                ReleaseComObject(xlCells)
                ReleaseComObject(xlWorkSheets)
                ReleaseComObject(xlWorkSheet)
                ReleaseComObject(xlWorkBook)
                ReleaseComObject(xlWorkBooks)
                ReleaseComObject(xlApp)
            Else
                MessageBox.Show("'" & FileName & "' not located. Try one of the write examples first.")
            End If
    
            Return dt
    
        End Function
        Private Sub ReleaseComObject(ByVal sender As Object)
            Try
                If sender IsNot Nothing Then
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(sender)
                    sender = Nothing
                End If
            Catch ex As Exception
                sender = Nothing
            End Try
        End Sub
    End Module
    
    What is missing from the above is knowing the last used row, my MSDN code sample shows how to get this.

    There libraries that are easier but they are paid or open source (while some of the open source libraries are not complete for this such operation).


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, January 14, 2019 11:14 PM
    Moderator

All replies

  • Do you want a solution using OleDb or another avenue to perform this task?

    If OleDb look at my MSDN code sample which shows how to read a range with OleDb. I did a visual representation with controls but the magic is solely in the SELECT statement construction. To get the last row you need to read a subset or all data (it depends on your prior knowledge of the sheets or no knowledge of the sheets). 

    If using Excel automation the following shows a simple example for reading a range (see more after the code sample)

    Option Strict On
    Imports Excel = Microsoft.Office.Interop.Excel
    Module ExcelDemoIteratingData_2
        Public Sub DemoGettingDates()
    
    
            Dim dt As DataTable = OpenExcelAndIterate(
                IO.Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    "GetDatesFromB.xlsx"),
                "Sheet1",
                "B1",
                "B10")
    
            Dim SomeDate As Date = #12/1/2013#
    
            Dim Results =
                (
                    From T In dt
                    Where Not IsDBNull(T.Item("SomeDate")) AndAlso T.Field(Of Date)("SomeDate") = SomeDate
                    Select T
                ).ToList
    
            If Results.Count > 0 Then
                For Each row As DataRow In Results
                    Console.WriteLine("Row [{0}] Value [{1}]",
                                      row.Field(Of Integer)("Identifier"),
                                      row.Field(Of Date)("SomeDate").ToShortDateString)
                Next
            End If
    
        End Sub
        Public Function OpenExcelAndIterate(
            ByVal FileName As String,
            ByVal SheetName As String,
            ByVal StartCell As String,
            ByVal EndCell As String) As DataTable
    
            Dim dt As New DataTable
    
            If IO.File.Exists(FileName) Then
    
                Dim Proceed As Boolean = False
                Dim xlApp As Excel.Application = Nothing
                Dim xlWorkBooks As Excel.Workbooks = Nothing
                Dim xlWorkBook As Excel.Workbook = Nothing
                Dim xlWorkSheet As Excel.Worksheet = Nothing
                Dim xlWorkSheets As Excel.Sheets = Nothing
                Dim xlCells As Excel.Range = Nothing
    
                xlApp = New Excel.Application
                xlApp.DisplayAlerts = False
                xlWorkBooks = xlApp.Workbooks
                xlWorkBook = xlWorkBooks.Open(FileName)
    
                xlApp.Visible = False
    
                xlWorkSheets = xlWorkBook.Sheets
    
    
                '
                ' For/Next finds our sheet
                '
                For x As Integer = 1 To xlWorkSheets.Count
                    xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet)
    
                    If xlWorkSheet.Name = SheetName Then
                        Proceed = True
                        Exit For
                    End If
    
                    Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet)
                    xlWorkSheet = Nothing
    
                Next
    
                If Proceed Then
    
                    dt.Columns.AddRange(
                        New DataColumn() _
                        {
                            New DataColumn With {.ColumnName = "Identifier", .DataType = GetType(Int32), .AutoIncrement = True, .AutoIncrementSeed = 1},
                            New DataColumn With {.ColumnName = "SomeDate", .DataType = GetType(Date)}
                        }
                    )
    
                    Dim xlUsedRange = xlWorkSheet.Range(StartCell, EndCell)
    
                    Try
    
                        Dim ExcelArray(,) As Object = CType(xlUsedRange.Value(Excel.XlRangeValueDataType.xlRangeValueDefault), Object(,))
    
                        If ExcelArray IsNot Nothing Then
                            ' Get bounds of the array.
                            Dim bound0 As Integer = ExcelArray.GetUpperBound(0)
                            Dim bound1 As Integer = ExcelArray.GetUpperBound(1)
    
                            For j As Integer = 1 To bound0
                                If (ExcelArray(j, 1) IsNot Nothing) Then
                                    dt.Rows.Add(New Object() {Nothing, ExcelArray(j, 1)})
                                Else
                                    dt.Rows.Add(New Object() {Nothing, Nothing})
                                End If
                            Next
                        End If
                    Finally
                        ReleaseComObject(xlUsedRange)
                    End Try
    
                Else
                    MessageBox.Show(SheetName & " not found.")
                End If
    
                xlWorkBook.Close()
                xlApp.UserControl = True
                xlApp.Quit()
    
                ReleaseComObject(xlCells)
                ReleaseComObject(xlWorkSheets)
                ReleaseComObject(xlWorkSheet)
                ReleaseComObject(xlWorkBook)
                ReleaseComObject(xlWorkBooks)
                ReleaseComObject(xlApp)
            Else
                MessageBox.Show("'" & FileName & "' not located. Try one of the write examples first.")
            End If
    
            Return dt
    
        End Function
        Private Sub ReleaseComObject(ByVal sender As Object)
            Try
                If sender IsNot Nothing Then
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(sender)
                    sender = Nothing
                End If
            Catch ex As Exception
                sender = Nothing
            End Try
        End Sub
    End Module
    
    What is missing from the above is knowing the last used row, my MSDN code sample shows how to get this.

    There libraries that are easier but they are paid or open source (while some of the open source libraries are not complete for this such operation).


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, January 14, 2019 11:14 PM
    Moderator
  • thanks for your help i will look in that code but look mine this is what i am doing right now to show in a listbox

     Dim libro As Object
            Dim ApExcel As Object
            Dim dia As Object
            Dim path As Object = AppDomain.CurrentDomain.BaseDirectory & "\Files\" & ComboBox7.Text & ".xls"
            Dim hoja As Object

            listbox1.Items.Clear()
            listbox2.Items.Clear()
            listbox3.Items.Clear()
            listbox15.Items.Clear()
            listbox16.Items.Clear()
            listbox17.Items.Clear()


            ApExcel = CreateObject("Excel.application")
            ApExcel.Workbooks.Open(FileName:=path)



            dia = ComboBox8.Text
            ApExcel.Sheets(dia).Activate()
            hoja = ApExcel.Sheets(dia)
            libro = ApExcel.ActiveWorkBook



            With ApExcel.Sheets(dia).Activate

                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(3, 1).value & "   " & ApExcel.Sheets(dia).Cells(3, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(4, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(4, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(5, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(5, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(6, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(6, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(7, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(7, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(8, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(8, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(9, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(9, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(10, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(10, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(11, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(11, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(12, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(12, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(13, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(13, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(14, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(14, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(15, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(15, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(16, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(16, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(17, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(17, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(18, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(18, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(19, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(19, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(20, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(20, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(21, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(21, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(22, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(22, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(23, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(23, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(24, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(24, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(25, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(25, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(26, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(26, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(27, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(27, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(28, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(28, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(29, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(29, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(30, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(30, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(31, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(31, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(32, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(32, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(33, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(33, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(34, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(34, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(35, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(35, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(36, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(36, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(37, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(37, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(38, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(38, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(39, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(39, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(40, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(40, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(41, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(41, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(42, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(42, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(43, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(43, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(44, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(44, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(45, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(45, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(46, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(46, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(47, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(47, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(48, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(48, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(49, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(49, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(50, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(50, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(51, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(51, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(52, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(52, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(53, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(53, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(54, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(54, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(55, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(55, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(56, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(56, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(57, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(57, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(58, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(58, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(59, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(59, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(60, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(60, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(61, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(61, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(62, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(62, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(63, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(63, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(64, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(64, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(65, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(65, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(66, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(66, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(67, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(67, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(68, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(68, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(69, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(69, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(70, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(70, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(71, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(71, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(72, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(72, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(73, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(73, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(74, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(74, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(75, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(75, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(76, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(76, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(77, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(77, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(78, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(78, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(79, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(79, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(80, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(80, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(81, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(81, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(82, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(82, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(83, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(83, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(84, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(84, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(85, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(85, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(86, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(86, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(87, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(87, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(88, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(88, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(89, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(89, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(90, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(90, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(91, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(91, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(92, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(92, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(93, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(93, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(94, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(94, 3).Value)
                listbox2.Items.Add(ApExcel.Sheets(dia).Cells(95, 1).Value & vbTab & ApExcel.Sheets(dia).Cells(95, 3).Value)



            End With



            'libro.save()

            ApExcel.quit()

            libro = Nothing
            dia = Nothing
            path = Nothing
            hoja = Nothing


            'PostMessage(ApExcel.Hwnd, WM_QUIT, 0, 0)
            ApExcel = Nothing

    end sub

    Tuesday, January 15, 2019 12:31 AM
  • I would drop how you are currently working with Excel and learn from my recommendations. Excel is notorious for not working as expected when working how you are currently.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, January 15, 2019 12:56 AM
    Moderator