I have browsed this and other forums to create a code to accomplish this task. I have no previous VBA experience so please be gentle.
Essentially, what I would like to have happen is the user fills out the Excel form and clicks a button. The button will reference what was selected in cell K4 and then based off that selection, copy a hidden worksheet into a new workbook and then prompt the user to save.
The Code I am using is:
Private Sub RSM_Click()
Dim newWkbk As Workbook
Dim newWksht As Worksheet
Dim wksht As Worksheet
Dim test As String
If StrComp(Me.Range("K4").Text, "INTERNAL USB", vbTextCompare) = 0 Then
test = "RSM_InternalUSB"
ElseIf StrComp(Me.Range("K4").Text, "INTERNAL 24 HR", vbTextCompare) = 0 Then
test = "RSM_Internal24Hr"
Else
test = "RSM_External"
End If
For Each wksht In ThisWorkbook.Worksheets
If wksht.Name = test Then
wksht.Visible = xlSheetVisible
Set newWksht = wksht.Copy
newWksht.Name = "RSM Onboarding Guide"
Set newWkbk = newWksht.Parent
End If
Next wksht
Dim varResult As Variant
Dim ActBook As Workbook
'displays the save file dialog
varResult = Application.GetSaveAsFilename(FileFilter:= _
"Excel Files (*.xlsm), *.xlsm", Title:="RSM Guide", _
InitialFileName:="\\Onboarding\")
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then
ActiveWorkbook.SaveCopyAs Filename:=varResult
Exit Sub
End If
End Sub
However I am getting a
Compile error: Expected function or Variable
on the Set newwksht = wksht.Copy
strand. It doesn't like the copy.
I don't even know if the save portion will work as I haven't been able to get past this
could try this:
If GetSheet(test, newWksht) Then
With newWksht
.Visible = xlSheetVisible
.Copy ' this will make a copy of the referenced sheet in a newly created workbook
ActiveSheet.Name = "new"
.Visible = xlSheetHidden
End With
Dim varResult As Variant
'displays the save file dialog
varResult = Application.GetSaveAsFilename(FileFilter:= _
"Excel Files (*.xls*), *.xls*", Title:="RSM Guide", _
InitialFileName:="\\Onboarding\")
With ActiveWorkbook ' reference the newly created workbook, which is the "active" one
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then .SaveAs Filename:=varResult
End With
End If
where it uses this GetSheet()
"helper" function:
Function GetSheet(shtName As String, retSht As Worksheet) As Boolean
Set retSht = Worksheets(shtName)
GetSheet = Not retSht Is Nothing
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.