• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3146
  • Last Modified:

get the last used column and row excel with vb.net

Hello,

I want to know how can I get the last row and column with data in Excel through vb.net. I've developed an application to automate Excel and make
some files and I need to know these values in order to apply correct format.

Thanks in advance.
0
nuberun
Asked:
nuberun
  • 3
1 Solution
 
dapcomCommented:
something like:

strAddress=Range("A1").SpecialCells(xlLastCell).Address
or
iCol=Range("A1").SpecialCells(xlLastCell).Column
iRow=Range("A1").SpecialCells(xlLastCell).Row

should help?

xlLastCell=11 if you don't have the constants...
0
 
dapcomCommented:
NB: with automation I guess you need to use a ref to the corrsponding sheet, workbook or applicationto use the Range property. I guess you already have one:

ex:
myExcelApp.WorkBooks(1).Sheets(1).range...

or myWorkbook.Sheets(1).range

mySheet.Range...
0
 
nuberunAuthor Commented:
And how can I also get the letter of this column, for example AA.
Thanks
0
 
dapcomCommented:
I don't know if there's a direct way to do that, I usually profer using the column numbers and change to an address when needed.

Anyway here is a way to get the letter from the address:

Public Sub test()
    Dim strAddress As String
    Dim strColLetter As String
    strAddress = Range("B2").EntireColumn.AddressLocal            'return "$B:$B"
    strColLetter = Mid$(strAddress, 2, InStr(strAddress, ":") - 2)  'get rid of first $ and read until ':'
    MsgBox strColLetter
End Sub

But something tells me an excel function must exist for that?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now