Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subtracting one cell from another using vba

Status
Not open for further replies.

vba317

Programmer
Mar 5, 2009
708
US
I am trying to write a macro to subtract one cell from another. If d5 is less than d4 I want cell G4 to have a message Low = A leg. I also want the number to be put in cell H4.

Code:
Sub FibNumbers()

    Dim i As Long
    Dim LR As Long
    Dim C(600) As Long
    'Dim D(600) As Long
    Dim D As String
    Dim G(600) As Long
    Dim ALegLowL As Double
    Dim ALegLowV As Double
    Dim ALegLowV1 As Double
    Dim ALegLowV2 As Double
    Dim ALegLowV3 As Double
    
    
     LR = ActiveSheet.UsedRange.Rows.Count
        
     For i = 2 To LR
        
     'If C(i) < C(i + 1) Then Range(G(i)).Value = C(i)
        
    
    
    ' Min Value Calculation
        'A leg Low location
        ALegLowL = i
        'A leg low header
         If Range("D" & i + 1 - "D" & i < 0) Then Range("G" & ALegLowL).Value = "Low = A leg"
        'A leg Value
         'AleglowV
        ALegLowV1 = i
        If Range("D" & i + 1 - "D" & i < 0) Then ALegLowV2 = WorksheetFunction.Min(Range(Cells(ALegLowV1, "D"), Cells(LR, "D")))
                If Range("D" & i + 1 - "D" & i < 0) Then Range("H" & ALegLowL).Value = ALegLowV2
        
        Next i
End Sub
 


Hi,

I'm not sure what you mean, but this makes no sense as an expression...
Code:
If Range("D" & i + 1 - "D" & i < 0) Then
Maybe...
Code:
If Range("D" & i + 1 ) < 0 Then
Why is the "D" in ther TWICE?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


or is it...
Code:
If Range("D" & i + 1) - Range("D" & i) < 0 Then


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I am trying to create a loop that loops through column D. the small letter i is the counter. I want the procedure to run when D5-D4 is a negative number. I thought the ("D" & i + 1) formula would work when i =5 . Your right, my thinking is wrong, which is why my program is not working. Can I resolve this problem?

Tom
 



Isn't that what I coded in my previous post?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Yes I just tried your suggestion. You did provide me with an answer to my question. I asked the wrong question thou. If many cells in a row go down I want the last cell to be displayed not all the inbetween cells. I guess I will have to try and use the min fumction, I'm not sure how thou.


Code:
If Range("D" & i + 1) - Range("D" & i) < 0 Then this seemed to work
 



I want the last cell to be displayed
Code:
MsgBox "Last value is " & Cells(1,"D").End(xlDown).Value
Assuming that D1 is the first cell with data and data is contiguous

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top