VBA还是新手。如果INDEX / MATCH没有返回值,我尝试使用InputBox结果作为条件。将是一个4个字符的字符串。我发现的所有结果都是数字或范围,没有一个是将值作为错误条件传递。
我觉得我90%的方式是因为 inpMfrDiv 在运行宏时在公式栏中显示用户输入,但在工作表中显示#NAME?错误。 inpMfrCode 根本不起作用,只是将公式放入单元格中。
这是宏的精简版。任何帮助将不胜感激。谢谢!
Sub PriceFileThings5()
Dim wb As Workbook
Dim lastRow As Long
Dim rngDynamicO, rngDynamicP, rngDynamicQ, rngDynamicR, rngDynamicS, rngDynamicT As Range
Dim inpMfrCode As String
Dim inpMfrDiv As String
lastRow = Sheets("Contract").Range("N1").End(xlDown).Row
Set wb = ActiveWorkbook
Set rngDynamicS = wb.Sheets("Contract").Range("S2:S" & lastRow)
Set rngDynamicT = wb.Sheets("Contract").Range("T2:T" & lastRow)
Application.DisplayAlerts = False
Call OptimizeCode_Begin
wb.Names.Add Name:="rngReturnR", _
RefersTo:="=ItemMaster_Matches!$D$2:INDEX(ItemMaster_Matches!$D:$D, COUNTA(ItemMaster_Matches!$D:$D))"
wb.Names.Add Name:="rngReturnS", _
RefersTo:="=ItemMaster_Matches!$E$2:INDEX(ItemMaster_Matches!$E:$E, COUNTA(ItemMaster_Matches!$E:$E))"
wb.Names.Add Name:="rngReturnT", _
RefersTo:="=ItemMaster_Matches!$F$2:INDEX(ItemMaster_Matches!$F:$F, COUNTA(ItemMaster_Matches!$F:$F))"
'NEED USER INPUT BOX FOR THESE RANGES
'*************************
With rngDynamicS
inpMfrCode = InputBox("Please enter the 4 character Manufacturer Code")
.Formula = "=IFERROR(INDEX(rngReturnS, MATCH(Contract!$A2, rngLookUp, FALSE)), " & inpMfrCode & ")"
lastRow = Range("N1:N" & Range("N1").End(xlDown).Row).Rows.Count
Range("S2" & ":S" & lastRow).FillDown
'Application.Calculate
End With
With rngDynamicT
inpMfrDiv = InputBox("Please enter the 4 character Manufacturer Division")
.Formula = "=IFERROR(INDEX(rngReturnT, MATCH(Contract!$A2, rngLookUp, FALSE)), " & inpMfrDiv & ")"
lastRow = Range("N1:N" & Range("N1").End(xlDown).Row).Rows.Count
Range("T2" & ":T" & lastRow).FillDown
'Application.Calculate
End With
'*************************
ActiveSheet.Calculate
Call OptimizeCode_End
Application.DisplayAlerts = True
End Sub
答案 0 :(得分:1)
您将字符串inpMfrCode
放入公式中,而不将其包装在引号中。
也就是说,它创建的公式如下:
=IFERROR(INDEX(rngReturnS, MATCH(Contract!$A2, rngLookUp, FALSE)), ABCD)
当你真正想要它写:
=IFERROR(INDEX(rngReturnS, MATCH(Contract!$A2, rngLookUp, FALSE)), "ABCD")
尝试将.Formula
行换成:
.Formula = "=IFERROR(INDEX(rngReturnS, MATCH(Contract!$A2, rngLookUp, FALSE)), """ & inpMfrCode & """)"
和
.Formula = "=IFERROR(INDEX(rngReturnT, MATCH(Contract!$A2, rngLookUp, FALSE)), """ & inpMfrDiv & """)"