1

My data has two columns.
Column C (Alphabets - A, B, C ...)
Column D (Value - 1, 2, 3 ...).
Each alphabet has its corresponding value.

I am trying to locate two cells (e.g. "G" to "J") from column C to set the range, then use this range to sum the corresponding values in column D (i.e. 7 + 8 +9 + 10 = 34).

I tried VLOOKUP and MATCH. The MATCH code returned Error 2015.

Sub loop1()
    'Dynamic range for cells with data
    Dim LastRow As Long
    LastRow = Worksheets("HU").Cells(Rows.Count, 2).End(xlUp).Row '
    LastRow1 = LastRow - 1
    Rng = "C1:D" & LastRow1
    matchrng = "C1:C" & LastRow1

    'Locate start cell in data
    alphabet_start = "G"
    locate_start = Application.Match(alphabet_start, matchrng, 0)

    'Determine end cell
    alphabet_end = "J"
    'WIP_end = Application.VLookup(alphabet_end, myrange, 2, False)
End Sub
Community
  • 1
  • 1
firezen
  • 15
  • 6
  • Do you want to use VBA for this? You can use Excel formula for this as well. For example `=IFERROR(SUM(INDIRECT("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0))),"Not Found")` – Siddharth Rout May 20 '21 at 04:54
  • Wow. I didnt expect to that this could be done with just formulas... This gave me the same output as the code! Thanks a lot Sir! – firezen May 20 '21 at 05:12
  • one more question.. if i want to present the sum in another sheet in the same workbook, how should i change the formula? – firezen May 20 '21 at 05:21
  • I have put together an answer which explains it all (I hope?). – Siddharth Rout May 20 '21 at 05:32
  • Was trying to understand the formula with the limited knowledge I have, but your explanation cleared all my doubts. Thank you!! – firezen May 20 '21 at 05:40

2 Answers2

2

You can achieve what you want using Excel formulas as well.

=IFERROR(SUM(INDIRECT("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0))),"Not Found")

To understand this, let's create a basic formula and then break up the formula to understand how it works.

Let's say, your worksheet looks like

enter image description here

So what we want is =SUM(D7:D10)? So let's break this up.

The formula can be broken up as =SUM("D" & "7" & ":D" & "10"). Do not yet directly enter this in the cell. It will not work. Ok next thing is to make it dynamic so that we can get the 7 & 10. Now 7 is the position of G and 10 is the position of J which we can get using MATCH. For example

MATCH("G",C:C,0) and
MATCH("J",C:C,0)

So the formula can now be written as

=SUM("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0))

"D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0) is just a string at the moment. You have to use INDIRECT to tell excel to consider it as a range. So the string now becomes

INDIRECT("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0))

and then putting it inside the SUM formula.

SUM(INDIRECT("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0)))

One final thing. The MATCH can return an error if no match is found so we can tackle that using =IFERROR(FORMULA,"ERROR MESSAGE")

So we get our final formula

=IFERROR(SUM(INDIRECT("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0))),"Not Found")

one more question.. if i want to present the sum in another sheet in the same workbook, how should i change the formula?

Let's say the data is in Sheet1 so the formula in another sheet will look like

=SUM(Sheet1!D7:D10)

So now follow the above logic and you will get

=IFERROR(SUM(INDIRECT("Sheet1!D" & MATCH("G",Sheet1!C:C,0) & ":D" & MATCH("J",Sheet1!C:C,0))),"Not Found")

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
1

You were heading in right direction firezen. Below mentioned code is what you desire.

Sub loop1()
    'Dynamic range for cells with data
    Dim LastRow As Long, locate_start As Variant, locate_end As Variant, matchrng As Range
    Dim alphabet_start As String, alphabet_end As String

    LastRow = Worksheets("HU").Cells(Rows.Count, 3).End(xlUp).Row '<== Last row of data in Column C
    Set matchrng = ThisWorkbook.Worksheets("HU").Range("C1:C" & LastRow) '<== Set range to Search
    
    'Locate start cell in data
    alphabet_start = "G"
    locate_start = Application.Match(alphabet_start, matchrng, 0) '<== First position
    
    'Determine end cell
    alphabet_end = "J"
    locate_end = Application.Match(alphabet_end, matchrng, 0) '<== Last Position
    
    'If not is error to handle error If alphabet_start or alphabet_end is not found
    If Not IsError(locate_start) And Not IsError(locate_end) Then
        ' Here output is at the end of column D after empty space. You may change as required
        ThisWorkbook.Worksheets("HU").Range("D" & LastRow + 2) = _
        Application.Sum(ThisWorkbook.Worksheets("HU").Range(Cells(locate_start, 4), Cells(locate_end, 4)))
    End If
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
Anmol Kumar
  • 157
  • 1
  • 7
  • Nicely done. One small suggestion. :) Fully qualify your range. You many want to see [Why does Range work, but not Cells?](https://stackoverflow.com/questions/17733541/why-does-range-work-but-not-cells) – Siddharth Rout May 20 '21 at 04:47
  • Thanks siddharth. I would look into it. I just copied the code tried to make minimum changes – Anmol Kumar May 20 '21 at 04:59