Monday, 28 November 2011

AutoFit the Cell in Excel while Copying data from one Excel to other

More advanced feature if you wanna autofit the cell's value while you copy data from one excel to another. Just copy following code and run it with QTP.
Dim A1 ‘As Range
Dim ws ‘ Worksheet
Set obj = CreateObject(“Excel.Application”)
obj.Visible = True
Set objWorkbook1= obj.Workbooks.Open(“C:\1.xls”)
Set objWorkbook2= obj.Workbooks.Open(“C:\2.xls”)
Set ws =objWorkbook2.Worksheets(“Sheet1″)
objWorkbook1.Worksheets(“Sheet1″).UsedRange.Copy
objWorkbook2.Worksheets(“Sheet1″).Range(“A1″).PasteSpecial Paste =xlValues
‘ To fit the first 10 columns
for column=1 to 10
ws.columns(column).AutoFit
next
****
objWorkbook1.save
objWorkbook2.save
objWorkbook1.close
objWorkbook2.close
set obj=nothing

No comments:

Post a Comment