Consulting

Results 1 to 3 of 3

Thread: Macro For Re-Coding Variables after a Specified Cut-off

  1. #1

    Macro For Re-Coding Variables after a Specified Cut-off

    Hi All,

    I currently have a fairly large data set of dichotomized data points denoting a right (1's) and wrong response (0's)

    I currently have a custom function that will start summing correct responses, but stop summing after a respondent has received four (or more) 0's in a row

    What I am looking to do next is write a macro that will re-code all of the responses after the participant has reached the four-0's-in-a-row cut-off to 0's (even if they got some questions correct after they answered four or more in a row incorrectly).

    I've attached a workbook which will hopefully clarify the issue, but any help/advice would be greatly appreciated!

    Darko
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    This runs on the active sheet:
    Sub blah()
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    For Each rw In Range("B2:AY" & LastRow).Rows
      ZeroCount = 0
      For Each cll In rw.Cells
        If cll.Value = 0 Then
          ZeroCount = ZeroCount + 1
          If ZeroCount = 4 Then
            Range(cll.Offset(, 1), rw.Cells(rw.Cells.Count)).Value = 0
            Exit For
          End If
        Else
          ZeroCount = 0
        End If
      Next cll
    Next rw
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Option Explicit
    Sub FourZeros()
        Dim r As Long, c As Long
            Dim rData As Range
            
            Application.ScreenUpdating = True
            
            Set rData = ActiveSheet.Cells(1, 1).CurrentRegion
            
            With rData
                For r = 2 To .Rows.Count
                    For c = 2 To .Columns.Count - 4
                        If Application.WorksheetFunction.Sum(.Cells(r, c).Resize(1, 4)) = 0 Then
                            Range(.Cells(r, c), .Cells(r, c).End(xlToRight)).Value = 0
                            Exit For
                        End If
                    Next c
                Next r
            End With
            
            Application.ScreenUpdating = True
            
    End Sub
    
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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