简体   繁体   English

我们可以将复杂的 excel 公式更改为 VBA

[英]Can we change complex excel formula into VBA

Can anyone please write this formula in VBA code, this is making the sheet heavy and whenever i add or edit raw data (Data sheet) it starts "Calculating 4 processors" and takes much time.任何人都可以在 VBA 代码中写下这个公式,这会使工作表变得很重,每当我添加或编辑原始数据(数据表)时,它都会开始“计算 4 个处理器”并花费很多时间。

In raw Data Sheet there are almsot 18000 entries and in other sheet where i am extracting the status contains 8000 entries, however it would be much helpful if it sees till the last raw.在原始数据表中有 almsot 18000 个条目,而在我提取状态的其他表中包含 8000 个条目,但是如果它看到最后一个原始数据将会很有帮助。

=IF(SUMPRODUCT((Data:$A$2:$A$17989=A7076) (Data:$B$2:$B$17989=B7076) (Data:$C$2,$C$17989="Combine")),"Available":IF(COUNTIFS(Data,$A$2,$A$17989:A7076,Data,$B$2:$B$17989,B7076,Data,$C$2:$C$17989,"Feed *")=2,"Available","Not Available")) =IF(SUMPRODUCT((数据:$A$2:$A$17989=A7076) (数据:$B$2:$B$17989=B7076) (数据:$C$2,$C$17989="组合")),"可用":IF(COUNTIFS(数据,$A$2,$A$17989:A7076,数据,$B$2:$B$17989,B7076,数据,$C$2:$C$17989,"Feed *")=2,"可用“,“无法使用”))

I have read many articles on web and tried them but not helpful and i am wondering if VBA is one of the best solution for this complex formula.我已经阅读了很多关于 web 的文章并进行了尝试但没有帮助,我想知道 VBA 是否是这个复杂公式的最佳解决方案之一。

what i have recorded is below:我记录的内容如下:

Sub Macro1() ' ActiveCell.FormulaR1C1 = _ Sub Macro1() ' ActiveCell.FormulaR1C1 = _

    "=IF(SUMPRODUCT((Data!R2C1:R17989C1=RC[-5])*(Data!R2C2:R17989C2=RC[-4])*(Data!R2C3:R17989C3=""Combine"")),""Available"",IF(COUNTIFS(Data!R2C1:R17989C1,RC[-5],Data!R2C2:R17989C2,RC[-4],Data!R2C3:R17989C3,""Feed *"")=2,""Available"",""Not Available""))"

Range("F2").Select

Selection.Copy

Range("F3:F7076").Select

ActiveSheet.Paste

Application.CutCopyMode = False

Selection.End(xlUp).Select

Range("F3").Select

ActiveWorkbook.Save

End Sub结束子

thank you谢谢

Please, test the next solution.请测试下一个解决方案。 Activate the sheet to be processed and run SetAvailability :激活要处理的工作表并运行SetAvailability

Sub SetAvailability()
   Dim sh As Worksheet, shD As Worksheet, lastRA As Long, lastRD As Long, arrD, arr, arrF, i As Long
   
   Set sh = ActiveSheet
   Set shD = Worksheets("Data")
    lastRA = sh.Range("A" & sh.rows.count).End(xlUp).Row
    lastRD = shD.Range("A" & shD.rows.count).End(xlUp).Row
    
    arrF = sh.Range("F2:F" & lastRA).Value2
    arr = sh.Range("A2:B" & lastRA).Value2
    arrD = shD.Range("A2:C" & lastRD).Value2
    
    For i = 1 To UBound(arr)
        arrF(i, 1) = getAvailability(arrD, arr(i, 1), arr(i, 2), "Combine", "Feed *")
    Next i
    
    'drop the processed aray content at once:
    sh.Range("F2").Resize(UBound(arrF), 1).Value2 = arrF
    
    MsgBox "Ready..."
End Sub

Function getAvailability(arrD, strAA, strBB, strAv As String, strFeed As String) As String
   Dim countFeed As Long, i As Long
   For i = 1 To UBound(arrD)
        If arrD(i, 1) = strAA And UCase(arrD(i, 2)) = UCase(strBB) Then
            If UCase(arrD(i, 3)) = UCase(strAv) Then getAvailability = "Available": Exit Function
            If arrD(i, 3) Like strFeed Then countFeed = countFeed + 1
            If countFeed = 2 Then getAvailability = "Available": Exit Function
        End If
   Next i
   getAvailability = "Not Available"
End Function

It should be fast enough, working only in memory and dropping the processed array content at once, at the end of the code.它应该足够快,仅在 memory 中工作,并在代码末尾立即丢弃处理过的数组内容。 And, no any workbook charge because of complicated formula...而且,因为复杂的公式,没有任何工作簿费用......

The above solution assumes that strings as "Feed *" will be at least two, for matchings in A:A and B:B, meaning that more (than two) such matches are allowed to consider the return as "Available".上面的解决方案假设作为“Feed *”的字符串至少有两个,用于 A:A 和 B:B 中的匹配,这意味着允许更多(超过两个)这样的匹配将返回视为“可用”。

It will take some time, anyhow, but I am curious how match for your specific data ranges.无论如何,这需要一些时间,但我很好奇如何匹配您的特定数据范围。

It will be faster if the availability is bigger (the iteration on "Data" sheet array stops after finding the match...).如果可用性更大,它会更快(“数据”表数组上的迭代在找到匹配项后停止......)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM