Sunday, September 5, 2010

Working with Excel Object

'Create an Microsoft Excel Object using VBScript
Set ExcelObject = CreateObject("Excel.Application")
ExcelObject.visible = True
ExcelObject.WorkBooks.Add 'Adds a workbook to an excel object
ExcelObject.Sheets(1).Cells(1,1).value = "Text in the Cell" 'Writes a text to a particular cell in the excel object

'Open Microsoft file using VBScript
Set ExcelObject = CreateObject("Excel.Application")
ExcelObject.visible = True
ExcelObject.Workbooks.Open("c:\excelsheet.xls",Default, False)
ExcelObject.Sheets(1).Cells(1,1).value = "Text to be entered"
ExcelObject.Activeworkbook.SaveAs("d:\excelsheet.xls") 'Using Save As

'Save the Excel Workbook
ExcelObject.Activeworkbook.save

'Get the Number of Rows used in the Excel sheet
RowCount = ExcelObject.ActiveWorkbook.Sheets(1).UsedRange.Rows.count
msgbox "Number of rows used in the excel sheet "& RowCount

'Get the Number of Columns used in the Excel sheet

ColumnCount = ExcelObject.ActiveWorkbook.Sheets(1).UsedRange.Columns.count
msgbox "Number of columns used in the excel sheet "&ColumnCount

'Change the sheet name of the Excel workbook
ExcelObject.Activeworkbook.Sheets(1).Name = "NameOftheFirstSheet"
ExcelObject.Activeworkbook.Sheets(2).Name = "NameoftheSecondSheet"

'Get the Sheet Name of the Excel workbook
FirstSheetName = ExcelObject.Activeworkbook.Sheets(1).Name
msgbox "Name of the first sheet"&FirstSheetName
SecondSheetName = ExcelObject.Activeworkbook.Sheets(2).Name
msgbox "Name of the second sheet"&SecondSheetName

'Get the value of the Particular Cell 
ValueOfTheCell = ExcelObject.Activeworkbook.Sheets(1).Range("A1").Value
msgbox "Value Of the Cell " &  ValueOfTheCell

'Storing and Using the data in Excel First Row as a collection in Dictionary Object
lNoofColumns = ExcelObject.Activeworkbook.Sheets(1).UsedRange.Columns.Count
Set oDic = New Collection
For lColNumber = 1 To lNoofColumns
        lColValue = ExcelObject.Activeworkbook.Sheets(1).Range(GetCol(lColNumber) & "1").Value
        If (Len(lColValue) > 0) Then
            oDic.Add Item:=lColNumber, Key:=CStr(LCase(lColValue))
        End If
Next

Function GetCol(ColumnNumber)
    FuncRange = ExcelObject.Activeworkbook.Sheets(1).Cells(1, ColumnNumber).AddressLocal(False, False)
    'Creates Range (defaults Row to 1) and returns Range in xlA1 format

    FuncColLength = Len(FuncRange)
    'finds length of range reference

    GetCol = Left(FuncRange, FuncColLength - 1)
    'row always "1" therefore take 1 away from string length and you are left with column ref
End Function

'Getting the value using Column value
Msgbox "Column Number of Cell" & GetCol(oDic("Row 1 value of the 1st Column"))

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi,
    I tried -

    ExcelObject.Sheets(1).Cells(1,1).value = "hello"
    .
    .
    ExcelObject.Sheets(1).Cells(4,1).value = "hello"

    but it doesn't work on merged cells.
    if i merge cells (1,1) to (4,1)
    the above command works on only (1,1)

    Please help.

    ReplyDelete
  3. How can I set focus on a particular cell in excel through vbscript

    Assume I need to paste a copied range of data
    If I simply write

    .Paste

    It will paste the data from the very first row (i.e A1 or (1,1))

    What if I want to paste that copied segment from some other cell say A10

    Plz Help
    Thanks in advance...!!

    ReplyDelete
  4. Hi Arpit,

    Below blog will help you to copy and paste the range of cell of Excek using Vbscript.

    http://vb-excel.blogspot.com/2015/06/copy-and-paste-range-of-cell-in-excel.html

    ReplyDelete
  5. Hi, UsedRange thing works but sometimes gives disguising results. At least on my reports. Does it have conditions to apply?

    ReplyDelete
  6. Hi, How to compare a row of cells (say the row has 7 columns) of 2 different worksheets in the same workbook ?

    ReplyDelete
  7. Hi, How do i open an Protected Excel sheet, which don't have a password using VBS?

    ReplyDelete