vb listing problem

zpierucci

New Member
Joined
Sep 5, 2019
Messages
42
I have an If loop comparing values. I am trying to move data from sheet "clean18" to sheet "monthcompare" based on the criteria. Everything in the code works. there are no errors. I just can't get the data from "clean 18" to list in "monthcompare" correctly. I would like the "clean18" data to be copied to "monthcompare" one row at a time. It is instead copying to the row associated with the clean 18 data. so I have spaces.
Code:
 Sub getsalesdata()
    
    Dim i As Integer
    Dim r2 As Integer
    Dim lrow18, lrow19, lrowmcompare As Long
    Dim range18, range19 As Range
    Dim colnum, monthselect As Variant
    
    r2 = 2
    monthselect = InputBox("Select month to compare")
    colnum = Application.WorksheetFunction.match(monthselect, Sheets("Clean18").Range("a1:n1"), 0)
    lrow18 = Sheet11.Cells(Rows.Count, 1).End(xlUp).Row
    lrow19 = Sheet12.Cells(Rows.Count, 1).End(xlUp).Row
    lrowmcompare = Sheet10.Cells(Rows.Count, 1).End(xlUp).Row
    MsgBox colnum
    Sheet10.Range("a2:b" & lrow18).Clear
    
    For i = 2 To lrow18
        If Sheets("clean18").Cells(i, colnum).Value > 0 Then
            Sheets("MonthCompare").Cells(r2, 1).Value = Sheets("Clean18").Cells(i, 1).Value
            Sheets("monthcompare").Cells(r2, 2).Value = Sheets("clean18").Cells(i, 2).Value
            End If
        r2 = r2 + 1
        
        Next i
    
    
        
    
    
    End Sub
What am I missing?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You need to move this line
Code:
r2 = r2 + 1
inside the If statement.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
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