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
Set ExcelSheet = nothing
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 we go...

Public Function GetColCount (sSheetName)
On Error Resume Next
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.
Get Column Count in Excel Get Column Count in Excel Reviewed by Animesh Chatterjee on February 07, 2009 Rating: 5

1 comment:

  1. very helpful for me.I’d have to examine with you here. Which is not one thing I usually do! I take pleasure in reading a post that may make folks think. Additionally, thanks for permitting me to comment!


Powered by Blogger.