我已经浏览了此论坛和其他论坛,以创建完成此任务的代码。我以前没有VBA经验,所以请保持冷静。
基本上,我希望发生的事情是用户填写Excel表单并单击一个按钮。该按钮将引用在单元格K4中选择的内容,然后基于该选择,将隐藏的工作表复制到新的工作簿中,然后提示用户进行保存。
我使用的代码是:
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
但是我得到
在编译错误:预期的函数或变量
Set newwksht = wksht.Copy
链上。它不喜欢副本。
我什至不知道保存部分是否可以正常工作,因为我无法通过此操作
答案 0 :(得分:0)
可以尝试:
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
使用此GetSheet()
“帮助”功能的地方:
Function GetSheet(shtName As String, retSht As Worksheet) As Boolean
Set retSht = Worksheets(shtName)
GetSheet = Not retSht Is Nothing
End Function