Tuesday, February 15, 2011

Copy Specifc Data from One Excel to Other

' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Excel application not found."
    Wscript.Quit
End If
On Error GoTo 0

'Create an object for Excel file

Set xlBook = objExcel.Workbooks.Open("C:\Documents and Settings\deepakpe\Desktop\Mine_doc\new.xlsx")
Set xlSheet = xlBook.Worksheets(1)


' Select the range of data to be copy
objexcel.Range("D1:D46").Copy
set xlSheet = xlBook.Worksheets("Sheet2")
' Select the range where we want to copy
objexcel.Range("C3").Select
objexcel.Range("c3").Copy


objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

'Release an object

Set xlBook = Nothing
Set xlSheet = Nothing

Write in the Excel

strExcelPath = "C:\Documents and Settings\deepakpe\Desktop\Mine_doc\new.xlsx"

' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Excel application not found."
    Wscript.Quit
End If
On Error GoTo 0


'Delete the Excel
objexcel.Worksheets.Delete strExcelPath

' Create a new workbook.
objExcel.Workbooks.Add

' Bind to worksheet by creating an object
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
'objSheet.Name = "User Groups"

' Populate spreadsheet cells with user attributes.
objSheet.Cells(1, 1).Value = "User Common Name"
objSheet.Cells(2, 1).Value = "sAMAccountName"
objSheet.Cells(3, 1).Value = "Display Name"
objSheet.Cells(4, 1).Value = "Distinguished Name"
objSheet.Cells(5, 1).Value = "Groups"

' Enumerate groups and add group names to spreadsheet.
k = 5

objSheet.Range("A1:A4").Font.Bold = True
objSheet.Select
objSheet.Range("B5").Select
objExcel.ActiveWindow.FreezePanes = True
objExcel.Columns(1).ColumnWidth = 20
objExcel.Columns(2).ColumnWidth = 30
objexcel.Worksheets.Add
Set objSheet = objExcel.ActiveWorkbook.Worksheets(4)

Set objSheet = objExcel.ActiveWorkbook.Worksheets(2)
objSheet.Cells(1, 1).Value = "User Common Name"
objSheet.Cells(2, 1).Value = "sAMAccountName"
objSheet.Cells(3, 1).Value = "Display Name"
objSheet.Cells(4, 1).Value = "Distinguished Name"
objSheet.Cells(5, 1).Value = "Groups"

objSheet.Cells(1, 1).Value = "User Common Name"


' Save the spreadsheet and close the workbook.
' Specify Excel7 File Format.

objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close

' Quit Excel.
objExcel.Application.Quit

' Clean Up  OR Realease the object
Set objUser = Nothing
Set objGroup = Nothing
Set objSheet = Nothing
Set objExcel = Nothing

Wscript.Echo "Done"

Get Cell Data

Dim objExcel
Dim oSheet
'Create an object for Excel Application
Set objExcel = WScript.CreateObject("Excel.Application")
Set temp = objExcel.Workbooks.Open("C:\Documents and Settings\deepakpe\Desktop\Mine_doc\new.xlsx")

'Loop to go through each cell n get the data

Dim intRow , intCol
intRow = 1 : intCol = 1
WScript.Echo " Coloumn : Data "

Do Until objExcel.Cells(intRow,intCol).Value = ""
    Do Until objExcel.Cells(intRow, intCol) = ""
        WScript.Echo   objExcel.Cells(intRow, intCol).Value
        intRow = intRow + 1
    Loop
    WScript.Echo " "
     intCol = intCol + 1
     intRow = 1
Loop

objExcel.Workbooks.Close ()
objExcel.Quit

'Release the object
Set objExcel = Nothing

Get Sub Excel Name

Dim objExcel
Dim oSheet
'Create an object for Excel Application
Set objExcel = WScript.CreateObject("Excel.Application")
Set temp = objExcel.Workbooks.Open("C:\Documents and Settings\deepakpe\Desktop\Mine_doc\new.xlsx")

MsgBox temp.Worksheets.Count
MsgBox temp.Worksheets.Application


'Loop get the name of sub excel

For Each oSheet In objExcel.Worksheets
    WScript.Echo oSheet.Name
    MsgBox oSheet.name
Next

objExcel.Workbooks.Close ()
objExcel.Quit

'Release the object

Set objExcel = Nothing

Delete the Excel sheet

Dim objExcel

'Create an object for Excel Application
Set objExcel = WScript.CreateObject("Excel.Application")
Set temp = objExcel.Workbooks.Open("C:\Documents and Settings\deepakpe\Desktop\Mine_doc\temp.xlsx")

MsgBox temp.Worksheets.Count
MsgBox temp.Worksheets.Application
temp.Worksheets.Application.DisplayAlerts = false
temp.Worksheets.Delete
objExcel.Workbooks.Close ()
objExcel.Quit

'Release the object
Set objExcel = Nothing
 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | JCpenney Printable Coupons