Macro to set a path

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
146
Hi all ,

i have a macro which copies data from one workbook to current work book which is working fine.

i need a help if i want to copy the data to some other file with the help of giving path insted of active workbook
Path - "C:\Result\ABC.xlsx"

at present i am using below code-

Set wbCopyTo = ActiveWorkbook
Set wsCopyTo = Sheet2



full code

Sub Foocopy()
Dim vFile As Variant
Dim wbCopyTo As Workbook
Dim wsCopyTo As Worksheet
Dim wbCopyFrom As Workbook
Dim wsCopyFrom As Worksheet
'>>>>> need to copy the data to ABC.xlsx
Set wbCopyTo = ActiveWorkbook
Set wsCopyTo = Sheet2
'-------------------------------------------------------------
'Open file with data to be copied

vFile = Application.GetOpenFilename("Excel Files (*.xl*)," & _
"*.xl*", 1, "Select Excel File", "Open", False)

'If Cancel then Exit
If TypeName(vFile) = "Boolean" Then
Exit Sub
Else
Set wbCopyFrom = Workbooks.Open(vFile)
Set wsCopyFrom = wbCopyFrom.Worksheets(1)
End If

'--------------------------------------------------------------
'Copy Range
wsCopyFrom.Range("B6").Copy
wsCopyTo.Range("B5").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False


wsCopyFrom.Range("B6").Copy
wsCopyTo.Range("B6").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False



'Close file that was opened
wbCopyFrom.Close SaveChanges:=False
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
try like this:
Code:
[COLOR=#333333]Set wbCopyTo = [/COLOR]Application.Workbooks.Open ([COLOR=#333333]"C:\Result\ABC.xlsx"[/COLOR]) 
[COLOR=#333333]Set wsCopyTo = wbCopyTo.Sheet2[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top