excel - 如何删除 ws.name
问题描述
我尝试使用以下脚本创建 PDF 文件,它可以工作,但是当我删除时ws.Name
,脚本会中断。
Sub createPDFfiles()
Dim ws As Worksheet
Dim strName As String
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next 'Continue if an error occurs
' Name PDF files based on the worksheet Index (e.g Annex 1.1.1, Annex 1.1.2, etc.)
strName = Range("A10").Text & " " & Range("C7").Text & ws.Name
' If you want to name the PDF files differently just change the Fname variable above to
' whatever you like. For example if you changed Fname to:
'
' Fname = "C:\myFolder\pdfs\" & ActiveWorkbook.Name & "-" & ws.Name
'
' The files would be stored in C:\myFolder\pdfs, and named using the
' spreadsheet file name and the worksheet name.
'
' WARNING: Using worksheet names may cause errors if the names contain characters that Windows
' does not accept in file names. See below for a list of characters that you need to avoid.
'
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next ws
End Sub
我怎样才能删除它ws.Name
并且仍然可以工作?
解决方案
您的Range
对象在此声明中不合格:
strName = Range("A10").Text & " " & Range("C7").Text & ws.Name
所以他们指的是活动工作表上的那些单元格,而不是工作表上的单元格ws
。
如果您想在您循环的每个工作表上引用这些单元格,请适当地限定它们:
strName = ws.Range("A10").Text & " " & ws.Range("C7").Text
您可能希望在尝试保存 PDF 之前测试 strName 不为空。
我还建议丢失On Error Resume Next
线路,并正确处理错误。
推荐阅读
- ruby-on-rails - 带有sidekiq的delayed_paperclip返回NoMethodError:nil的未定义方法`fetch':NilClass
- android - 如何在不使用 WindowManger 的情况下将视图放置在其他视图之上?
- php - PHP+MySql 中的 foreach 重复超过 10 次
- html - 如果值包含双引号,如何将 HTML 表单中的参数传递给文本框
- python-3.x - 在 Python 中将两个排序列表合并为一个排序列表
- docker - 为什么 Docker CMD 在 GKE 中作为 chronos 运行?
- r - 将表作为函数参数和值传递
- api - 有没有办法在 Strapi 中动态存储 API 请求响应?
- mysql - MySQL将两个表合并在一起并使用第二个表中最近插入的行进行排序
- python - 无法使用 Selenium(Python)单击按钮