Comparing cell values and moving whole rows

comppi

New Member
Joined
Jul 2, 2014
Messages
7
I'm trying to compare the values of columns B and G and if any of the cells are G < B, I would like to move the whole row into the second worksheet. And only if possible, it would be awesome if the macro could check column A for duplicates before the value comparison and sum up the column B if there are duplicates. In my example row 6 and 7 on column A have the same suppliers code and the amount totals to 2.
In this case for example, the value of the third row of G is lower than B, and therefore I would like the whole row 3 to be cut out from worksheet 1 and moved to worksheet 2.

I've tried making my own macros from other examples, but haven't really found anything that would suite my needs and fulfill the objective. I would be so thankful for any help provided! I understand that this might seem really complicated since I'm not that good at phrasing my problem :(.

Original view of sheet 1:
SUPPLIERSCODEORDERAMOUNTPRODUCTNAMEBARCODESUPPLIERSCODEWAREHOUSE STOCK
12342,00TEST PRODUCT 1999999912342,00
12354,00TEST PRODUCT 2999999912353,00
12361,00TEST PRODUCT 3999999912361,00
12371,00TEST PRODUCT 4999999912371,00
12381,00TEST PRODUCT 5999999912381,00
12381,00TEST PRODUCT 59999999

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

The macro would first make the sheet look like:
SUPPLIERSCODEORDERAMOUNTPRODUCTNAMEBARCODESUPPLIERSCODEWAREHOUSE STOCK
12342,00TEST PRODUCT 1999999912342,00
12354,00TEST PRODUCT 2999999912353,00
12361,00TEST PRODUCT 3999999912361,00
12371,00TEST PRODUCT 4999999912371,00
12382,00TEST PRODUCT 5999999912381,00

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

And then cut out the rows in which G<B, so the sheet 1 would look like:
SUPPLIERSCODEORDERAMOUNTPRODUCTNAMEBARCODESUPPLIERSCODEWAREHOUSE STOCK
12342,00TEST PRODUCT 1999999912342,00
12361,00TEST PRODUCT 3999999912361,00
12371,00TEST PRODUCT 4999999912371,00

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

And Sheet 2:
12354,00TEST PRODUCT 2999999912353,00
12382,00TEST PRODUCT 5999999912381,00

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Last edited:

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.
Hi

Try this under a command button

Code:
Application.ScreenUpdating = False

Static fx, Lr, LrRange, WS1, WS2, RowHolder
Set WS1 = Sheets("sheet1") ' Alter Sheetname if Required
Set WS2 = Sheets("sheet2") ' Alter Sheetname if Required
WS1.Activate
Lr = ActiveSheet.Range("a70000").End(xlUp).Row
      
LrRange = ActiveSheet.Range("a2:a" & Lr).Address(False, False)
 
 

For Each fx In ActiveSheet.Range(LrRange)
        If fx.Value > "" Then
        
        RowHolder = fx.Address(0, 0)
        
        If fx.Offset(0, 5).Value = "" Then
        
           fx.Offset(0, 5).Value = fx.Value
           fx.Offset(0, 6).Value = fx.Offset(0, 1).Value
        
        End If
        
            If fx.Offset(0, 1).Value = fx.Offset(0, 6).Value Then
            
                    'Do Nothing
            Else
            
                   fx.EntireRow.Cut
                   
                    
                    WS2.Activate
                    
                             If ActiveSheet.Range("a2").Value = "" Then
                             
                                ActiveSheet.Range("a2").Select
                                ActiveSheet.Paste
                                WS1.Activate
                                Range(RowHolder).Select
                                ActiveCell.EntireRow.Select
                                Selection.Delete Shift:=xlUp
                                GoTo ender
                                
                                
                             ElseIf ActiveSheet.Range("a2").Value > "" And ActiveSheet.Range("a3").Value = "" Then
                             
                                ActiveSheet.Range("a3").Select
                                ActiveSheet.Paste
                                WS1.Activate
                                Range(RowHolder).Select
                                ActiveCell.EntireRow.Select
                                Selection.Delete Shift:=xlUp
                                GoTo ender
                                                          
                                                          
                             ElseIf ActiveSheet.Range("a2").Value > "" And ActiveSheet.Range("a3").Value > "" Then
                             
                                ActiveSheet.Range("a2").Select
                                Selection.End(xlDown).Select
                                ActiveCell.Offset(1, 0).Select
                                ActiveSheet.Paste
                                WS1.Activate
                                Range(RowHolder).Select
                                ActiveCell.EntireRow.Select
                                Selection.Delete Shift:=xlUp
                                GoTo ender
                             End If
             End If
        End If
ender:
WS1.Activate

Next fx


Regards

Kev
 
Upvote 0
You're so awesome. This is so close to what I wanted. Only 1 thing I would have liked different, but like I said, I'm not that good at explaining this in English.

