Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 协助实施基于大型列表中的多个条件的值选择和计算以及类型不匹配错误_Excel_Vba - Fatal编程技术网

Excel 协助实施基于大型列表中的多个条件的值选择和计算以及类型不匹配错误

Excel 协助实施基于大型列表中的多个条件的值选择和计算以及类型不匹配错误,excel,vba,Excel,Vba,我的文档包含多个工作表,其中三个涉及我的声明。在表1(预测)中,一行(x轴)中有多个等级值,它们形成了一个相交的单元格网格,其中有一个物品(材料成分编号)列(y轴)。在“等级值”行上方是另一个对应的Alt编号行,这些编号仅对列是唯一的(列中对齐的每个单元格共享相同的Alt)。Sheet2(BOMLIST)包含包含物料编号、等级值、alt编号、物料成分编号、批次数量和批次大小的列。表3包含物料编号列和相应的批次大小列。Sheet2上的“批次大小”列包含一个索引/匹配公式,该公式将单元格中Sheet

我的文档包含多个工作表,其中三个涉及我的声明。在表1(预测)中,一行(x轴)中有多个等级值,它们形成了一个相交的单元格网格,其中有一个物品(材料成分编号)列(y轴)。在“等级值”行上方是另一个对应的Alt编号行,这些编号仅对列是唯一的(列中对齐的每个单元格共享相同的Alt)。Sheet2(BOMLIST)包含包含物料编号、等级值、alt编号、物料成分编号、批次数量和批次大小的列。表3包含物料编号列和相应的批次大小列。Sheet2上的“批次大小”列包含一个索引/匹配公式,该公式将单元格中Sheet2上的物料编号与Sheet3上的相应物料编号相匹配,然后从Sheet3的批次大小中提取,并将其除以1000。并非表2上的每个材料编号都列在表3上,因为并非所有材料都在使用中。这导致表2中批次大小列中的许多单元格填充了#N/A

(1) 这就引出了我的第一个问题,在填充网格的某个时刻,我得到了一个“类型不匹配”错误。这可能是什么原因造成的?有没有办法用if语句编写索引/匹配函数,用0代替N/a

我的代码应该做的是,计算批次大小乘以BOMLIST上的数量,并将该值返回到网格内预测表上的相应单元格。它逐个单元格,检查在该点相交的坡度和材质(物品)编号。然后,通过将预测单元格中的物料成分编号(MatComp)和等级与BOM表上的MatComp和等级值进行比较,筛选并选择要在BOM表上乘以的批次大小和数量

(2) 我的第二个问题是,我不知道如何实现两个不同的特性。现在我的陈述只是比较了每页的分数和材料组成值。我需要添加一个也按Alt值过滤的部件。因此,现在可以通过比较X轴(行)上的Alt和Grade以及Y轴(列)上的MatComp值来选择批次大小和数量。BOMLIST上偶尔也会出现多个项目。例如,某些项目可以具有相同的选择标准(alt、grade、matcomp和批次大小),但数量不同。在这种情况下,我想添加Quantity值并乘以相同的批量大小。有人能帮我解决这个问题,或者给我指出正确的方向吗?我尝试合并一个附加的With和If语句,但似乎不起作用

谢谢

这是我的密码:

