copying cells from one file to another file

O

oercim

Hello. I need help. I am not a profession VBA programmer. I want to
create macro such that it will copy some cells from a file(let C:
\folder\file1) to other filelet C:\folder\file2).

The below macro isnot my main macro. However if it works, I can make
work may main macro. But the below one doesnt work, with same style
how can I make it work? Thanks alot

I try to copy "cell(1,1) of Sheet1 of C:\folder\file1" to "cell
(1,1) of Sheet1 of C:\folder\file2"

Sub macro1()
F1 = "C:\folder\file1"
F2 = "C:\folder\file2"
F2.Sheets("Sheet1").Cells(1,1) = F1.Sheets("Sheet1").Cells(1,1)
End Sub
 
J

Joel

Try these changes

Sub CopyCells()
F1Name = "C:\folder\file1"
F2Name = "C:\folder\file2"

Set F1 = Workbooks.Open(Filename:=F1Name)
Set F2 = Workbooks.Open(Filename:=F2Name)

'this will not copy any formating
F2.Sheets("Sheet1").Cells(1, 1) = F1.Sheets("Sheet1").Cells(1, 1)
'this will copy formating
F1.Sheets("Sheet1").Cells(1, 1).Copy _
Destination:=F2.Sheets("Sheet1").Cells(1, 1)

'or copy multiple cells
F1.Sheets("Sheet1").Range("A1:B10").Copy _
Destination:=F2.Sheets("Sheet1").Cells(1, 1)

End Sub
 
O

oercim

Try these changes

Sub CopyCells()
F1Name = "C:\folder\file1"
F2Name = "C:\folder\file2"

Set F1 = Workbooks.Open(Filename:=F1Name)
Set F2 = Workbooks.Open(Filename:=F2Name)

'this will not copy any formating
F2.Sheets("Sheet1").Cells(1, 1) = F1.Sheets("Sheet1").Cells(1, 1)
'this will copy formating
F1.Sheets("Sheet1").Cells(1, 1).Copy _
   Destination:=F2.Sheets("Sheet1").Cells(1, 1)

'or copy multiple cells
F1.Sheets("Sheet1").Range("A1:B10").Copy _
   Destination:=F2.Sheets("Sheet1").Cells(1, 1)

End Sub

I just want to copy values not the format or other things when I use
my way in the same file copying from one sheet to another it works.
For example

Sub macro1()
Sheets("Sheet2").Cells(1,1) = Sheets("Sheet1").Cells(1,1)
End Sub

Above one works fine fcopying cell(1,1) from Sheet1 to Sheet2. Cant I
use use this type in copying from one file to anotjer file? thanks
alot
 
O

oercim

Try these changes

Sub CopyCells()
F1Name = "C:\folder\file1"
F2Name = "C:\folder\file2"

Set F1 = Workbooks.Open(Filename:=F1Name)
Set F2 = Workbooks.Open(Filename:=F2Name)

'this will not copy any formating
F2.Sheets("Sheet1").Cells(1, 1) = F1.Sheets("Sheet1").Cells(1, 1)
'this will copy formating
F1.Sheets("Sheet1").Cells(1, 1).Copy _
   Destination:=F2.Sheets("Sheet1").Cells(1, 1)

'or copy multiple cells
F1.Sheets("Sheet1").Range("A1:B10").Copy _
   Destination:=F2.Sheets("Sheet1").Cells(1, 1)

End Sub

Thanks alot. I did it. Again thanks alot. That was very helpful for me
 
J

Joel

Sorry to confuse you. I just gave additional methods for copying. The only
problem with you code is the refereencing the other workbook. I assumed the
workbooks were closed. I just realized the workbooks may be opened. Here is
code assuming the workbooks were opened.

Sub CopyCells()
F1Name = "C:\folder\file1"
F2Name = "C:\folder\file2"

Set F1 = Workbooks(F1Name)
Set F2 = Workbooks(F2Name)

'this will not copy any formating
F2.Sheets("Sheet1").Cells(1, 1) = F1.Sheets("Sheet1").Cells(1, 1)
'this will copy formating
F1.Sheets("Sheet1").Cells(1, 1).Copy _
Destination:=F2.Sheets("Sheet1").Cells(1, 1)

'or copy multiple cells
F1.Sheets("Sheet1").Range("A1:B10").Copy _
Destination:=F2.Sheets("Sheet1").Cells(1, 1)

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top