The same product (the same suppliers code and product name) might be on the order side on 2 or more different rows like this:
SUPPLIERSCODEORDERAMOUNTPRODUCTNAMEBARCODESUPPLIERSCODEWAREHOUSE STOCK
111123,00TEST1641790100700980177381,00
111111,00TEST26430104924201111111,00
111111,00TEST26430104924201

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

Now the macro copies A4 & B4 to F4 & G4. What I wanted that it would do was that it combines A3 B3 and A4 B4, and removes the remaining product name and row, so the end result would be the following:

SUPPLIERSCODEORDERAMOUNTPRODUCTNAMEBARCODESUPPLIERSCODEWAREHOUSE STOCK
111123,00TEST1641790100700980177381,00
111112,00TEST26430104924201111111,00

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

And after that, it will check the amounts of B and G like it now does.
 
Upvote 0
****, I didnt have time to edit my second post. This is the fixed one:

You're so awesome. This is so close to what I wanted. Only 1 thing I would have liked different, but like I said, I'm not that good at explaining this in English.

The same product (the same suppliers code BUT DIFFERENT product name) might be on the order side on 2 or more different rows like this:
SUPPLIERSCODEORDERAMOUNTPRODUCTNAMEBARCODESUPPLIERSCODEWAREHOUSE STOCK
111111,00TEST16417901007009111111,00
111121,00TEST26417901007011111121,00
111121,00TEST2OLD6417901007012

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

Now the macro copies A4 & B4 to F4 & G4. What I wanted that it would do was that it combines A3 B3 and A4 B4, and removes row that has the old product name and barcode, so the end result would be the following:
SUPPLIERSCODEORDERAMOUNTPRODUCTNAMEBARCODESUPPLIERSCODEWAREHOUSE STOCK
111111,00TEST16417901007009111111,00
111122,00TEST26417901007011111121,00

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

And after that, it will check the amounts of B and G like it now does.
 
Upvote 0
Hi

Will the SupplierCode Always be in order or will we need to order thew data first. Also will the second occurance of Supplier code always have cells "F & G" blank.


Kev
 
Upvote 0
Actually first I have to sort columns ABCD so that first occurrence of supplier code will be ascending from low to high. I will add the second supplier code and warehouse stock afterwards, so they are not even present when the combining happens. I actually think It might be easier to just have 2 buttons then. First button when I load the ordered items into excel, it will sort them, and combine duplicate suppliercodes. Then the second button that I push when I have added the second supplier code and warehouse stock, which are already in ascending order via supplier code.
 
Upvote 0
Hi

Try this as button 1. is it doing what you want? as button 1. I have a problem and I think it is related to your keyboard layout, my layout doesn't recognise your numbers as numbers because of the commas, but it may work OK for you we will have to see.

Code:
Application.ScreenUpdating = False

Static fx, Lr, LrRange, WS1, Lr1, Lr1Range, counter

Set WS1 = Sheets("sheet1") ' Alter Sheetname if Required
WS1.Activate
Lr = ActiveSheet.Range("a70000").End(xlUp).Row
      
LrRange = ActiveSheet.Range("a2:g" & Lr).Address(False, False)
Lr1 = ActiveSheet.Range("a70000").End(xlUp).Row
      
Lr1Range = ActiveSheet.Range("a2:A" & Lr1).Address(False, False)
Range(LrRange).Select
    WS1.Sort.SortFields.Clear
    WS1.Sort.SortFields.Add Key:=Range(Lr1Range) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With WS1.Sort
        .SetRange Range(LrRange)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    
 counter = 0
While counter < 2
    counter = counter + 1    ' Increment Counter.
    
    
    
            For Each fx In ActiveSheet.Range(Lr1Range)
                     If fx.Value > "" Then
        
               
                            If fx.Value = fx.Offset(1, 0).Value Then
        
                                fx.Offset(0, 1).Value = (fx.Offset(0, 1).Value + fx.Offset(1, 1).Value)
                                fx.Offset(1, 0).Select
                                Selection.EntireRow.Select
                                Selection.Delete Shift:=xlUp
                            End If
              
                    End If
              
            Next fx
   
Wend



regards


Kev
 
Upvote 0
The first button works flawlessly, but I have noticed that the second button has some kind of a bug. Don't know if it's related to the number formatting or something, but sometimes it decides to move everything to the second sheet, even when order and supply are identical and warehouse stocks are above the order amount. Actually it has to be related to formatting. Excel recognizes the data I pull out from the warehouse db as text, even though they are purely numbers. Don't know how I can change that. The cells are preformatted to be only integers.. And when it does start moving everything, it even moves the headers to the other sheet! Mystifying
 
Last edited:
Upvote 0
No. The first button is the first button, the second I have to send you, the idea was to get your approval on the first stage before I moved onto the second. Correct me if I am wrong but the second stage should. Look at the order quantity and the stock holding quantity, if there is a differance, remove that complete row to sheet 2.


Please comment.


regards


kev
 
Upvote 0

Forum statistics

Threads
1,215,212
Messages
6,123,649
Members
449,111
Latest member
ghennedy

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