Add cell if

tuytuy

Board Regular
Joined
Mar 28, 2013
Messages
75
Hi,
I have a sheet that looks like this:
GSM numberExtensionGroup nameSubgroup nameName GSM numberUser ref1User Ref2Call dateCall timeCall durationDestinationZone/Country/OperatorTariffTypeSupplementary servicesIndicative tariffUsage amountCurrencyCountry of destinationAccount numberDiscount Indicator
Jan-13 xxxxxxxxxxxxUnfiled - Niet toegekend - Non attribué28-Jan-1316:58:5300:02:05478882664On SiteNormalCommunications nationales00EUR10736059N
Jan-13 xxxxxxxxxxxxUnfiled - Niet toegekend - Non attribué29-Jan-1316:47:2100:11:37493194255MobistarNormalCommunications nationalesR01.3803EUR10736059Y
Jan-13 xxxxxxxxxxxxUnfiled - Niet toegekend - Non attribué02-Jan-1321:31:2100:01:0031 111 483 392From SiteRéduitCommunications internationalesR00.06EUR10736059Y

<tbody>
</tbody>

i'd like to add up, using a macro, the "Usage amount" if the type is "Communication nationals" only.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
THen something like this but it will be much less efficient

Code:
Sub test()
Dim LR As Long, i As Long
Dim Total As Double
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
For i = 2 To LR
    If Range("O" & i).Value = "Communications nationales" Then Total = Total + Range("R" & i).Value
Next i
MsgBox Total
End Sub
 
Upvote 0
Give this a try...
Code:
Sub GetUsageAmounts()
  Dim LastRow As Long, Total As Double
  Const LookupType As String = "Communications nationales"
  LastRow = Cells(Rows.Count, "O").End(xlUp).Row
  Total = WorksheetFunction.SumIf(Range("O2:O" & LastRow), LookupType, Range("R2:R" & LastRow))
  MsgBox Total
End Sub
 
Upvote 0
It's part of a longer macro written to generate a report of a hug file every month.
Thx anyways it worked ;)
 
Upvote 0
i've gone a bit further, but i get an error with my code, for this one there are 4 conditions to the if function is the way i did ok ?

Code:
'Copy COST FOR NATIONAL CALLS
ActiveWorkbook.Sheets("Sheet6").Activate
Dim LastRow As Long, TotalN As Double
  Const LookupType As String = "Communications internationales" Or "Communications internationales" Or "Communications effectuées a l'étranger (ROAMING)" Or "Communications recues a l'étranger (ROAMING)"
  LastRow = Cells(Rows.Count, "O").End(xlUp).Row
  Total = WorksheetFunction.SumIf(Range("O2:O" & LastRow), LookupType, Range("R2:R" & LastRow))
ActiveWorkbook.Sheets("Synthesis").Activate
Range("A4").Value = "Total cost of International Communications"
Range("B4").Value = TotalN
 
Upvote 0
i've gone a bit further, but i get an error with my code, for this one there are 4 conditions to the if function is the way i did ok ?

