Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Ayden Smith   on Feb 20 In MS Office Category.

  
Question Answered By: Connie Wallace   on Feb 20

To mimic your code  sample, something like this :

Dim rngSource As Range
Dim avarAccounts() As Variant
Dim varSum As Variant

Dim strValue As String
Dim rngCell As Range
Dim varInc As Variant

' Initialization
Set rngSource = ThisWorkbook.Sheets(1).Range("A2:A13")
avarAccounts = Array("50", "511", "512")

' Sum
varSum = 0
For Each rngCell In rngSource
strValue = rngCell.Value2
For Each varInc In avarAccounts
If Left$(strValue, Len(varInc)) = varInc Then
varSum = varSum + rngCell.Offset(0, 4).Value
Exit For
End If
Next varInc
Next rngCell

' Save Result
' ...

A better alternative would be to let Excel do the calculation for you

Dim strAccounts As String
Dim strAmounts As String
Dim avarAccounts() As Variant
Dim varSum As Variant

Dim varInc As Variant

' Initialization
strAccounts = "sheet1!$A$2:$A$13"
strAmounts = "sheet1!$E$2:$E$13"
avarAccounts = Array("50", "511", "512")

' Compute Sum
varSum = 0
For Each varInc In avarAccounts
varSum = varSum + Application.Evaluate("=SUM((LEFT(" & strAccounts & ","
& Len(varInc) & ")=" & """" & varInc & """)*(" & strAmounts & "))")
Next varInc

' Save Result
' ...

Share: 

 

This Question has 20 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on trouble with IF/OR, Left and a simple loop Or get search suggestion and latest updates.


Tagged: