Macro help here!!!!

thunder_anger

Board Regular
Joined
Sep 27, 2009
Messages
206
<TABLE style="WIDTH: 108pt; BORDER-COLLAPSE: collapse" dir=rtl border=0 cellSpacing=0 cellPadding=0 width=144><COLGROUP><COL style="WIDTH: 54pt" span=2 width=72><TBODY><TR style="HEIGHT: 14.25pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #8db4e3; WIDTH: 54pt; HEIGHT: 14.25pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" dir=ltr class=xl63 height=19 width=72 align=left>Item</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #8db4e3; WIDTH: 54pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" dir=ltr class=xl63 width=72 align=left>Stock</TD></TR><TR style="HEIGHT: 14.25pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #8db4e3; HEIGHT: 14.25pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" dir=ltr class=xl63 height=19 align=left>Tea</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" dir=ltr class=xl64 align=right>30</TD></TR><TR style="HEIGHT: 14.25pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #8db4e3; HEIGHT: 14.25pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" dir=ltr class=xl63 height=19 align=left>Coffee</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" dir=ltr class=xl64 align=right>100</TD></TR><TR style="HEIGHT: 14.25pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #8db4e3; HEIGHT: 14.25pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" dir=ltr class=xl63 height=19 align=left>Gum</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" dir=ltr class=xl64 align=right>125</TD></TR><TR style="HEIGHT: 14.25pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #8db4e3; HEIGHT: 14.25pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" dir=ltr class=xl63 height=19 align=left>Water</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" dir=ltr class=xl64 align=right>400</TD></TR></TBODY></TABLE>

in the above example i made this macro not to allow user to delete or edit any cell that contans an item that have a stock so if he tries to delete the word tea a message box will appear saying that this item can not be deleted because the stock is greater than zero

Code:
Sub Hos()
Dim StRng As Range
Dim StRngC As Range
Set StRng = Sheet1.Range("A6:A8")
For Each StRngC In StRng
If StRngC.Value = "" And StRngC.Offset(0, 1) > 0 Then
MsgBox "you can not delete the item! stock greater than (0)"
Application.Undo
End If
Next StRngC
End Sub
and this code for the sheet1 change event

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("A6:A8"), Target) Is Nothing Then Call Hos
End Sub

but i discovered that i can edit the cells:confused:

any one help me not to allow users edit or delete an item unless stock is 0
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Range("A6:A8"), Target) Is Nothing Then
    For Each c In Target
        If c.Value = "" And c.Offset(, 1).Value <> "" Then
            Application.EnableEvents = False
            Application.Undo
            MsgBox "verboten", vbInformation
            Application.EnableEvents = True
            Exit Sub
        End If
    Next c
End If
End Sub
 
Upvote 0
Thanks for the reply
but
I still can edit the cells so i can change the words in cells i can change Tea to thunder and that is not ok
 
Upvote 0
thunder anger

How about simply applying a test to the number in stock, when the "item" cell is selected, and if there are stock items still remaining, "Bounce" the user into a different cell, then give them the message:
NB this must be in the worksheet's code module (where you've got your intersect code already)
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range

Set rng = Me.Range("B2:B5")

    If Not Intersect(rng, Target) Is Nothing Then
        If Target.Offset(0, -1).Value > 0 Then
            Me.Range("A1").Select
            MsgBox "You can't edit that cell - there are still some units in stock!", 48, "don't touch !!"
        End If
    End If
End Sub
 
Upvote 0
...........Do make sure that you change the tested range of my code ("rng") to that of your ITEMS column, and NOT your STOCK column, so that the code fires when the user selects "Tea, Coffee" etc. This is because it's these entries you've stated you wish to protect from being changed when there are still some in stock.
 
Upvote 0
Pleasure, and glad it worked for you.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,135
Members
452,890
Latest member
Nikhil Ramesh

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