1

I have a spreadsheet where I need to calculate the number of times something has been done to a particular area, I know that when certain criteria exists a field in another spreadsheet should be incremented.

for i to rows.length
   if Cell(A,i) == Cell(A,i+1) AND Cell(D,i) != Cell(D,i+1)
      otherspreadsheet.field++
   else if Cell(A,i) != Cell(A,i+1) 
      otherspreadsheet.field++ 

I don't know how to use vba with Microsoft excel, any help would be greatly appreciated.

+++

I was playing about with the code you provided me to get familiar and eventually figure this problem. The strYear doesn't seem to work, strYear = InputBox("enter year"). If I enter 2014, the count comes out as 0. However if I replace the code below from "strYear" to 2014 then it works.

With MySheet
For i = 4 To LastRow

    If .Cells(i, "AA").Value = "strYear" Then
        GreensFCounter = GreensFCounter + 1
    Else
        GreensFCounter = GreensFCounter
    End If
Next i
End With
MsgBox (GreensFCounter)
vinnie667
  • 109
  • 11
  • The psuedocode in your post can definitely be implemented in VBA without too much struggle, but a quick question before anyone dives in -- it looks like both the `if` and the `else` both do the same thing... that's not intentional, right? – Dan Wagner Jun 12 '14 at 00:56
  • Hey, thanks for your reply. The if statement is correct I think, the difference between the two is the first one has the AND statement, the else is due to if the two dates (column a) are different then the field should be incremented regardless – vinnie667 Jun 12 '14 at 01:20
  • My apologies, I should have been more clear... Do you want to increment the same counter, `otherspreadsheet.field`, in both cases? Or should it say be `Counter1++` for the first conditional and `Counter2++` for the second? – Dan Wagner Jun 12 '14 at 01:33
  • Oh yeah sorry, by field I meant, there is a 3 fields that need to be incremented, Cell(D,i) can contain 3 `strings`, say x, y and z. In both cases, the Cell of (D,i+1) will be used to find the field to increment. So yes, the counters are dependent on the content of Cell(D,i+1). I have 3 counters `X,Y and Z` – vinnie667 Jun 12 '14 at 01:38
  • I think you should remove the double quotes: `If .Cells(i, "AA").Value = strYear Then` – Dan Wagner Jun 14 '14 at 15:44
  • I think that's solved it, cheers! I've managed to finish it now so thanks for all the help, and I wont bother you again! – vinnie667 Jun 14 '14 at 15:48

1 Answers1

0

The code below is untested, but heavily commented -- I think it will answer your questions as it moves through each section.

A quick note though: you're comparing a cell to the next cell below it, right? On the surface that seems OK, but one corner case you need to consider is the end of the range: you'll be comparing to an empty cell.

Option Explicit
Sub CheckCellsAndTrackResults()

Dim i As Long, LastRow As Long
Dim Counter1 As Long, Counter2 As Long
Dim MySheet As Worksheet

'initialize our counter variables
Counter1 = 0
Counter2 = 0

'lets assume the comparison is happening on Sheet1
Set MySheet = ThisWorkbook.Worksheets("Sheet1")

'identify the last row for our loop
LastRow = MySheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

'NOTES:
'use a single equal sign ("=") for equivalent
'use less/greater ("<>") for not equivalent

'lets loop through rows 1 to the last row using a
'cool little VBA construct, a With...End With qualifier,
'to make some of our cell-checking easier to write:
With MySheet
    For i = 1 To LastRow
        If .Cells(1, i).Value = .Cells(1, i + 1).Value And _
        .Cells(4, i).Value <> .Cells(4, i + 1).Value Then
            Counter1 = Counter1 + 1
        ElseIf .Cells(1, i).Value <> .Cells(1, i + 1) Then
            Counter2 = Counter2 + 1
        Else
            'maybe do something else here?
        End
    Next i
End With

'finally, we can write out the counter results somewhere
'like this:
'MySheet.Cells(x, y) = Counter1
'MySheet.Cells(p, q) = Counter2

End Sub
Dan Wagner
  • 2,693
  • 2
  • 13
  • 18
  • Thanks very much I will look through it, Yes I am, I will have to have a separate case for the very 1st row. Also, I was going to have a case where it could just break the for loop if the cell below was empty. Once again thanks for your time! – vinnie667 Jun 12 '14 at 01:45
  • My pleasure, hope the above gets you where you're going – Dan Wagner Jun 12 '14 at 01:50
  • Hey, I have been trying to get it to work, however `If .Cells(27, i).Value = "strYear" Then` this part of the if statement never gets executed even though the value of the cell(27,i) should equal strYear. I have tried MsgBox'ing out the strYear and it gives me the correct output however when `MsgBox (.Cells(27,i))` it displays nothing. Any ideas ? – vinnie667 Jun 14 '14 at 14:33
  • I'm not sure I've seen that part of your code before. Can you post an example of this `"strYear"` mis-handling in `MySheet.Cells(27, i)`? – Dan Wagner Jun 14 '14 at 15:30
  • I have updated the question, I think this solution might be trivial although as I program in Java I'm not great on vba... Thanks again – vinnie667 Jun 14 '14 at 15:41