i want to achieve 2 things:
firstly to copy 2 sheets out of a workbook and into a new book but only the values and formats.
and secondly copy one of the sheets into an existing workbook again only values and formats.
i have got the first part working but am struggling with the second. the code i have up to now is below, probably not the best way to do it, but it does the job apart from the second bit (highlighted) can someone point me in the right direction?
Cheers, Craig
Si fractum non sit, noli id reficere
firstly to copy 2 sheets out of a workbook and into a new book but only the values and formats.
and secondly copy one of the sheets into an existing workbook again only values and formats.
i have got the first part working but am struggling with the second. the code i have up to now is below, probably not the best way to do it, but it does the job apart from the second bit (highlighted) can someone point me in the right direction?
Code:
Function Export()
'exports sheets into individual weekly workbooks
Application.ScreenUpdating = False
'declare and set variables*******************
Dim wb As Workbook
Dim sh
Dim shNo As Integer
Dim strNewFileName As String
Dim objSheet As Worksheet
Dim strMsg As String
Dim msgStyle
Dim msgTitle As String
Dim response
Dim mystring
Dim strDate
Dim intFileOverwrite As Integer
Dim arrws(), i As Long
Dim AnnualWB As Workbook
Dim strSickSheetName As String
Set objSheet = Sheets("parameters")
strDate = objSheet.Range("b3").Value
intFileOverwrite = 0
shNo = 1
strNewFileName = objSheet.Range("o3").Value
msgStyle = vbYesNo + vbCritical + vbDefaultButton2
msgTitle = "Confirm Save"
strMsg = "A file already exists for week commencing " _
& strDate _
& ". Do you want to overwrite it?"
'AnnualWB = objSheet.Range("o5").Value
strSickSheetName = objSheet.Range("i3").Value
'check to see if file already exists************
If FileThere(strNewFileName) Then
response = MsgBox(strMsg, msgStyle, msgTitle) ' set response variable to display message box
'get user response
If response = vbYes Then ' User chose Yes.
intFileOverwrite = 0
Else ' User chose No.
intFileOverwrite = 1
End If
Else
End If
'if file exists or user chose to overwrite then run code else end
If intFileOverwrite = 0 Then
Application.DisplayAlerts = False
Application.SheetsInNewWorkbook = 2
'create new workbook
'copy relevant sheets
'and paste values and formats ONLY
Set wb = Workbooks.Add
With wb
For Each sh In Array("sickness", "abstractions")
ThisWorkbook.Sheets(sh).Cells.Copy
.Sheets(shNo).Cells.PasteSpecial Paste:=xlValues
.Sheets(shNo).Cells.PasteSpecial Paste:=xlFormats
.Sheets(shNo).Name = sh
.Sheets(shNo).EnableSelection = xlUnlockedCells
.Sheets(shNo).Protect Password:="pword", DrawingObjects:=True, Contents:=True, Scenarios:=True
shNo = shNo + 1
Next
End With
Application.CutCopyMode = False
'Application.DisplayAlerts = false
wb.SaveAs strNewFileName 'save the new workbook with the given name
wb.Close 'close new workbook
[COLOR=red yellow]
shNo = 1
Set AnnualWB = Workbooks.Open(objSheet.Range("o5").Value)
With AnnualWB
ThisWorkbook.Sheets("Sickness").Copy
.Sheets(shNo).Cells.PasteSpecial Paste:=xlValues
.Sheets(shNo).Cells.PasteSpecial Paste:=xlFormats
.Sheets(shNo).Name = strSickSheetName
.Sheets(shNo).EnableSelection = xlUnlockedCells
.Sheets(shNo).Protect Password:="mrmann", DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
AnnualWB.Save
AnnualWB.Close
[/color]
Application.SheetsInNewWorkbook = 3 'reset number of sheets in new workbooks to default
Application.ScreenUpdating = True
Else
End
End If
End Function
Cheers, Craig
Si fractum non sit, noli id reficere