Results 1 to 18 of 18

Thread: VB.net Excel

  1. #1

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

    VB.net Excel

    I have been trying to convert an Excel macro to VB.net but I'm having problems with the above commands. XlUp, XlDown, XltoRight, and XltoLeft. They all work in the macro but they all throw an error in VB.net. Is there a reference I'm not importing? I have the following Imports:

    Imports Microsoft.Office.Interop.Excel Imports Excel = Microsoft.Office.Interop.Excel

    If it is a missing Import is there a way to find out which Import is missing by the error?

    Thanks all.

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

    Re: VB.net Excel

    It might be helpful if you could post the VBA code where you are using these items.

  3. #3

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

    Re: VB.net Excel

    That would be ok except the VBA macro code is working. The VB.net is the one that isn't working.

  4. #4

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

    Re: VB.net Excel

    VBA macro code (works fine):
    LastRow = Range("A1").End(xlDown).Row
    LastCol = Range("A1:X1").End(xlToRight).Column

    VB.net code (doesn't work throws error):
    LastRow = xlWs.Cells(xlWs.Rows.Count, 1).End(xlDown).Row
    LastCol = xlWs.Cells(1, xlWs.Columns.Count).End(xlToRight).Column

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,842

    Re: VB.net Excel

    Quote Originally Posted by JimReid View Post
    ...They all work in the macro but they all throw an error in VB.net....
    By throw an error you mean that the code breaks when it runs or it doesn't compile?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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

    Re: VB.net Excel


  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,416

    Re: VB.net Excel

    You could create your own constants that store the values used in VBA:

    https://learn.microsoft.com/en-us/of...el.xldirection

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

    Re: VB.net Excel

    see if UsedRange works for you

    Code:
     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                Dim xlApp As New Microsoft.Office.Interop.Excel.Application()
                Dim xlWb As Microsoft.Office.Interop.Excel.Workbook
                xlWb = xlApp.Workbooks.Open("E:\TestFolder\vbexcel.xlsx")
                Dim xlSt As Microsoft.Office.Interop.Excel.Worksheet = CType(xlWb.Worksheets("Sheet1"), Worksheet)
    
                Dim ColRange = xlSt.UsedRange.Columns.Count 'count the used Columns      
                Dim rowRange = xlSt.UsedRange.Rows.Count 'count the used rows
    
                'show UsedRanges
                MessageBox.Show(CStr(ColRange))
                MessageBox.Show(CStr(rowRange))
            Catch g As Exception
                MessageBox.Show(g.ToString)
            End Try
        End Sub
    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.

  9. #9

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

    Re: VB.net Excel

    It doesn't compile
    Message says: "xlDown is not declared, it may be inaccessible due to its protection level"
    same message for xltoRight

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,708

    Re: VB.net Excel

    NuGet package SpreadsheetLight makes this task easy.

    Code:
    Public Shared Function GetLastRowColumn(ByVal fileName As String, ByVal sheetName As String) As (EndRowIndex As Integer, EndColumnIndex As Integer)
    	Using document As New SLDocument(fileName, sheetName)
    		Return (document.GetWorksheetStatistics().EndRowIndex, document.GetWorksheetStatistics().EndColumnIndex)
    	End Using
    End Function

  11. #11

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

    Re: VB.net Excel

    My issue isn't with VBA but with VB.net not being able to use xlUp or xlDown or xltoRight or xltoLeft. They all work with VBA but not with VB.net. I'm thinking that I have a reference missing or something similar but not sure what.

  12. #12

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

    Re: VB.net Excel

    It doesn't compile.

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

    Re: VB.net Excel

    I scanned our source code for xlDown and saw it used like this:

    xlWorkbook.Sheets(1).Rows(intRow1 & ":" & intRow1).Insert Shift:=xlDown

    rkbook.Sheets(1).Range("J1", xlWorkbook.Sheets(1).Range("J1").End(xlDown)).Columns.NumberFormat = "@"

    Maybe it will point you somewhere.
    Please remember next time...elections matter!

  14. #14

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

    Re: VB.net Excel

    Quote Originally Posted by kareninstructor View Post
    NuGet package SpreadsheetLight makes this task easy.

    Code:
    Public Shared Function GetLastRowColumn(ByVal fileName As String, ByVal sheetName As String) As (EndRowIndex As Integer, EndColumnIndex As Integer)
    	Using document As New SLDocument(fileName, sheetName)
    		Return (document.GetWorksheetStatistics().EndRowIndex, document.GetWorksheetStatistics().EndColumnIndex)
    	End Using
    End Function
    Don't want to get involved with 3rd party packages. Should be able to do this with just VB.net.

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

    Re: VB.net Excel

    use .Range rather than .Cells

    Code:
     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Try
                Dim xlApp As New Microsoft.Office.Interop.Excel.Application()
                Dim xlWb As Microsoft.Office.Interop.Excel.Workbook
                xlWb = xlApp.Workbooks.Open("E:\TestFolder\vbexcel.xlsx")
                Dim xlSt As Microsoft.Office.Interop.Excel.Worksheet = CType(xlWb.Worksheets("Sheet1"), Worksheet)
                Dim LastRow As Integer, LastCol As Integer
    
                With xlSt
                    LastCol = .Range("A1:X1").End(XlDirection.xlToRight).Column
                    LastRow = .Range("A" & .Rows.Count).End(XlDirection.xlUp).Row
                End With
    
                MessageBox.Show(CStr(LastCol))
                MessageBox.Show(CStr(LastRow))
    
    'Edit: need to close the Excel file 
    
                xlWb.Save()
                xlApp.Quit()
                xlApp = Nothing
    
    
            Catch g As Exception
                MessageBox.Show(g.ToString)
            End Try
        End Sub
    Last edited by ChrisE; Jan 10th, 2025 at 08:27 AM.
    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.

  16. #16
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,416

    Re: VB.net Excel

    Quote Originally Posted by JimReid View Post
    Don't want to get involved with 3rd party packages. Should be able to do this with just VB.net.
    You can and I gave you a link in post #7 to where you can find the appropriate values, and from there you can create your own Constant variables in code.

  17. #17

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

    Re: VB.net Excel

    Quote Originally Posted by OptionBase1 View Post
    You can and I gave you a link in post #7 to where you can find the appropriate values, and from there you can create your own Constant variables in code.
    Someone else gave me this and it works fine.
    LastCol = .Range("A1:X1").End(XlDirection.xlToRight).Column
    LastRow = .Range("A" & .Rows.Count).End(XlDirection.xlUp).Row

    I must have had it coded wrong. Thanks anyway.

  18. #18
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,416

    Re: VB.net Excel

    Good catch, I didn't catch that it is an Enum so the XLDirection prefix is necessary.

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