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

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
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,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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