我正在处理一张纸,而我正在做的事情似乎非常缓慢且漫长。我希望有一种轻松的方法。
我想在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行中输入某些工具的某些值。用户可以决定某些工具可以用在其他项目(由列表示的项目)上,因此,他们可能希望在整个这些项目中摊销那些工具的成本。用户决定在第一个项目上跨项目1,2,3,5(第15行)使用第8行和第9行(第12行)。第13行采用项目1中单元格B8和B9中的价格值,第16行是摊销价值,即每个参考项目1,2,3 + 5的第2行成本。费用除以(1,2,3 + 5的总项目费用)*摊销价值第13行。
答案 0 :(得分:1)
如果我对您的理解正确,那么可以在B13和B16中使用以下两个功能
注意:
Application.Volatile
(这可能会减慢工作表的计算速度)。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
他们提供您的屏幕快照中显示的答案。