我正在尝试将工作表的副本保存到基于单元格B8的特定文件夹,并根据单元格范围B8命名.xlsx文件。
例如,用户首先创建一个名为“test1”的新文件夹,&然后将此文件夹名称/文本输入单元格“B8”。他/她将在完成工作表上的工作后激活宏,&它会将副本保存到名为“test1”的文件夹中,并将.xlsx文件命名为“test1”。 (因此.xlsx文件将被命名为“testfolder1”,存储它的文件夹也称为“test1”)
我使用以下代码将工作表的副本保存到文件夹。只是无法弄清楚如何将单元格B8包含在SaveAs行中。用VB来解决这个问题。
Sub SaveForm()
exampleForm = Range("B8").Value
Application.ScreenUpdating = False
Application.DisplayAlerts = False
ActiveSheet.Copy
With ActiveWorkbook.ActiveSheet
.Range("42:" & Rows.Count).EntireRow.Delete xlShiftDown
.Range(.Cells(1, "J"), .Cells(1, Columns.Count)).EntireColumn.Delete xlToRight
.Parent.SaveAs "C:\Users\JohnSmith\Desktop\ExtractedWorksheet\" & exampleForm & ".xlsx"
.Parent.Close False
End With
End Sub
欣赏任何意见,希望我的结局目标是可以理解的。 -Thanks!
答案 0 :(得分:0)
我认为这就是你所追求的,试一试:
Sub SaveForm()
Static Path as string
Static FileName as string
if len(Path) = 0 then
Path = Range("B8")
if right(Path,1) <> "\" then
'make sure the path is "\" terminated
Path = Path & "\"
End if
else
FileName = Range("B8")
'Application.ScreenUpdating = False
Application.DisplayAlerts = False
ActiveSheet.Copy 'not sure why you're doing this, but do so if it makes sense elsewhere in your code
With ActiveWorkbook.ActiveSheet
.Range("42:" & Rows.Count).EntireRow.Delete xlShiftDown
.Range(.Cells(1, "J"), .Cells(1, Columns.Count)).EntireColumn.Delete xlToRight
.Parent.SaveAs "C:\Users\JohnSmith\Desktop\ExtractedWorksheet\" & Path & _
FileName & ".xlsx"
.Parent.Close False
End With
Path = ""
FileName = ""
End if
End Sub
如果从workheet_OnChange事件中调用此代码,则在更新单元格B8时,它将:
Path
。如果不是,则假设这是Path
Path
,请假设这是FileName
并保存。将“应用程序。屏幕更新”注释掉,直到一切正常,然后再将其重新插入。确定更容易的事情。
更新根据您对OP的最新评论:
Sub SaveForm()
'Application.ScreenUpdating = False
Application.DisplayAlerts = False
ActiveSheet.Copy 'not sure why you're doing this, but do so if it makes sense elsewhere in your code
With ActiveWorkbook.ActiveSheet
.Range("42:" & Rows.Count).EntireRow.Delete xlShiftDown
.Range(.Cells(1, "J"), .Cells(1, Columns.Count)).EntireColumn.Delete xlToRight
.Parent.SaveAs "C:\Users\JohnSmith\Desktop\ExtractedWorksheet\" & _
Range("B8") & "\" & FileName & ".xlsx"
.Parent.Close False
End With
Path = ""
FileName = ""
End Sub
答案 1 :(得分:0)
这是我为我所参与的项目创建的一个。
此代码将在活动工作表中运行(一旦单击按钮):
Sub exporttopdf()
Dim prnumber As Variant
Set prnumber = ActiveWorkbook.Names("prform_prnumber").RefersToRange
ActiveSheet.ExportAsFixedFormat xlTypePDF, ActiveWorkbook.Path & "/" & filesavename & ".pdf", , , False
End Sub