2015-06-24 102 views
0

我在Excel中使用宏記錄器來記錄我需要自動化的一系列任務。這些任務發生在兩個不同的Excel電子表格之間。我相信宏在當前工作表上執行代碼,但現在第二張表是硬編碼的。我如何提示用戶選擇一個Excel工作表來引用?VBA提示用戶選擇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 
+1

它看起來像[此線索](代碼http://stackoverflow.com/questions/5600533/how-do-i-prompt-用戶選擇文件和工作表時使用宏到impor)可能能夠幫助你。看看第一個答案,那會讓你開始。你熟悉VBA嗎? – BruceWayne

回答

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 
+0

嘿!非常感謝。 VBA新手如此努力學習 –

+0

不客氣。起初很混亂,但你會得到它的縈繞。 –

+0

在這種情況下,InputBox是否可以選擇使用哪個excel工作簿?我需要將宏中的Excel工作簿路徑替換爲所選文件的路徑。 –