kixelsid,
Here is what I came up with. Keep in mind that this code has not been tested, so I hope it works.
Also, I have alot of comments in this code. Some of it is just notes for you, others are actual lines of code commented out. Just
incase you didn't already know this, an apostrophe (') will comment out a line (or anything to the right of the '--good for putting
notes after executable code on the same line). Since you are new to Excel/VBA, read the comments before you delete them. They may
help you with picking up the VBA language faster. The comments show up in my VBA editor as green text. They probably will be green
in yours, but they could be a different color.
To get this code into your spreadsheet, open the VBA Editor, right click your spreadsheet file, click Insert?, click Module. Then
double click the new module ("Module1" if the file has no other modules) to get the blank code window for the new module. Now copy
all of the code below (from Option Explicit to End Sub) and paste it into this blank code window. Notice that I included the Option
Explicit statement at the top, above the Sub routine. This is not necessary, but I highly recomend it. This causes the compiler to
check, before the code executes, to make sure that any variable in the code is declared first. If it is not declared, the code will
not execute and you will get an error message telling you to declare the variable. If you misspell a variable in your code, the
compiler will catch it before it tries to execute. If you leave the Option Explicit statement out, when you run the code, the
compiler will automatically create a new variable for the one that was misspelled. Now you will think that you are using one
variable throughout the code when actually there are 2 different variables and this will cause errors and bugs. (Check this site
out for more info on Option Explicit:
http://www.cpearson.com/excel/DeclaringVariables.htm).
There is something that I've left out of this code and that is error handling. And it's not because I'm being rude, it is because I
have not yet mastered error handling. It really should be in here in case something trip it up, so if anyone has any suggestions
for error handling, please, all is welcomed.
I hope this helps. Please let me know if this works. If it doesn't, I will work with you to see if we can get it to. Also let me
know if you have any other questions.
Conan
Option Explicit
Sub IfThenElseLoop()
'Declare variables
'Use only one of the following running total variabls depending
'on what data type you need to keep a running total of.
'Delete or comment out the others.
Dim pintRunningTotal As Integer
' Dim plngRunningTotal As Long
' Dim psngRunningTotal As Single
' Dim pdblRunningTotal As Double
Dim pintLoopCounter As Integer
Dim pdteDateImLookingFor As Date
Dim pintValueImLookingFor As Integer 'or Long, Single, or Double (same as above)
Dim pshtSheet1 As Worksheet
Dim pshtSheet2 As Worksheet
'Initializing variables
pintRunningTotal = 0
pintLoopCounter = 3
Set pshtSheet1 = ActiveWorkbook.Sheets("Sheet1")
Set pshtSheet2 = ActiveWorkbook.Sheets("Sheet2")
'This equals 2/24/06--DateSerial(Year as integer,Month as Integer,Day as Integer)
pdteDateImLookingFor = DateSerial(2006, 2, 24)
'If you enter the date you are looking for in cell A2 on Sheet1...
'...here is another option for initializing this variable
' pdteDateImLookingFor = pshtSheet1.Range("A2").Value
pintValueImLookingFor = 991
'If you enter the value you are looking for in cell C2 on Sheet1...
' pintValueImLookingFor = pshtSheet1.Range("C2").Value
'***Remember, if you choose the optional variable initializations for the Date & Value variables***'
'***delete or comment out the other one***'
'Start of loop
For pintLoopCounter = 3 To pshtSheet1.Range("A1").Value
'Start of IF statement testing the 2 logicals
If pshtSheet1.Cells(pintLoopCounter, 1).Value = pdteDateImLookingFor And _
pshtSheet1.Cells(pintLoopCounter, 3).Value = pintValueImLookingFor Then
'***Notice the 2 lines above. The space and underscore (" _") at the end of the first line...
'***...followed by a carriage return is a way of continuing the same line of code on a new line
'***This is done just for readability. This would work just the same if you put both lines on one...
'***...and deleted the " _" & carriage return. Personally, I prefer it all being on one line,
'***but when you are emailing code, the email programs can wrap the code onto new lines making it...
'***very dificult to read.
'The Cells property used above is what causes the cell to move down a row with each time throught the loop.
'The syntax is Cells(Row as integer, Column as integer). By using pintLoopCounter in position of Row,...
'...you cause the cell in the row equal to the value of pintLoopCounter and in specified column to be...
'...referenced. Remember, Column is an integer. So Column A would be 1, B = 2, C = 3...etc.
pintRunningTotal = pintRunningTotal + pshtSheet1.Cells(pintLoopCounter, 5).Value
End If
Next pintLoopCounter
'Enter the number stored in pintRunningTotal into cell E1 on Sheet2
pshtSheet2.Range("E1").Value = pintRunningTotal
End Sub