我正在做一张工作表,我正在做的事情似乎很慢,而且喘不过气来。我希望有一种更圆滑的方法。
我想做在Excel中看起来像这样的事情,但是在VBA:- if(Search("5,",A10,1)>0,A5,0)+if(Search("4,",A10,1)>0,A4,0)+if(Search("3,",A10,1)>0,A3,0)中,等等。
我看到在vba中这样做的唯一方法是进行搜索,将数据添加到其他地方,然后在以后对其进行求和。我有一个:-
Sub AmorTotal_1()
Dim Coll As String
Dim ClTo As String, RuTo As String, ElTo As String, Jig As String, Teth As String, Admin As String, Crane As String
Dim ClToS As Integer, RuToS As Integer, ElToS As Integer, JigS As Integer, TethS As Integer, AdminS As Integer, CraneS As Integer
Coll = "B"
ClTo = 5
RuTo = 6
ElTo = 7
Jig = 8
Teth = 9
Admin = 10
Crane = 11
ThisWorkbook.Sheets(2).Range(Coll & 2).Value = ThisWorkbook.Sheets(1).Range(Coll & 2).Value
If InStr(1, Range(Coll & 12), "5,") > 0 Then
ThisWorkbook.Sheets(2).Range(Coll & 3).Value = ThisWorkbook.Sheets(1).Range(Coll & ClTo).Value
Else
ThisWorkbook.Sheets(2).Range(Coll & 3).Value = 0
End If
If InStr(1, Range(Coll & 12), "6,") > 0 Then
ThisWorkbook.Sheets(2).Range(Coll & 4).Value = ThisWorkbook.Sheets(1).Range(Coll & RuTo).Value
Else
ThisWorkbook.Sheets(2).Range(Coll & 4).Value = 0
End If
If InStr(1, Range(Coll & 12), "7,") > 0 Then
ThisWorkbook.Sheets(2).Range(Coll & 5).Value = ThisWorkbook.Sheets(1).Range(Coll & ElTo).Value
Else
ThisWorkbook.Sheets(2).Range(Coll & 5).Value = 0
End If
If InStr(1, Range(Coll & 12), "8,") > 0 Then
ThisWorkbook.Sheets(2).Range(Coll & 6).Value = ThisWorkbook.Sheets(1).Range(Coll & Jig).Value
Else
ThisWorkbook.Sheets(2).Range(Coll & 6).Value = 0
End If
If InStr(1, Range(Coll & 12), "9,") > 0 Then
ThisWorkbook.Sheets(2).Range(Coll & 7).Value = ThisWorkbook.Sheets(1).Range(Coll & Teth).Value
Else
ThisWorkbook.Sheets(2).Range(Coll & 7).Value = 0
End If
If InStr(1, Range(Coll & 12), "10,") > 0 Then
ThisWorkbook.Sheets(2).Range(Coll & 8).Value = ThisWorkbook.Sheets(1).Range(Coll & Admin).Value
Else
ThisWorkbook.Sheets(2).Range(Coll & 8).Value = 0
End If
If InStr(1, Range(Coll & 12), "11,") > 0 Then
ThisWorkbook.Sheets(2).Range(Coll & 11).Value = ThisWorkbook.Sheets(1).Range(Coll & Crane).Value
Else
ThisWorkbook.Sheets(2).Range(Coll & 11).Value = 0
End If
ThisWorkbook.Sheets(1).Range(Coll & 13).Value = ThisWorkbook.Sheets(2).Range(Coll & 3).Value + ThisWorkbook.Sheets(2).Range(Coll & 4).Value + ThisWorkbook.Sheets(2).Range(Coll & 5).Value + ThisWorkbook.Sheets(2).Range(Coll & 6).Value + ThisWorkbook.Sheets(2).Range(Coll & 7).Value + ThisWorkbook.Sheets(2).Range(Coll & 8).Value + ThisWorkbook.Sheets(2).Range(Coll & 9).Value
End Sub我需要在20列和多行中这样做,所以我希望有另一种方法。
我已经搜索了网络,找不到任何我能理解的东西(我对VBA还是新手)。
下面是一些数据的示例。总而言之,
用户在第5-11行中键入某些工具的某些值。用户可以决定某些工具可以用于其他项目(由列表示的项目),因此他们可能希望在整个这些项目中摊销这些工具的成本。在第一项中,用户决定第8行和第9行(第12行)可以跨项目1、2、3、5(第15行)使用。第13行取项目1中单元格B8和B9中的价格值,第16行为摊销值,即每个参考项目1、2、3+ 5的第2行的成本。这是由该项目总成本除以(1,2,3+5的总项目成本)*摊销值第13行完成的。
希望这有意义,这是有点棘手的解释。

发布于 2020-05-01 12:05:36
如果我正确地理解了您,下面是两个可以在B13和B16中使用的函数
注:
由于公式中没有直接引用各种成本项目,因此如果更改,公式将不会自动更新。如果更改了行列表或项目单元格列表,则公式将更新:在每个函数的声明语句之后添加references的
Application.Volatile (这可能会减慢工作表中列/行号参数的范围references)。
Option Explicit
'sRows is the cell (or string) containing the commas separated row numbers
'lItemCol is the column number on which to run this calculation.
' If omitted, it will default to the column containing the formula
Function toolCostToAmortize(sRows As String, Optional lItemCol As Long = 0) As Currency
Dim v
Dim P As Currency
If lItemCol = 0 Then lItemCol = Application.Caller.Column
For Each v In Split(sRows, ",")
P = P + Cells(v, lItemCol)
Next v
toolCostToAmortize = P
End Function
'======================================
'sItemIDX is the cell containing the comma-separated string to use
'cCostToAmortize is the cell reference containing the toolCostToAmortize result
'lItemRow is optional and is the row containing the list of Items (row 1 in your screenshot)
' If omitted, it will default to row 1
Function amortizedValue(sItemIDX As String, cCostToAmortize As Currency, Optional lItemRow As Long = 1) As Currency
Dim v
Dim P As Currency
For Each v In Split(sItemIDX, ",")
'Assuming Item List starts in column B
' But could use other methods to locate table
P = P + Cells(lItemRow + 1, v + 1)
Next v
amortizedValue = cCostToAmortize / P * Cells(lItemRow + 1, Application.Caller.Column)
End Function他们提供的答案显示在你的截图。
https://stackoverflow.com/questions/61541716
复制相似问题