Help Modify My code - Returning same results twice

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hey guys,

I have this code (below) that looks in column C and searches for values that are greater than 0.01 and if found pastes them in column G as a list. to test I entered in two cells to have values that were greater than 0.01 Cell C5 and Cell C9 and in my results the code is entering cell C5's cell address/Value twice? Not sure what I am doing wrong.

Excel 2010

Code:
Sub FindVaraince()
Dim ws As Worksheet
Dim c As Range


Dim FinalRow As Long
FinalRow = cells(Rows.Count, 2).End(xlUp).Row 'Finds Last row and excludes header


Set ws = ActiveSheet


For Each c In ws.Range("C2:C" & FinalRow)
    
    If c.Value >= 0.01 Then Rng = c.Address
         
    If c.Value >= 0.01 Then Rng2 = c.Value
    
    ws.Range("G" & Rows.Count).End(xlUp).Offset(1).Value = Rng
    ws.Range("H" & Rows.Count).End(xlUp).Offset(1).Value = Rng2


Application.CutCopyMode = False
Application.DisplayAlerts = False
    
Next c

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You still have the values in the variable Rng and Rng2. You need to empty those variables.

Try this and see if this is what you are looking for

Code:
Sub FindVaraince()
Dim ws As Worksheet
Dim c As Range




Dim FinalRow As Long
FinalRow = Cells(Rows.Count, 2).End(xlUp).Row 'Finds Last row and excludes header




Set ws = ActiveSheet




For Each c In ws.Range("C2:C" & FinalRow)
    
    If c.Value >= 0.01 Then Rng = c.Address
         
    If c.Value >= 0.01 Then Rng2 = c.Value
    
    ws.Range("G" & Rows.Count).End(xlUp).Offset(1).Value = Rng
    ws.Range("H" & Rows.Count).End(xlUp).Offset(1).Value = Rng2
    Rng = ""
    Rng2 = ""


Application.CutCopyMode = False
Application.DisplayAlerts = False
    
Next c


End Sub
 
Upvote 0
Try this.
Code:
Sub FindVaraince()
Dim ws As Worksheet
Dim c As Range
Dim FinalRow As Long

    FinalRow = Cells(Rows.Count, 2).End(xlUp).Row    'Finds Last row and excludes header

    Set ws = ActiveSheet

    For Each c In ws.Range("C2:C" & FinalRow)

        If c.Value >= 0.01 Then
            Rng = c.Address

            Rng2 = c.Value

            ws.Range("G" & Rows.Count).End(xlUp).Offset(1).Value = Rng
            ws.Range("H" & Rows.Count).End(xlUp).Offset(1).Value = Rng2

        End If

    Next c
    
    Application.CutCopyMode = False
    Application.DisplayAlerts = False

End Sub
 
Upvote 0
^^ Alternative Code. Dont really need the Rng & Rng2 variables:

Code:
Sub FindVaraince()
    Dim ws As Worksheet
    Dim c As Range
    Dim FinalRow As Long
    
    FinalRow = Cells(Rows.Count, 3).End(xlUp).Row 'Finds Last row and excludes header
    Set ws = ActiveSheet
    
    For Each c In ws.Range("C2:C" & FinalRow)
        
        If c.Value >= 0.01 Then
             With ws.Range("G" & Rows.Count).End(xlUp)
                .Offset(1).Value = c.Address
                .Offset(1, 1).Value = c.Value
             End With
        End If
    Next c
        
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
End Sub

Caleeco
 
Upvote 0
You still have the values in the variable Rng and Rng2. You need to empty those variables.

Try this and see if this is what you are looking for

Code:
Sub FindVaraince()
Dim ws As Worksheet
Dim c As Range





Dim FinalRow As Long
FinalRow = Cells(Rows.Count, 2).End(xlUp).Row 'Finds Last row and excludes header




Set ws = ActiveSheet




For Each c In ws.Range("C2:C" & FinalRow)
    
    If c.Value >= 0.01 Then Rng = c.Address
         
    If c.Value >= 0.01 Then Rng2 = c.Value
    
    ws.Range("G" & Rows.Count).End(xlUp).Offset(1).Value = Rng
    ws.Range("H" & Rows.Count).End(xlUp).Offset(1).Value = Rng2
    Rng = ""
    Rng2 = ""


Application.CutCopyMode = False
Application.DisplayAlerts = False
    
Next c


End Sub



thanks for the help on this. It worked!
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top