我在Excel中使用Macro Recorder来记录我需要自动化的一系列任务。这些任务发生在两个不同的Excel电子表格之间。我相信宏在当前工作表上执行代码,但是现在第二张表是硬编码的。如何提示用户选择要引用的Excel工作表?
将拥有宏的工作簿是MasterHardwareDB
&需要用用户输入替换的文件是Computer&DeploymentInfo_06_23_15_v3.xlsx
我正在研究filedialog对象,但我不确定如何在这里集成。
Sub AutomateCompare()
'
' AutomateCompare Macro
'
ActiveCell.Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[scheduleddate],MATCH([ @HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
ActiveCell.Offset(1, 0).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[scheduleddate],MATCH([@HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
ActiveCell.Offset(1, 0).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveWindow.SmallScroll ToRight:=-1
ActiveCell.Offset(-2, 1).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[forecastdate],MATCH([@HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
ActiveWindow.SmallScroll ToRight:=1
ActiveCell.Offset(1, 0).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveWindow.SmallScroll ToRight:=-7
ActiveSheet.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=9, _
Criteria1:="FALSE"
ActiveWindow.SmallScroll ToRight:=1
ActiveCell.Offset(4169, -4).Range("MasterHardwareDB[[#Headers],[Name]:[EmpID]]") _
.Select
ActiveCell.Offset(521, 1).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveWindow.SmallScroll ToRight:=-7
ActiveSheet.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=5, _
Criteria1:="=Scheduled", Operator:=xlOr, Criteria2:="=To Be Scheduled"
ActiveCell.Offset(87, -8).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveWindow.SmallScroll ToRight:=8
End Sub
答案 0 :(得分:0)
最简单的方法是
shtNum = InputBox("Enter the number of the sheet you want to use.")
With Sheets(shtNum)
Your code here
End With
另外,您知道,宏录制器代码包含大量垃圾。这是代码的清理版本。
Sub AutomateCompare()
With ActiveSheet
'Change the ranges to what you need. Using activecell is usually dangerous
.Range("A1:A2").FormulaR1C1 = "=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[scheduleddate],MATCH([ @HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
.Range("B1").FormulaR1C1 = "=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[forecastdate],MATCH([@HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=9, Criteria1:="FALSE"
.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=5, Criteria1:="=Scheduled", Operator:=xlOr, Criteria2:="=To Be Scheduled"
End With
End Sub
编辑:这是打开另一个工作簿的代码
Sub AutomateCompare()
Dim fileBrowse As FileDialog
Dim shtNum As Integer
Set fileBrowse = Application.FileDialog(msoFileDialogOpen)
If fileBrowse.Show = True Then wbPath = fileBrowse.SelectedItems(1)
With Workbooks.Open(wbPath)
shtNum = InputBox("Enter the number of the sheet you want to use.")
With .Sheets(shtNum)
'Change the ranges to what you need. Using activecell is usually dangerous
.Range("A1:A2").FormulaR1C1 = "=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[scheduleddate],MATCH([ @HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
.Range("B1").FormulaR1C1 = "=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[forecastdate],MATCH([@HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=9, Criteria1:="FALSE"
.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=5, Criteria1:="=Scheduled", Operator:=xlOr, Criteria2:="=To Be Scheduled"
End With
End With
End Sub