Code:
'Copy COST FOR NATIONAL CALLS
ActiveWorkbook.Sheets("Sheet6").Activate
Dim LastRow As Long, TotalN As Double
  Const LookupType As String = "Communications internationales" Or "Communications internationales" Or "Communications effectuées a l'étranger (ROAMING)" Or "Communications recues a l'étranger (ROAMING)"
  LastRow = Cells(Rows.Count, "O").End(xlUp).Row
  [B][COLOR=#a52a2a]Total [/COLOR][/B]= WorksheetFunction.SumIf(Range("O2:O" & LastRow), LookupType, Range("R2:R" & LastRow))
ActiveWorkbook.Sheets("Synthesis").Activate
Range("A4").Value = "Total cost of International Communications"
Range("B4").Value = TotalN
You cannot do that with constants (the Const statement). What you will need to do is make LookupType a variable instead of a constant and then create a loop that iterates the four values while maintaining a running total. By the way, I see you changed my suggested variable name Total to TotalN, but you missed changing the one I highlighted in red above. See if this code works for you...
Rich (BB code):
'Copy COST FOR NATIONAL CALLS
TotalN = 0
ActiveWorkbook.Sheets("Sheet6").Activate
Dim LastRow As Long, TotalN As Double, LookupType As Variant, vItem As Variant
LastRow = Cells(Rows.Count, "O").End(xlUp).Row
LookupType = Array("Communications internationales", _
                   "Communications internationales", _
                   "Communications effectuées a l'étranger (ROAMING)", _
                   "Communications recues a l'étranger (ROAMING)")
For Each vItem In LookupType
  TotalN = TotalN + WorksheetFunction.SumIf(Range("O2:O" & LastRow), LookupType, Range("R2:R" & LastRow))
Next
ActiveWorkbook.Sheets("Synthesis").Activate
Range("A4").Value = "Total cost of International Communications"
Range("B4").Value = TotalN
NOTE: The first two items in the Array function being assigned to the LookupType variable look identical to me (see blue highlighted text above)... that is what you posted so I used it (especially since you said you had 4 items), but you will need to change one of them to whatever item type it should have been (otherwise "Communications internationales" will end up being double counted).
 
Upvote 0
i get a duplicate deceleration error ?! but can see it anywhere
Code:
'Copy COST FOR NATIONAL CALLS


ActiveWorkbook.Sheets("Sheet6").Activate
Dim LastRow As Long, TotalN As Double
  Const LookupType As String = "Communications nationales"
  LastRow = Cells(Rows.Count, "O").End(xlUp).Row
  TotalN = WorksheetFunction.SumIf(Range("O2:O" & LastRow), LookupType, Range("R2:R" & LastRow))
ActiveWorkbook.Sheets("Synthesis").Activate
Range("A3").Value = "Total cost of National Communications"
Range("B3").Value = TotalN


'Copy COST FOR INTERNATIONAL CALLS
TotalIN = 0
ActiveWorkbook.Sheets("Sheet6").Activate
Dim LastRow As Long, TotalIN As Double, LookupType As Variant, vItem As Variant
LastRow = Cells(Rows.Count, "O").End(xlUp).Row
LookupType = Array("Communications internationales", _
                   "Communications effectuées a l'étranger (ROAMING)", _
                   "Communications recues a l'étranger (ROAMING)")
For Each vItem In LookupType
  TotalIN = TotalIN + WorksheetFunction.SumIf(Range("O2:O" & LastRow), LookupType, Range("R2:R" & LastRow))
Next
ActiveWorkbook.Sheets("Synthesis").Activate
Range("A4").Value = "Total cost of International Communications"
Range("B4").Value = TotalIN
 
Last edited:
Upvote 0
i get a duplicate deceleration error ?! but can see it anywhere
Code:
'Copy COST FOR NATIONAL CALLS


ActiveWorkbook.Sheets("Sheet6").Activate
[B][COLOR=#a52a2a]Dim LastRow As Long, TotalN As Double[/COLOR][/B]
  Const LookupType As String = "Communications nationales"
  LastRow = Cells(Rows.Count, "O").End(xlUp).Row
  TotalN = WorksheetFunction.SumIf(Range("O2:O" & LastRow), LookupType, Range("R2:R" & LastRow))
ActiveWorkbook.Sheets("Synthesis").Activate
Range("A3").Value = "Total cost of National Communications"
Range("B3").Value = TotalN


'Copy COST FOR INTERNATIONAL CALLS
TotalIN = 0
ActiveWorkbook.Sheets("Sheet6").Activate
[B][COLOR=#a52a2a]Dim LastRow As Long, TotalIN As Double[/COLOR][/B], LookupType As Variant, vItem As Variant
LastRow = Cells(Rows.Count, "O").End(xlUp).Row
LookupType = Array("Communications internationales", _
                   "Communications internationales", _
                   "Communications effectuées a l'étranger (ROAMING)", _
                   "Communications recues a l'étranger (ROAMING)")
For Each vItem In LookupType
  TotalIN = TotalIN + WorksheetFunction.SumIf(Range("O2:O" & LastRow), LookupType, Range("R2:R" & LastRow))
Next
ActiveWorkbook.Sheets("Synthesis").Activate
Range("A4").Value = "Total cost of International Communications"
Range("B4").Value = TotalIN
If the above code is all in the same procedure, then the problem is the lines of code I highlighted in red... you can only declare (Dim) a variable one time with a procedure otherwise you get a "Duplicate declaration in current scope" error. Most people declare their variables at the beginning of the procedure where it is easier to spot duplicate declarations rather than posting them inline like your code above does.
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,569
Members
449,038
Latest member
Guest1337

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top