Excel

Find the pairs of cells of a selected range whose sum equals to a provided value

Ease of Use

Easy

Version tested with

2002 

Submitted by:

ALe

Description:

you'll have messages providing all the pairs of cells whose sum equals to a value you insert in an inputbox 

Discussion:

I created this code to make my job faster. Working with financial stuff, I knew the result of some indicators without knowing which cells built it. I think you'll find this code helpful if you have a result of a sum but you don't know which cells take part in it. SumFind doesn't work with merged cells. Specifically the upper left cell of a merge returns the value in the cell, but the other cells involved in the merge give a 0 value. 

Code:

instructions for use

			

Option Explicit Sub SumFind() Dim MR As Range Dim dx As Double Dim cell As Range Dim cell2 As Range Dim MySum As Double Dim i As Integer Dim k As Integer Dim stTotally As String stTotally = "The pair of ranges are:" & Chr(10) 'setting the result message k = 0 On Error GoTo CancelOption 'getting the range in which the search will be executed Set MR = Application.InputBox(Prompt:="Select the range containing the numbers", Title:="STEP1", Type:=8) If MR.Cells.Count < 2 Then MsgBox "Select an appropriate range of numbers (at least two)", vbOKOnly + vbCritical, "Error" Exit Sub End If 'getting the number you're looking for dx = Application.InputBox(Prompt:="Input the number you're looking for as the result of the sum of two numbers", Title:="STEP 2", Type:=1) If IsNull(dx) Then MsgBox "Input an appropriate number", vbOKOnly + vbCritical, "Error" Exit Sub End If If dx = 0 Then GoTo CancelOption 'find the pairs of cells For Each cell In MR For i = 1 To MR.Cells.Count Set cell2 = MR.Cells(i) MySum = (cell.Value + cell2.Value) If MySum = dx Then If cell.Address <> cell2.Address Then k = k + 1 stTotally = stTotally & Chr(10) & k & ") " & cell.Address(False, False, xlA1) & " - " & cell2.Address(False, False, xlA1) End If End If Next i Next cell 'display results Select Case stTotally Case "The pair of ranges are:" & Chr(10) MsgBox "No ranges match", vbOKOnly, "Report" Case Else MsgBox stTotally, vbOKOnly, "Report" End Select CancelOption: 'clearing Set MR = Nothing Set cell = Nothing Set cell2 = Nothing End Sub

How to use:

  1. Open an Excel Workbook
  2. Press Alt + F11 to open the Visual Basic Editor (VBE)
  3. Select INSERT > MODULE from the menubar
  4. Paste code into the right pane
  5. Make sure there's only one "Option Explicit" at the top of the code
  6. Press Alt+Q to return to Excel
  7. Save workbook before any other changes
 

Test the code:

  1. Click on the button "Run SumFind" or start the procedure "SumFind" via VBE
  2. Select the range of cells when required (by the Inputbox "STEP1")
  3. Input the value you're looking for (Inputbox "STEP2")
 

Sample File:

FindTheSum.zip 12.09KB 

Approved by mdmackillop


This entry has been viewed 134 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express