+ Reply to Thread
Results 1 to 2 of 2

How to write a conditional statement where something occurs twice.

Hybrid View

  1. #1
    Registered User
    Join Date
    11-28-2020
    Location
    Socal
    MS-Off Ver
    10
    Posts
    7

    How to write a conditional statement where something occurs twice.

    I am trying to create a function that runs through multiple values and determines where the value in Column A is less than the value in Column B.
    This instance has to occur twice within a range of 10 cells (IE: A2:B12), with the range of values exists from A2:B300.

    This can happen immediately following one another (A2<B2, A3<B3), or it can happen infrequently (A5<B5, A12<B12).

    If this happens twice, my then statement will return the difference between the the cell value of column A in the FIRST instance, and the cell value of column B in the SECOND instance.

    IE:
    IF
    A3<B3
    A7<B7

    THEN

    "A3- B7"

    Sorry if this is confusing. English isnt my first language lol

  2. #2
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: How to write a conditional statement where something occurs twice.

    Does what you want.

    Finds first record where A<B and sets "a" as Col A value
    When next row found where A<B, Col C shows "a-Col B value",
    Resets "a" as new base value, and deducts that from third instance, then when next found, and so on.

    Option Explicit
    Dim a As Long, f As Long, j As Long, m As Long
    
    Sub DIFFERENCES()
    
    'Find last row with data
    
        With Sheet1
        f = .Cells(.Rows.Count, "A").End(xlUp).Row
        
        For m = 2 To f
        
        
    'If COl A  < Col B
            If .Cells(m, 1).Value < .Cells(m, 2).Value Then
                j = j + 1
                
    'If this is first instance, copy Col A value
                If j = 1 Then
                a = .Cells(m, 1).Value
                
    'If this is second instance, Col C is Col A - Col B
                ElseIf j = 2 Then
                .Cells(m, 3).Value = a - .Cells(m, 2).Value
                
    'Reset as first value in next pair
                j = 1
                a = .Cells(m, 1).Value
                
                End If
            End If
        
    'Find next record
        Next
        
        End With
            
    
    
    End Sub
    Hope this helps

    Ochimus
    Attached Files Attached Files
    Last edited by Ochimus; 12-09-2020 at 07:58 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Write Sub procedure to format excel and write case/if statement to text file
    By vbronton in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-06-2018, 08:26 AM
  2. Replies: 2
    Last Post: 07-09-2015, 04:25 PM
  3. Compile error: expected end of statement occurs when 'Handles' keyword is used
    By RowanB in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-28-2011, 04:27 PM
  4. how to write the conditional statement(s)
    By Solomon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-26-2009, 04:40 PM
  5. [SOLVED] How do I write a comlicated conditional statement?
    By OlYellerSnow in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-12-2006, 05:30 PM
  6. Proper way to write a conditional statement with ands
    By DKY in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 07-18-2005, 12:08 PM
  7. [SOLVED] How do I write a conditional statement in Excel to count if two c.
    By marblelife in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-16-2005, 08:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1