How To Add Only Distinct Positive Values with excel VBA?
Tags: toaddonlydistinctpos, vbamacros
Published: 2021-09-23 02:11:43

---
title: How To Add Only Distinct Positive Values with excel VBA?
published: true
date: 2021-09-22 08:48:20 UTC
tags: ToAddOnlyDistinctPos,VBAMacros
canonical_url: https://geekexcel.com/how-to-add-only-distinct-positive-values-with-excel-vba/
---

In this article, you will learn how to **add only distinct positive values** with Excel VBA. Let’s see them below!! Get the official version of  **MS Excel**  from the following link: [https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)

## Example

- Firstly, you need to  **create**   **a sample data**  with **values** in an Excel Workbook.

![](https://geekexcel.com/wp-content/uploads/2021/09/Sample-data-30.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```

Function AddUnique(ByRef inputrange As Range, _
Optional IgnoreText As Boolean = True, _
Optional IgnoreError As Boolean = True, _
Optional IgnoreNegativenumbers As Boolean = True)
Dim distinctnumbers As Double
Dim cell As Range
Dim dict As Object
Set dict = CreateObject("Scripting.dictionary")
distinctnumbers = 0
    For Each cell In inputrange.Resize(inputrange.Rows.Count, 1)
        cval = cell.Value
        If IgnoreText Then
           If Not (VBA.IsNumeric(cval)) Then cval = 0
        Else
           AddUnique = CVErr(0)
           Exit Function
        End If
        If IgnoreError Then
           If IsError(cval) Then cval = 0
        Else
           AddUnique = CVErr(1)
           Exit Function
        End If
        If IgnoreNegativenumbers Then
           If cval < 0 Then cval = 0
        Else
           AddUnique = CVErr(2)
           Exit Function
        End If
        If Not dict.Exists(cval) Then
           dict.Add cval, cval
           distinctnumbers = distinctnumbers + cval
        End If
    Next
AddUnique = distinctnumbers
End Function

```

- You need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-66.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to use the **formula** given below in **cell C2**.

```
=AddUnique(A2:A6)
```

![](https://geekexcel.com/wp-content/uploads/2021/09/Use-Formula-1.png)<figcaption>Use Formula</figcaption>

- Finally, you will receive the **output** as given below in **Excel**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Output-31.png)<figcaption>Output</figcaption>

- To check whether **code ignores repeated value or not** , you have to **change the value in cell A1 to 50** & see the result.

![](https://geekexcel.com/wp-content/uploads/2021/09/Output1-5.png)<figcaption>Changed value Output</figcaption>

## Sum-Up

In the above short tutorial, we have illustrated how to **add only distinct positive values** with Excel VBA. Hope you like this article. Please feel free to state your **query** or **feedback** for the above article. Thank you so much for Reading!! To learn more, check out **[Geek Excel](https://geekexcel.com/)!! **and [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Related Articles:**

- **[How to Count All Distinct Values in a Range in Excel 365? - Kutools](https://geekexcel.com/how-to-count-all-distinct-values-in-a-range-in-excel-365-kutools/)**
- **[Excel Formulas To Count All Distinct Values in a Range in Excel 365!!](https://geekexcel.com/excel-formulas-to-count-all-distinct-values-in-a-range-in-excel-365/)**
- **[Formulas to Count Cells that contain Positive Numbers in Excel!!](https://geekexcel.com/formulas-to-count-cells-that-contain-positive-numbers-in-excel/)**
- **[How to Separate Negative and Positive Values From a Column in Excel?](https://geekexcel.com/how-to-separate-negative-and-positive-values-excel/)**
- **[How to Change the Signs of Numbers in Excel Office 365? - Quick Steps!!](https://geekexcel.com/how-to-change-the-signs-of-numbers-in-excel-office-365-quick-steps/)**