Copying cell contents from an Excel file then pasting into different Excel file

Started by mjwillyone, August 11, 2015, 08:25:50 PM

Previous topic - Next topic

mjwillyone

Hello,

I am wanting to copy the cell contents from an Excel file and then paste it into the cell of a different Excel file.   I searched this forum and didn't find quite this scenario.

I have attached an Excel file with the data highlighted that I would like to copy using Winbatch.  Below I have listed each of the highlighted cell contents and the variable name I would like each to be called:

100001 = batchnbr
08/03/2015 = glpostdate
$11,861.00 = batchtotal

I then want to be able to use those variables in a different spreadsheet ...  to paste the values in three different cells.   How can I do this?

Thank you very much!
Mike



stanl

It shouldn't be that complicated, but we need to understand your setup.  If we can assume n number of workbooks under the same Excel instance, one method (though perhaps not the most efficient):

val = oXLS.workbook1.worksheet(n).cells(r,c).Value  ; or use range() instead of cells

oXLS.workbook2.worksheet(n1).cells(r1,c1).Value=val

with the understanding that all dotted items (i.e. workbook1) are objects created from the Excel hierarchy.

If just moving data from different sheets in the same workbook the method is similar.

mjwillyone

They are two different workbooks.

I am assuming that workbook1 would be the file name of one workbook, and workbook2 would the file name of the 2nd?

Thanks,
Mike

stanl

Yes, once loaded the workbooks collection saves the filename + extension (w/out the path).  I would suggest assigning to variables, i.e.

;load first workbook
wkb1 = oXLS.Activeworkbook.name
;load second workbook
wkb2 = oXLS.Activeworkbook.name

then you can always move between them...

oXLS.wkb1.Activate  ;sets focus back to first workbook