基于变量名称或数组名称新添加的工作表
我有一个包含我们基金经理月度报表所有PDF的文件夹。我正在尝试创建一个将循环遍历它们的子文件,将每个PDF报告转换为Excel文档中的工作表。问题是,每个公司给我们发表声明的命名约定是非常不同的,所以我试图想出一个优雅的方式来命名每个工作表。这是打开新工作簿的第一个子代码,设置路径并调用实际导入pdf的其他子代码。基于变量名称或数组名称新添加的工作表
Sub newWkbk_callSub()
Dim PDF_File As String
Dim wb As Workbook
Set wb = Workbooks.Add
wb.SaveAs Filename:="H:\Performance Reports\SMU Quick Endowment Performance Summaries\2017\Supporting Docs\Monthly Manager Statements\Update wksht " + Format(Now(), "mm-dd-yyyy") + ".xlsx"
Dim rptName As Variant
Dim element As Variant
Dim asOf As Date
Dim path, sfx, Fund1, Fund2 As String
path = "H:\Performance Reports\SMU Quick Endowment Performance Summaries\2017\Supporting Docs\Monthly Manager Statements\"
sfx = ".pdf"
asOf = WorksheetFunction.EoMonth(Now(), -1)
fund1= path + Format(asOf, "yyyy-mm-dd") + " fund1" + sfx
'this is fund 1's naming convention: "2017-02-28 Fund1"
fund2 = "something similar to fund1"
rptName = Array(fund1, fund2)
'loop through the report names/paths in the array
For Each element in rptName
Call Imp_Into_XL(element)
Next
End Sub
理想的情况下,通过rptName阵列这将循环,打开每个文件,它给了路径,然后使用以下子到PDF文本拖放到一个新的工作表:
Sub ImportPDF(PDF_File As String)
Dim PDFfile As Acrobat.AcroPDDoc 'access pdf file
Dim wordCount As Acrobat.AcroHiliteList 'set selection word count
Dim PDFpage As Acrobat.AcroPDPage 'get the particular page
Dim PDFtext As Acrobat.AcroPDTextSelect 'get the text of selection area
Dim wb As Workbook
Dim ws As Worksheet
Dim tabName As String
Application.ScreenUpdating = False
Set PDFfile = New Acrobat.AcroPDDoc
Set wordCount = New Acrobat.AcroHiliteList
With PDFfile
.Open (PDF_File) 'open PDF file
'add workbook sheet
Set ws = Worksheets.Add(, Worksheets(Sheets.Count))
tabName = (PDF_File)
Debug.Print (tabName) ' Can't just name the tab the PDF_file, because it's too long - "H:\areallylongstringofdirectories"
tabName = Right(tabName, Len(tabName) - 113)
tabName = Left(tabName, Len(tabName) - 4)
Debug.Print (tabName) ' I thought about trying to shorten it, but then I run into the problem where the naming convention for each firm's report is different, and the name will be different for a firm reporting "02-2017 Fund1" vs. "Fund2 February 2017"
ws.Name = tabName
'and really the code for doing the pdf import is not relevant to my question, this is where I'm trying to get the naming convention right
End Sub
两个我想过的想法是(1)我可以在调用字符串的变量名称之后命名选项卡(即,即使存储字符串“H:\ etc”,fund1是变量名称),我看起来(2)将另一个字符串变量作为附加参数传递给第二个子例程(即,调用Imp_Into_XL(element,tabName),但我不确定那个w我的想法是循环访问数组,以便最初打开文件。我觉得如果我对建立一个新班级了解很多(或者其他的),这可能会有所帮助,但是我对这种技巧却一无所知。
这是一个非常具体的问题,它很难描述,所以我真的很感激任何有关如何解决它的见解,或者如果你有任何想法,我没有想到会做到这一点事情,我都是耳朵。
1)强制执行一些规则:
如果你从你需要规范有IMO 3种方式来对付它的客户越来越可变数据。让您的客户以指定的名称格式提供他们的文档。你控制他们的数据,他们知道,有一些谈判的空间,这样你的代码可以相当统一。
2)你硬编码一堆if语句&其他逻辑 - 知道你收到的文件名 - 把文件名改为你的代码可以接受的东西,然后你用这些规则和文件名运行你的子文件。
3)你sucumb企业疯狂,花你的余生徒劳的编码,以适应下一个新客户的命名约定要求:)
我会去的选项之一。
我投票结束这个问题作为题外话,因为这似乎是一个设计问题,而不是一个编码问题。 – Comintern
注意这也是不正确的变量声明:'Dim path,sfx,Fund1,Fund2 As String'。只有'Fund2'显式为'String'类型。其余的是“变体”。 –
选项1(基于*变量名*命名基本上不可能在VBA中不支持任何类型的内省)。不要使用路径数组,而应使用字典,其中keys = paths和values = tabName $?否则,我同意共产国际这是一个比特定的代码*问题更多的设计问题。 –