Saving a particular sheet from one workbook to another in a particular folder - vba

I am trying to save a particular worksheet from my working workbook to another workbook, and trying to save it in the path of my current workbook. The saving option is in such a way that it should get saved with the dd.mm.yyyy.
I tried the following code and I am getting application defined error in the line
> newWB.SaveAs filename:=Path2 & Format(Now(), "yyyymmdd") &
> myWorksheets(i), FileFormat:=xlsx
Could you please help me figure out where I am wrong?
Sub save()
Dim myWorksheets() As String 'Array to hold worksheet names to copy
Dim newWB As Workbook
Dim CurrWB As Workbook
Dim i As Integer
Dim path1, Path2
path1 = ThisWorkbook.Path
Path2 = path1 & "\TT"
Set CurrWB = ThisWorkbook
myWorksheets = Split("Report", ",")
For i = LBound(myWorksheets) To UBound(myWorksheets) 'Go through entire array
Set newWB = Workbooks.Add 'Create new workbook
CurrWB.Sheets(Trim(myWorksheets(i))).Copy
newWB.SaveAs filename:=Path2 & Format(Now(), "yyyymmdd") & myWorksheets(i), FileFormat:=xlsx
newWB.Close saveChanges:=False
Next i
CurrWB.Save 'save original workbook.
End Sub

#Jenny - This code will accomplish what you ask in your question, it will save the ActiveSheet as a new file; and can be used as a function the can be called within your code. You can change the "Rpt" to identify the title of the new workbook. When I'm writing vba code, I always try to follow what my mother use to say to me, "Keep it simple".
Application.DisplayAlerts = False
Dim wbPath As String
wbPath = ThisWorkbook.Path
ActiveSheet.Copy
ActiveWorkbook.SaveAs Filename:=wbPath & "\" & "Rpt" & " " & Format(Date, "yyyymmdd") & ".xlsx", _
FileFormat:=51, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
Application.DisplayAlerts = True

Related

VBA - can not save as "xlsx" from "xlsm"

Me again , I'm trying to code for spliting sheets in the xlsm file into the seperate sheet then save them in the same place with the xlsm file. the code as below:
Sub splitsheet()
Dim path As String
Dim cities
ReDim cities(1 To ThisWorkbook.Worksheets.Count)
Dim i As Long
Dim sh As Worksheet
path = ActiveWorkbook.path
For i = 1 To Worksheets.Count
cities(i) = Sheets(i).Name
ActiveWorkbook.SaveAs _
Filename:=path & "\" & Sheets(i).Name & ".xlsx"
'ActiveWorkbook.Close False
Next i
End Sub
The error in my photo below. Why it can not save as in "xlsx" extension , above code is work fine with "xlsm" extension
Filename:=path & "\" & Sheets(i).Name & ".xlsm" 'it can work fine with xlsm extension
My question is how can save as in "xlsx" extension in this case. All assist/explaination will be appriciated.
Please try this code.
Sub EachSheetToEachOwnWorkbook()
' 286
Dim Path As String
Dim Ws As Worksheet
Application.ScreenUpdating = False
Path = ThisWorkbook.Path & "\"
For Each Ws In ThisWorkbook.Worksheets
Ws.Copy
With ActiveWorkbook
.SaveAs Filename:=Path & Ws.Name & ".xlsx", _
FileFormat:=xlOpenXMLWorkbook, _
CreateBackup:=False
.Close
End With
Next Ws
Application.ScreenUpdating = True
End Sub

How to save a macroless copy without closing the current file?

