Error setting value of variable1 to value of variable2

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
367
Office Version
  1. 2019
I have a snippet of code as part of a much larger macro. In the snippet below I am getting a Compile Error: Object Required on the line Set PrevTROrderNo = TROrderNo
I want to check if the new instance of TROrderNo is the same as the last one. If the above is not the way to do this, what can I use?

The intent of this snippet is to look through a list of order numbers to see if that order number appears x times and then modify another workbook's data by dividing the Quantity by x.
So, for each cell in the relevant range, TROrderNo is the cell's value. Count how many times that value appears in the table (PayCount). If it appears more than once then find those occurrences in another workbook and divide the Quantity value by the PayCount. At the end of this I want the code to remember the TROrderNo it has been looking at, so when it gets the next TROrderNo in the list, the code can skip over a TROrderNo it has already handled.

VBA Code:
Dim TROrder As Range, TROrderNo As String, PrevTROrderNo As String, PayCount As Integer, FoundOrder As Range, firstAddress As Range, InitialQty As Integer

For Each TROrder In TypeReport.ActiveSheet.Range("D" & TypeRepFirstRow, "D" & TypeRepLastRow)
    TROrderNo = Range(TROrder).Value
    If Not PrevTROrderNo = TROrderNo Then
        PayCount = Application.CountIf(TypeReport.ActiveSheet.UsedRange, cell.Value)
        If PayCount > 1 Then
            With ExpReport.ActiveSheet.Range("A" & ExpFirstOrderRow, "A" & ExpLastOrderRow)
                Set FoundOrder = .Find(TROrderNo, LookIn:=xlValues)
                If Not FoundOrder Is Nothing Then
                    firstAddress = FoundOrder.Address
                    Do
                        InitialQty = FoundOrder.Offset(0, 3).Value
                        FoundOrder.Offset(0, 3).Value = InitialQty / PayCount 'Re-write Qty according to number of payments
                        Set FoundOrder = .FindNext(FoundOrder)
                    Loop While Not FoundOrder Is Nothing And FoundOrder.Address <> firstAddress
                End If
            End With
        End If
    End If
    Set PrevTROrderNo = TROrderNo
Next TROrder
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
As PrevTROrderNo is a string type there is no need to use 'Set'

Try as 'PrevTROrderNo = TROrderNo'
 
Upvote 0
Solution

Forum statistics

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