以下 Sub 要求用户选择要打开的通用模板,然后要求用户选择源文件(以填充模板)。源文件包含许多工作表和数据透视表。然后,子从数据透视表中选择数据并将其复制到模板中。我需要源文件是一个变量而不是一个硬编码的数据透视表源,因为这个标题会根据用户的选择而变化。
问题1:复制数据时只显示REF!而不是实际数据(即使数据存在)。
'Open Generic Report to populate with data
Dim GenericFolderLocation As String
MsgBox "Please select the generic porfolio template..."
GenericFolderLocation = "C:\Users\user.name\Desktop"
ChDrive GenericFolderLocation
SelectedFile = Application.GetOpenFilename( _
fileFilter:="Excel Files (*.xls*), *.xls*", MultiSelect:=False)
Workbooks.Open (SelectedFile)
Set test = ActiveWorkbook
Dim SourceFolderLocation As String
Dim FileName As String
Dim SourceFile As String
MsgBox "Please select the data source file..."
SourceFolderLocation = "C\Users\user.name\Desktop"
ChDrive SourceFolderLocation
SourceFile = Application.GetOpenFilename( _
fileFilter:="Excel Files (*.xls*), *.xls*", MultiSelect:=False)
Workbooks.Open (SourceFile)
Set wkbk = ActiveWorkbook
test.Activate
'Test1
'Select empty cell in Chart template
Range("C28").Select
'Populate with pivot table data from sourceFile
ActiveCell.FormulaR1C1 = _
"=GETPIVOTDATA("" Value"",'[wkbk]ActCost_PIVOT'!R3C1,""Team"",""Field1"",""Row Descrption"",""Row1"",""Type"",""DataPoint1"")"
'Repeat for next cell
Range("C27").Select
ActiveCell.FormulaR1C1 = _
"=GETPIVOTDATA("" Value"",'[wkbk]CRActCost_PIVOT'!R3C1,""Team"",""Field1"",""Row Descrption"",""Row1"",""Type"",""DataPoint2"")"
[已解决] 问题 2。与问题 1 类似,我想制作诸如“TypeName”或“TeamName”变量之类的东西。我可以像这样在 Sub 之外声明它们吗?
Dim TeamName As String
Sub()
TeamName = "Tigers"
End Sub
谢谢你的帮助!