specifing a file path in vba

stevebutler

Board Regular
Joined
Jul 5, 2005
Messages
65
i want to save a copy of a sheet from Workbook A, into Workbook b.

I want Workbook B (with the new copied sheet) to be saved in the same folder as Workbook A

I thought i could use ThisWorkbook.path but i can't seem to get it to work.

also, i want to add the date to the end of the file name.

thanks in advance.
steve
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
How about:
Code:
Sub CopySheet()
Dim wbPath As String
Dim wbB As Variant
Dim NewName As String

'file path of current workbook
wbPath = ThisWorkbook.Path & "\"

'full path to Workbook B
wbB = wbPath & "WorkbookB.xls"

'full path and name that workbook with copied sheet will be saved as
NewName = wbPath & "WorkbookB " & Format(Date, "mm-dd-yy") & ".xls"

'check if workbook B is open
If WorkbookIsOpen("WorkbookB.xls") = False Then
    'if not open, open workbook
    Set wbB = Workbooks.Open(wbB)
Else
    Set wbB = Workbooks("WorkbookB.xls")
End If

Application.ScreenUpdating = False

'copy first sheet of workbook to WorkbookB
ThisWorkbook.Sheets(1).Copy _
After:=wbB.Sheets(Sheets.Count)

Application.ScreenUpdating = True

'save WorkbookB in same folder with new name
With wbB
    .SaveAs Filename:=NewName
    .Close 'close workbookB
End With

End Sub

Private Function WorkbookIsOpen(wbname) As Boolean
'   Returns TRUE if the workbook is open
Dim x As Workbook
On Error Resume Next
Set x = Workbooks(wbname)
If Err = 0 Then WorkbookIsOpen = True Else WorkbookIsOpen = False
End Function
 
Upvote 0
Steve

Can we see what you've tried?

How isn't it working?
 
Upvote 0
I don't know what i'm doing wrong...everytime i try this i get an error 400.

Workbook B does not exist...it should be created by the macro...but i tried to adjust and it didn't work

any ideas??
 
Upvote 0
fname = ThisWorkbook.Path & "\" & Date & ".xls"
Set wbk = Workbooks.Add
ThisWorkbook.Worksheets("Sheet1").Copy _
After:=wbk.Worksheets("Sheet1")
wbk.SaveAs fname


this is probably easy for you guys...i'm just missing something
 
Upvote 0
Oh! I see what you're doing.

You can use the date in the file name, but you have to format it--you can't use the mm/dd/yy format because it will interpret the slashes as directories. You can use *dashes* in filenames, though.

In place of the word Date, use
Code:
Format(Date,"mm-dd-yy")
 
Upvote 0
that was it!!

i also had to change the name of one of the worksheets...(so they didn't have the same name)

thank you so much, i learned a lot

steve
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,797
Members
449,048
Latest member
greyangel23

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