Code to: Open another workbook and close current

daniels012

Well-known Member
Joined
Jan 13, 2005
Messages
5,219
I want code to:
Open EstimatingSheet.xls
SaveAs current workbook as Range B13 &".xls"
Open EstimatingSheet.xls workbook from somewhere on C:\
Then close the one with B13.xls


If I need to explain further, I will

Michael
 
Ok
How about?
Code:
Sub SaveToSidonna()
    Dim wb As Workbook, myFileName As String, myVal As Long, myMax As Long
    Dim strPath As String, fname As String, i As Integer
    If MsgBox("Have you printed your worksheets yet?", vbYesNo) = vbNo Then Exit Sub
    Application.ScreenUpdating = False
    Sheets("ESTIMATING").Select
    ActiveWorkbook.Save
    strPath = "\\Sidonna\c\Estimating\"
    fname = Dir(strPath & Range("B11") & "*.xls"
    If fName = "" Then
         myFileName = Range("b11").Value & "1.xls"
    Else
         Do While fName <> ""
              myVal = Val(Replace(Replace(fName, ".xls",""),Range("b11").Value,"")
              myMax = WorksheetFunction.Max(myMax, myVal)
              fName = Dir()
         Loop
         myFileName = Range("b11").Value & myMax + 1
    End If
    ThisWorkbook.SaveCopyAs strPath & myFileName & ".xls"
End Sub
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I had to add a parenthesis here:
fname = Dir(strPath & Range("B11") )& "*.xls"
And here:
myVal = Val(Replace(Replace(fName, ".xls",""),Range("b11").Value,"") )

I get an error on this line of code??

Code:
fName = Dir()


Michael
 
Upvote 0
I do not know wether this helps, but I had problems to change to network drive. This Dir function was not work with network drive like \\.

I learn the solution from Jindom:

Dim myDir As String
myDir = "\\xxx\yyy" '<- change here
CreateObject("WScript.Shell").CurrentDirectory = myDir
MsgBox CurDir
 
Upvote 0
I had to add a parenthesis here:
fname = Dir(strPath & Range("B11") )& "*.xls"
And here:
myVal = Val(Replace(Replace(fName, ".xls",""),Range("b11").Value,"") )

I get an error on this line of code??

Code:
fName = Dir()


Michael

The first one should read as
Code:
fname = Dir(strPath & Range("B11") & "*.xls")
 
Upvote 0
:eek: Wow :oops: :eek:
That really seems to be working.
Jindon,
I appreciate all your help on this. I am sorry I did not explain very well in my earlier descriptions.

We will be testing this on all our different computers. I'll stay in touch.
:biggrin: :biggrin: :biggrin: :biggrin: :biggrin: :biggrin: :biggrin:
Thank You, :pray:
Michael
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,047
Members
449,206
Latest member
Healthydogs

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