Copy range from Open Workbook without naming file path

oplintx

New Member
Joined
Mar 1, 2014
Messages
28
I am trying to figure out how to copy a range of data from a open file WITHOUT naming a file path. The file path varies, and is a temp file downloaded from our system. I would only like to apply this macro only when a file called pricedownload.csv is open

My work team each downloads a pricing file each day and I have another file that applies a margin by customer. We download a file column A2:J and paste it into the work

1.) Check to see if "pricedownload.csv" is open
A. If No, Error "please open pricedownload file first"
B. If Yes, Step 2
2.) Find Range in "pricedownload.csv" file, using the active sheet starting in A2 and end in column J (rows vary)
3.) Select and then Copy
4.) Go to destination workbook "Rack Pricing" and paste values in A3.

I am new to VBA, I know logically how it should work but am clueless on the code.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Code:
Sub cp()
Dim objPriceFile As Workbook
On Error Resume Next
    Set objPriceFile = Workbooks("pricedownload.csv")
On Error GoTo 0
If objPriceFile Is Nothing Then MsgBox "Please Open Price File", vbExclamation: Exit Sub
    With objPriceFile.Sheets(1)
        .Range("A2:J" & .Range("J" & Rows.Count).End(xlUp).Row).Copy Workbooks("Rack Pricing.xlsm").Sheets("Sheet1").Range("A3")
    End With
End Sub
 
Upvote 0
One more thing...
Some times the file downloaded is
pricedownload (1).csv instead of pricedownload.csv. I don't know why this happens
cuz their are not multiple instances open.

Can I say change this to
Set objPriceFile like Workbooks("pricedownload(.*).csv")

Thanks
 
Upvote 0
you cannot use wildcards if that is how you intended to use the "*"

you can do this:

Code:
Sub prcDown()
Dim objPriceFile As Excel.Workbook
On Error Resume Next
Set objPriceFile = Workbooks("pricedownload.csv")

If objPriceFile Is Nothing Then
    Set objPriceFile = Workbooks("pricedownload (1).csv")
End If
On Error GoTo 0
End Sub
 
Upvote 0
How would this code be rewritten to capture files that do change. I have a file that each time is downloaded, it is renamed....ie...Sheet1,Sheet2,Sheet3,Sheet4,Sheet5,Sheet6,Sheet7,Sheet8,ect.

I am not sure how to make this code work for something like that.

thanks
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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