Header Ads

Get Column Count in Excel

Few days Back I got a requirement of posting few excell related code.....


Hi Animesh,Can you please tell me the exact code for Get Column Count in ExcelI need to code very urgentRegardsRobin







Function getColoumnNumber()
Set ExcelFile = CreateObject("Excel.Application")
'You can set it to false as well
ExcelFile.Visible = true
'Open the Excel file
ExcelFile.Workbooks.Open "C:\Demo.xls"
'Get the 1st sheet (can be change to some other index number)
Set ExcelSheet = ExcelFile.Sheets.Item(1)
'Get the number of columns
Cols = ExcelSheet.UsedRange.Columns.Count
msgbox "No of Columns: " & Cols
getColoumnNumber=Cols
Set ExcelSheet = nothing
ExcelFile.Quit
Set ExcelFile = nothing
End Function








one more good piece of code is ..
Here is a simple piece of code that will give you the column count.even you can get the column name as well..here we go...

Public Function GetColCount (sSheetName)
On Error Resume Next
Do
i = i + 1
sColName = DataTable.GetSheet(sSheetName).GetParameter(i).Name
Loop While 0 = Err.Number
'GetParameter throws an error when using an index that is out of bounds ...
'We can use this functionality to ASSUME that it's an unused column.
'We've come to the end of our USED columns
GetColCount = i - 1
On Error GoTo 0
End Function




Just let me know what more you want.
Powered by Blogger.