-
Jan 9th, 2025, 12:16 PM
#1
Thread Starter
Junior Member
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.
-
Jan 9th, 2025, 01:19 PM
#2
Re: VB.net Excel
It might be helpful if you could post the VBA code where you are using these items.
-
Jan 9th, 2025, 01:35 PM
#3
Thread Starter
Junior Member
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.
-
Jan 9th, 2025, 01:38 PM
#4
Thread Starter
Junior Member
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
-
Jan 9th, 2025, 02:55 PM
#5
Re: VB.net Excel
Originally Posted by JimReid
...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?
-
Jan 9th, 2025, 03:18 PM
#6
-
Jan 9th, 2025, 03:42 PM
#7
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
-
Jan 9th, 2025, 03:54 PM
#8
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.
-
Jan 9th, 2025, 07:16 PM
#9
Thread Starter
Junior Member
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
-
Jan 10th, 2025, 05:31 AM
#10
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
-
Jan 10th, 2025, 07:51 AM
#11
Thread Starter
Junior Member
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.
-
Jan 10th, 2025, 07:52 AM
#12
Thread Starter
Junior Member
-
Jan 10th, 2025, 07:55 AM
#13
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!
-
Jan 10th, 2025, 08:00 AM
#14
Thread Starter
Junior Member
Re: VB.net Excel
Originally Posted by kareninstructor
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.
-
Jan 10th, 2025, 08:17 AM
#15
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.
-
Jan 10th, 2025, 09:53 AM
#16
Re: VB.net Excel
Originally Posted by JimReid
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.
-
Jan 10th, 2025, 01:05 PM
#17
Thread Starter
Junior Member
Re: VB.net Excel
Originally Posted by OptionBase1
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.
-
Jan 10th, 2025, 01:46 PM
#18
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|