I am trying to save a copy of ThisWorkbook in xlsx while using SaveCopyAs. The problem is that SaveCopyAs saves the file as a macro enabled file, xlsb in my case which is not desired and while SaveAs does what I need, it also closes ThisWorkbook .
Is there an easier way to save in a format that does not support macros and without doing workarounds ?
Currently the best solution I found is the code snip below, but it's kinda finicky and it does not save the ContentTypeProperties that I need further on in the project, so does need to be added again.
Any ideas are appreciated, thanks!
Dim sPath as String
sPath = ThisWorkbook.FullName
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & sFileName & ".xlsx", 51
Application.Workbooks.Open (sPath)
Application.DisplayAlerts = True
Exit Sub
Define your path sPath = ThisWorkbook.Path & "\" & sFileName without extension
Use ThisWorkbook.SaveCopyAs sPath & ".xlsm" to save a copy.
Open that copy with Set Wb = Application.Workbooks.Open(sPath & ".xlsm")
Save again without Wb.SaveAs sPath & ".xlsx", xlOpenXMLWorkbook
Close workbook Wb.Close SaveChanges:=False
So something like the following should work, and would keep your current code running without any re-opening side effects of the current workbook.
Dim sPath As String
sPath = ThisWorkbook.Path & "\" & sFileName 'no extension!
ThisWorkbook.SaveCopyAs sPath & ".xlsm"
Dim Wb As Workbook
Set Wb = Application.Workbooks.Open(sPath & ".xlsm")
Wb.SaveAs sPath & ".xlsx", xlOpenXMLWorkbook
Wb.Close SaveChanges:=False
And probably you want to delete the xlsm file copy
Kill sPath & ".xlsm"
You might want to make use of Application.DisplayAlerts = False and Application.ScreenUpdating = False.

Excel Macro to save