Sub Work_test()
Dim wb As Workbook
Dim ForeCast As Worksheet
Dim BomList As Worksheet
Dim Grade  As Variant
Dim MatComp As Variant
Dim ALT As Integer
Dim BomGrade As Variant
Dim StartRng As Range
Dim rng As Range
Dim FormatRuleInput As String
Dim ColumnLetter As String
Dim GradeCheck As Long
Dim Count As Integer
Dim GradeBomAddress As String
Dim BomMatComp As Variant
Dim BomMatCompRng As Variant
Dim BomQuantityRng As Variant
Dim Quantity As Variant
Dim BomBatchRng As Variant
Dim Batch As Variant
Dim Calc As Variant
    
    
    Set wb = ThisWorkbook
    Set ForeCast = wb.Worksheets("Forecasting")
    Set BomList = wb.Worksheets("BOMLIST")


    'PURPOSE: Obtain A Number Format Rule From A Cell User's Determines


    'Get A Cell Address From The User to Get Number Format From
    On Error Resume Next
        Set rng = Application.InputBox( _
        Title:="Number Format Rule From Cell", _
        Prompt:="Select a cell to pull in your number format rule", _
        Type:=8)
    On Error GoTo 0

    'Test to ensure User Did not cancel
    If rng Is Nothing Then Exit Sub
  


    'Apply NumberFormat To User Selection
    If TypeName(Selection) = "Range" Then
        Selection.NumberFormat = FormatRuleInput
    Else
        MsgBox "Please select a range of cells before running this macro!"
    End If


    'Debug.Print rng.Address

    For Each cell In rng
        'Debug.Print cell.Column
         ColumnLetter = Split(Cells(1, cell.Column).Address, "$")(1)
         Debug.Print ColumnLetter


        Grade = ForeCast.Cells(4, cell.Column)
        'Debug.Print Grade
        MatComp = ForeCast.Cells(cell.Row, ForeCast.Cells.Find("Article").Column)
        'Debug.Print MatComp
        ALT = ForeCast.Cells(3, cell.Column)
        'Debug.Print ALT
        
        'Count variable counts all the used rows and gives you a numberic value. This value can then be used to define the last cell row number in a range
        Count = BomList.UsedRange.Rows.Count
        'The three variables below are used to grab the column number for each of their respective values. This can be used to set up a cell location for a specific value when you find a match.
        'If the column header changes you must change it in this code or you will get an error.
        BomMatCompRng = BomList.Cells.Find("Mat-comp", lookat:=xlWhole).Column
        BomQuantityRng = BomList.Cells.Find("Quantity", lookat:=xlWhole).Column
        BomBatchRng = BomList.Cells.Find("Batch Size", lookat:=xlWhole).Column
        
        
        'This "with" statment defines the range of cells which we will use the find function with in. If you move the grade code  varibles outside of column G you must change it here or you will get wrong calculation.
        With BomList.Range("A1" & ":" & "G" & Count)
            Set StartRng = .Find(Grade, lookat:=xlWhole)
            If Not StartRng Is Nothing Then
                GradeBomAddress = StartRng.Address
                Do
                    'The code below uses the found column number along with the current row of the cell that has the corrosponding grade code to check if it matches with the Mat-Comp number if it does then it will calculate the correct batch size amount.
                    Debug.Print StartRng.Address
                    BomMatComp = BomList.Cells(StartRng.Row, BomMatCompRng)
                    Debug.Print BomMatComp
                    If BomMatComp = MatComp Then
                        Quantity = BomList.Cells(StartRng.Row, BomQuantityRng)
                        Batch = BomList.Cells(StartRng.Row, BomBatchRng)
                        Calc = Quantity * Batch
                        cell.Value = Calc
                        Debug.Print Quantity
                        Debug.Print Batch
                        Debug.Print Calc
                    End If
                    Set StartRng = .FindNext(StartRng)
                 'The code will loop through all of the Cell that contain the Grade code and check to see if they have a matching Mat-Comp number if not the value will be denoted as zero
                Loop While Not StartRng Is Nothing And StartRng.Address <> GradeBomAddress And BomMatComp <> MatComp
            End If
        End With
        If BomMatComp <> MatComp Then
            cell.Value = 0
        End If
    Next cell

End Sub
Sub-Work\u test()
将wb设置为工作簿
作为工作表的Dim预测
作为工作表的Dim BomList
变差等级
Dim MatComp作为变型
将ALT设置为整数
Dim BomGrade作为变体
暗启动As范围
变暗rng As范围
Dim FormatRuleInput作为字符串
将字母变为字符串
尽可能长的检查
将计数设置为整数
朦胧的衣服像绳子一样
Dim BomMatComp作为变型
Dim BomMatCompRng作为变型
Dim Bomquantityring作为变体
变暗量
Dim BomBatchRng作为变体
将批处理作为变量
Dim Calc作为变体
设置wb=ThisWorkbook
设置预测=wb.工作表(“预测”)
设置BomList=wb.工作表(“BomList”)
'目的:从单元格用户的列表中获取数字格式规则
'从用户处获取要从中获取数字格式的单元格地址
出错时继续下一步
设置rng=应用程序输入框(_
标题:=“单元格中的数字格式规则”_
提示:=“选择要拉入数字格式规则的单元格”_
类型:=8)
错误转到0
'测试以确保用户未取消
如果rng为空,则退出Sub
'将NumberFormat应用于用户选择
如果TypeName(选择)=“范围”,则
Selection.NumberFormat=FormatRuleInput
其他的
MsgBox“请在运行此宏之前选择一系列单元格!”
如果结束
'Debug.Print rng.Address
对于rng中的每个单元
'Debug.Print cell.Column
ColumnLetter=拆分(单元格(1,cell.Column).地址,“$”(1)
调试。打印一封信
等级=预测。单元格(4,单元格。列)
'Debug.Print Grade
MatComp=ForeCast.Cells(cell.Row,ForeCast.Cells.Find(“文章”)列)
'Debug.Print MatComp
ALT=ForeCast.Cells(3,cell.Column)
'Debug.Print ALT
'Count变量对所有使用的行进行计数,并给出一个数值。然后,该值可用于定义范围内的最后一个单元格行号
Count=BomList.UsedRange.Rows.Count
'下面的三个变量用于获取各自值的列号。这可用于在找到匹配项时为特定值设置单元格位置。
'如果列标题更改,则必须在此代码中更改它,否则将出现错误。
bommatcomperng=BomList.Cells.Find(“Mat comp”,lookat:=xlother).列
BomQuantityRng=BomList.Cells.Find(“数量”,lookat:=xlWhole.Column
BomBatchRng=BomList.Cells.Find(“批量大小”,lookat:=xlWhole).列
“This”with“station定义了我们将在中使用find函数的单元格范围。如果将等级代码变量移到G列之外,则必须在此处进行更改,否则将得到错误的计算结果。
带BomList.范围(“A1”和“:”和“G”和计数)
设置StartRng=.Find(等级、外观:=xlother)
如果不是开始,那就什么都不是了
GradeBomAddress=起始地址
做
'下面的代码使用找到的列号以及具有腐蚀等级的单元格的当前行