# Formula to check previous row results before displaying result

HI Experts

I hope you can help

I have a sheet that is completed by various personnel to detail packing requirements for shipments including weights, dimensions and stock information.

Basically, I would like to know if there is  a way in which a formula can look at the carton number in column A and if it is the same as the next line entry, then the carton dimensions are not repeated until the carton number changes.

Ive tried this formula:-

=IF(A3<>A2,g2,IF(A3<>SUM(A2+1),G2,""))

But it gives me the same result ??

I have attached an example, if you could help I would be very grateful

J
Sheet_Example.xlsx
###### Who is Participating?

Finance AnalystCommented:
You could probably do it with Data Validation rather than VBA.

Select G2 to G11 and use the following to set a Custom rule in the Data Validation:

=IF(\$A2=\$A1,AND(G2<0,G2>0),LEN(G2)>5)

This says if the value in A2 matches the value in A1 the entry in G2 has to be greater than AND less than zero which is impossible so no entry can be made, otherwise it has to be greater than 5 characters "HxWxD".

Thanks
Rob H
0

Commented:
HI,

How do you get

24x24x25
is it a formula?

Regards
0

Compliance ManagerAuthor Commented:
HI RG

Apologies

No this is just typed in its the height, width and depth of a packaging cardboard box that we send goods in.

What I need is for the cell below the first entry to look to see if the carton number is the same in column A and if it is, then return a blank. If it isn't (ie Carton 2) then it will allow the new carton dimensions to be visible.

This formula will be entered AFTER all the details are in unless there is a macro that can be run so that after the first entry and subsequent entry in row 3, it will look at the carton number in A3 and if its the same as A2, blocks the cells in G3 so the user cannot duplicate the carton dimensions until the carton number changes. Is this possible?

J
0

Commented:
if cell G3 does not have the information in the first place why create a formula?
0

Compliance ManagerAuthor Commented:
Hi RG

Apologies

I believe the user has misled me into what is requested

What is actually requested is, for example, after entering the data in row 2, a macro looks at cell A3 and if it is the same as A2 (eg 1) then cell G2 is locked. If the user enters a number that is different from A2 (ie 2) then G2 is unlocked and is enabled for cell entry (eg carton dimensions)

Is this possible?

J
0

Commented:
then try
``````Sub macro()
ActiveSheet.Unprotect
Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
Rng.Offset(, 6).Locked = False
For Each c In Rng
If c.Value = c.Offset(-1).Value Then c.Offset(, 6).Locked = True
Next
ActiveSheet.Protect
End Sub
``````
0

Compliance ManagerAuthor Commented:
HI RG

This is great except when I enter the macro and then try to enter a new row of data in row 12, it comes up with the 'please unprotect sheet' alert.

Is there a way to isolate the locked cells to JUST column G as when the macro is run, the user will still need to enter data however, not in Column G if the carton numbers are duplicated as explained above ??

J
0

Commented:
then try
``````Sub macro()
ActiveSheet.Unprotect
Cells.Locked = False
Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
For Each c In Rng
If c.Value = c.Offset(-1).Value Then c.Offset(, 6).Locked = True
Next
ActiveSheet.Protect
End Sub
``````
0

Compliance ManagerAuthor Commented:
HI RG

Thanks for the response

So Ive entered the code and it will now let me enter data into the next row (row 12) however, if I enter 4 (carton 4) in column A12 and some data through to Column G12. If I enter data into the next row (A13) as carton 4 (duplicating the previous entry) it still allows me to enter freely into Column G (G13) instead of the macro seeing that the previous entry was also carton 4 and therefore, G13 should be blocked.

Also, when I run this macro and try typing into Column G cells from G2 to G11, the locked doesn't work ??

Any ideas ? Thank you for your help by the way I really appreciate it.

J
0

Commented:
then you have to use in the sheet module

``````Private Sub Worksheet_Change(ByVal Target As Range)
Me.Unprotect
Cells.Locked = False
Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
For Each c In Rng
If c.Value = c.Offset(-1).Value Then c.Offset(, 6).Locked = True
Next
Me.Protect
End Sub
``````
0

Finance AnalystCommented:
Alternatively, do you have a list of Carton codes with their respective dimensions. If so, use a VLOOKUP to get the dimensions rather than entering them:

=IF(A2=A1,"",VLOOKUP(A2,DimensionTable,2,False))

Thanks
Rob
0

Finance AnalystCommented:
See attached with both examples on separate sheets.
Sheet_Example.xlsx
0

Compliance ManagerAuthor Commented:
HI Rob

Your Data Validation option is perfect ! Thank you so much

One question for the purpose of limiting the input further, is it possible to remove the Retry option on the Warning Alert in a Data Validation pop-up?

J
0

Finance AnalystCommented:
Unfortunately, I don't know of a way of doing that. There are 3 options for what error message comes up and the other two allow the user to continue with an invalid entry.

Also, one disadvantage of Data Validation, it doesn't stop the user from just copying and pasting from another cell or using the mouse to drag fill cells. It only stops physical entries.
0

Compliance ManagerAuthor Commented:
Thanks guys for the help

Appreciated as always !!

J ;o))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.