我正在使用VBA嘗試從主子例程調用一系列子例程。當我將所有的子程序與類似於下面的代碼結合起來時,我得到#N/A表示公式被忽略的單元格。調用VBA子程序在從另一個子程序內部調用時不起作用
Sub Main()
'Turn off autocalculation
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False
'*********************************************************
'A bunch of other code
'*********************************************************
Call Sub_Routine1
Call Sub_Routine2
Call Sub_Routine3
Call Sub_Routine4
Call Sub_Routine5
Call Sub_Routine6
Call Sub_Routine7
'This is the sub routine that is not working correctly
Call Material_Formulas
'Turn back on the autocalculation function
Application.Calculation = xlAutomatic
'*********************************************************
'A bunch of other code
'*********************************************************
Application.DisplayStatusBar = True
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
當我刪除從主要子例程Material_Formulas子程序,並分別使用下面的腳本來運行它,執行它,因爲它是假設,看起來像圖像波紋管。
Private Sub Material_Formulas()
'Turn on manual calculation
Application.Calculation = xlCalculationManual
Dim lRow As Integer
Dim tiesMaterial As String
Dim result As String
lRow = Sheets("Material").Range("A2").End(xlDown).Row
lCol = Sheets("Material").Range("A2").End(xlToRight).Column
'Starts the count at column CU
endCount = lCol - 1
For c = 99 To endCount
For r = 3 To lRow
tiesMaterial = Cells(r, 87).Value
'Looks to see if the cells starting at CU2 contains a number and then iterates through each cell in row 3 to add a formula
If tiesMaterial = "TIES MATERIAL" Then
'Defines the unique ID and calendar year cells for the index-match-match function
materialID = Sheets("Material").Cells(r, "CQ").Address(False, False)
materialYear = Sheets("Material").Cells(2, c).Address(False, False)
'Starting in cell CU3 it adds the formula =INDEX(BOM_Summary_Array,MATCH(CQ3,BOM_Summary_ID,0),MATCH(CU2,BOM_Summary_Head,0))
Sheets("Material").Cells(r, c).Formula = "=INDEX(BOM_Summary_Array,MATCH(Material!" & materialID & ",BOM_Summary_ID,0),MATCH(Material!" & materialYear & ",BOM_Summary_Head,0))"
End If
Next r
Next c
'Turn on the auto calculation function
Application.Calculation = xlAutomatic
End Sub
我在做什麼錯?當我手動和獨立地選擇它時它如何運行良好,但是當我將它與其他子例程結合時它失敗了?
當你說*「失敗」*你的意思是它什麼都不做或者拋出一個錯誤** ?? ** –
單元格中的公式是什麼?如果不是正確的公式應該是什麼? –
跨越第2行的實際日期的格式爲「yyyy」或2012年至2021年的整數? – Jeeped