Get Column Count in Excel

1
91
Spread the love
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares

Few days (Excel) 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




Get Column Count in Excel
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


Get Column Count in Excel
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.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares

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!
    datingsites

LEAVE A REPLY

Please enter your comment!
Please enter your name here