I know i should probably be doing this in access or VB but I dont know how to use either. At the moment I have a submit button at the end of my form that saves and closes the workbook as whatever is inputted to a certain cell.
I am looking for a way to be able to do the same thing but break the worksheet out of the workbook. So it saves just the worksheet as its own excel file and with the name inputted into a certain cell. Below is the macro I am currently using.
Sub Saveworkbook()
Application.DisplayAlerts = False
Dim dName$, vName$
dName = Range("B8")
vName = ActiveWorkbook.FullName
ActiveWorkbook.SaveAs "W:\Test\" & dName
ActiveWorkbook.SaveAs vName
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub
It would also be cool if it could add the date and computers user in to save name but not essential.
Thanks in advance
Sam
Something like this -
Sub SaveSheet()
Dim wbkDashboard As Workbook
Dim wsTarget As Worksheet
Set wsTarget = Thisworkbook.worksheets("Sheet1")
Dim strFileName As String
strFileName = wsTarget.Range("B8").Value _
& Format(Now, "ddmmyyyy") & "-" & Environ("username") & ".xlsx"
Set wbkDashboard = Workbooks.Add
wsTarget .Copy Before:=wbkDashboard.Sheets(1)
For intSheetCount = 2 To wbkDashboard.Sheets.Count
wbkDashboard.Sheets(2).Delete
Next
wbkDashboard.SaveAs "W:\Test\" & wsTarget.Range("B8").Value _
& Format(Now, "ddmmyyyy") & "-" & Environ("username") & ".xlsx"
wbkDashboard.Close
wsTarget.Range("B8").Value= strFileName
Set wsTarget = Nothing
Set wbkDashboard = Nothing
End Sub
This code will save any changes you have created in the current version, then it will save just the Active Sheet as a new workbook with the username and date (Credit to #Will on the Environment Variables).
Sub Saveworkbook()
Application.DisplayAlerts = False
Dim Sheet1 As Worksheet
Dim dName$, vName$, sName$
dName = Range("B8")
vName = ActiveWorkbook.FullName
sName = ActiveWorkbook.ActiveSheet.Name
For Each Sheet1 In ActiveWorkbook.Sheets
If Not Sheet1.Name = sName Then
Sheet1.Delete
End If
Next Sheet1
ActiveWorkbook.SaveAs "W:\Test\" & dName & "_" & Environ("username") & "_" & Format(Now, "ddmmyy") & "xlsx"
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub

PasteSpecial method of range class fails in Function

I've been searching around about my problem, but still can't find the solution.
my goal is I want to copy a range from workbook A and paste it to workbook B.
Below my code:
Function fillDistributor(ByVal kodis As String)
Dim wbThis As Workbook
Dim wbTarget As Workbook
Dim fileName As String
Set wbThis = Workbooks.Open(fileName:="D:\Work\Master Data\Testing JMC\" & kodis & "\" & kodis & " Manual Template.xlsx")
wbThis.Sheets("Distributor").Activate
Range("B13:S14").Select
Selection.Copy
Set wbTarget = Workbooks.Open(fileName:="D:\Work\Master Data\Testing JMC\" & kodis & "\" & kodis & "-Distributor.xlsx")
wbTarget.Sheets("Sheet1").Activate
Range("A1:R2").Select
Selection.PasteSpecial Paste:=xlPasteAll
End Function
The error is
Pastespecial method of range class failed.
Any help would be appreciated!
You are writing this as a function. It needs to be a sub. Functions are intended to return a value to a calling process. Subs are meant to perform actions on ranges or variables within a workbook/worksheet.
Sub fillDistributor(kodis As String)
Dim wbThis As Workbook, wbTarget As Workbook
'Dim fileName As String '<- what is this used for?
Set wbThis = Workbooks.Open(fileName:="D:\Work\Master Data\Testing JMC\" & kodis & "\" & kodis & " Manual Template.xlsx")
Set wbTarget = Workbooks.Open(fileName:="D:\Work\Master Data\Testing JMC\" & kodis & "\" & kodis & "-Distributor.xlsx")
wbThis.Sheets("Distributor").Range("B13:S14").Copy _
Destination:=wbTarget.Sheets("Sheet1").Range("A1")
wbThis.Close
wbTarget.Close
End Sub
A sub can accept parameters passed in just as a function can.

How can I SaveAs a new workbook to original workbook directory with Excel VBA?

Basically, I'd like to save some worksheets into separate new workbooks in the same system location as original notebook I am deriving from.
I recognize that the default path is to save something new is to the location of the current notebook, but perhaps since I am opening a new workbook the default reverts to the user's Document's folder, which is where they are saving right now.
I "learned" VBA over the last couple of days, so advice on other things you notice is cool too, but the saveas is what's bothering me.
Dim ws As Worksheet
Dim wb As Workbook
Dim dept_array As Variant
Dim dept As Variant
' Add or remove a department name spelled exactly as it is in the filter
dept_array = Array("HR", "IT", "Marketing", "Product Marketing", "Sales", "Channels", "Presales", "Direct", "Sales Ops", "R&D", "Support", "G&A")
Application.ScreenUpdating = False
For Each ws In Workbooks("Weekly Department Transaction Report.xlsm").Worksheets
For Each dept In dept_array
If Application.Proper(ws.Name) = Application.Proper(dept) _
Then
Set wb = Workbooks.Add
ThisWorkbook.Sheets(dept).Copy Before:=wb.Sheets(1)
wb.Saveas dept & "_" & Format(Now, "yyyymmdd") & ".xlsx"
Workbooks("Weekly Department Transaction Report.xlsm").Sheets("Codes").Copy After:=Workbooks(dept & "_" & Format(Now, "yyyymmdd") & ".xlsx").Sheets(dept)
Workbooks("Weekly Department Transaction Report.xlsm").Sheets("How").Copy Before:=Workbooks(dept & "_" & Format(Now, "yyyymmdd") & ".xlsx").Sheets(dept)
Workbooks(dept & "_" & Format(Now, "yyyymmdd") & ".xlsx").Save
End If
Next dept
Next ws
Application.ScreenUpdating = True
End Sub
Please let me know if I am not following the correct stackoverflow format.
Longtime user first time asker :)
Edit this line in your code:
wb.SaveAs FileName:= ThisWorkbook.Path & "\" & dept & "_" & Format(Now, "yyyymmdd") & ".xlsx"