Help with an If problem in vba

Baziwan

New Member
Joined
Sep 4, 2018
Messages
32
Hi all, below is a code I use to add a quantity to a "Sales" sheet from a matching name on a "Purchase" sheet. What I need to know is how do I get it to not do it if the cell in column M of the "Sales" sheet is > ("0")

Code:
Sub Match() 
    Dim NextRw As Long
    Dim rCl As Range
    Dim Rw As Long
    Dim Amt As Long 
    Dim sFind As String
    Dim ws As Worksheet
    
    Set ws = Worksheets("Sales")
        
    With Range("Purchases")
        For Rw = 1 To .Rows.Count
            sFind = .Cells(Rw, 1).Text ' Name
            Amt = .Cells(Rw, 4).Value 'Quantity
            With ws.Range("D1", ws.Cells(Rows.Count, "D").End(xlUp)) 
            Set rCl = .Find(sFind, LookIn:=xlValues, lookat:=xlWhole)
            On Error Resume Next
            rCl.Offset(0, 2).Value = Amt 
            End With
        Next Rw
    End With
End Sub

Hope you can help as I'm a novice with vba.

Thanks in Advance
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hello Baziwan,

This should work...

Code:
Sub Match()


    Dim Amt     As Long
    Dim NextRw  As Long
    Dim rCl     As Range
    Dim Rng     As Range
    Dim Rw      As Long
    Dim sFind   As String
    Dim ws      As Worksheet
    
    Set ws = Worksheets("Sales")
    Set Rng = .Range("D1", ws.Cells(Rows.Count, "D").End(xlUp))
    
    With Range("Purchases")
        For Rw = 1 To .Rows.Count
            sFind = .Cells(Rw, 1).Text ' Name
            Amt = .Cells(Rw, 4).Value 'Quantity
            Set rCl = Rng.Find(sFind, LookIn:=xlValues, lookat:=xlWhole)
            If ws.Cells(Rw, "M") > 0 Then
                If Not rCl Is Nothing Then rCl.Offset(0, 2).Value = Amt
            End If
        Next Rw
    End With
    
End Sub
 
Upvote 0
Hi Leith, thanks for the reply.

I tried it but I got a compile error here

Code:
Set Rng = .Range("D1", ws.Cells(Rows.Count, "D").End(xlUp))

The .Range was highlighted and the error says its an invalid or unqualified reference
 
Upvote 0
Hello Baziwan,

My fault. It should be...
Code:
Set Rng = ws.Range("D1", ws.Cells(Rows.Count, "D").End(xlUp))
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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