Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Rainhard Fischer   on Jan 17 In MS Office Category.

  
Question Answered By: Anselma Schmidt   on Jan 17

OK.. I think this script  needs lots of help!!! (sorry)
Let's start with this:
I "assume" the name of the SHEET is "Mary".
You're putting this change  event in the workbook, not in the sheet.
so that means that if you change a value in column  9 of sheet "tom",
this macro will run and highlight all of the errors in sheet "Mary"...

so, we need to iron out how you wish for the sheet stuff to work.

It's never a good idea to declare a variable with the same name as a keyword.
In this case, the use of "row" is ill-advised... You're relying on Bill Gates to
decide for you when you mean to use the actual "row" property of a
worksheet/range, and when to use the value of the variable you've specified.
I prefer to use something like Trow...

Next, You're hard-coding the "mary" sheet name
in the range of cells... I think there's a better way.

Next, the use of RGB.. I believe that the RGB(0,0,0) is clearing the interior
color.
I would suggest using: Interior.ColorIndex = xlNone
and Interior.ColorIndex = 3 to set  it to "red"...(I recorded a macro to get
these values!)

I think that the problem with your sheet "nothing happens" is probably because
you added the Application.EnableEvents = false.
That's fine for when everything is running, but while you're testing, if the
script "bombs",
you're left with the events turned off!
(I have a short macro in my Personal book to turn  them back on)


Looking at the macro from the start, you're trying to find  the last row  of data
by looking for a "*"...
Is there going to be a "*" in the last row?
How about this: Is there any columns that will ALWAYS have data?
If so, you can use a worksheet function to count the number of non-blank cells
LastRow = Application.WorksheetFunction.CountA(Range("A1:A65500"))

If that's not practical, since you're really concerned about the values  in the
"I" column, then
you really need the last row with a value in "I". Record a macro where you
select the cell I65500.
then hit the "end" key followed by the "up arrow". This will take you to the
last non-blank cell.
save the row as the Activecell.Row like:

CurCell = ActiveCell.Address
Range("I65500").Select
Selection.End(xlUp).Select
lastrow = ActiveCell.Row
Range(CurCell).Select

Here, I save the address of the currently active cell, go to a cell near the
bottom of column "I",
find the last non-blank cell, store it's row number, and return to the
previously selected cell.

Now, if you're wanting this to work with specific sheets, Like "Mary", "Peter"
and "Paul", then
you need to make use of the sheet index being passed to the macro.

also, rather than clearing background color  of each line, you can clear the
entire sheet at once:

Cells.Interior.ColorIndex = xlNone

I've cleaned it up a bit, and I think this may be easier to follow:


Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal target  As Range)
Dim LastRow, Trow
Dim response
Dim CurCell
Dim ShtName

Application.EnableEvents = False
'Application.ScreenUpdating = False
ShtName = Sh.Name

If Target.Column = 9 Or Target.Column = 35 Or Target.Column = 43 Then
CurCell = ActiveCell.Address
Range("I65500").Select
Selection.End(xlUp).Select
LastRow = ActiveCell.row
Cells.Interior.ColorIndex = xlNone
Range(CurCell).Select

For Trow = 2 To LastRow
If Sheets(ShtName).Range("I" & Trow).Value = "BIGSUPPLIER" Then
Select Case Sheets(ShtName).Range("AI" & Trow).Value
Case "THINGONE"
Sheets(ShtName).Range("AQ" & Trow).FormulaR1C1 = "A"
' Sheets(ShtName).Range("B" & Trow & ":AR" &
Trow).Interior.ColorIndex = xlNone
Sheets(ShtName).Range("A" & Trow).Interior.Color =
RGB(204, 255, 255)
Case "THINGTWO"
Sheets(ShtName).Range("AQ" & Trow).FormulaR1C1 = "B"
Sheets(ShtName).Range("A" & Trow).Interior.Color =
RGB(204, 255, 255)
Case "THINGTHREE"
Sheets(ShtName).Range("AQ" & Trow).FormulaR1C1 = "C"
Sheets(ShtName).Range("A" & Trow).Interior.Color =
RGB(204, 255, 255)
Case Else
If Sheets(ShtName).Range("AI" & Trow) <> Empty Then
Sheets(ShtName).Range("AI" & Trow).Select
Sheets(ShtName).Range("B" & Trow & ":AR" &
Trow).Interior.ColorIndex = 3
response = MsgBox("The following is a list  of
acceptable values for the Rep/OSR field  when the supplier is IBM" & vbCr _
& vbCr & vbTab & "THINGONE" & vbCr _
& vbTab & "THINGTWO" & vbCr _
& "The value you have entered  is not one of these
allowed values. Please change this value.", vbOKOnly)
Else
Sheets(ShtName).Range("AI" & Trow).Select
End If
End Select
Else
Application.EnableEvents = True
Exit Sub
End If
Next
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub

Share: 

 

This Question has 17 more answer(s). View Complete Question Thread

 


Tagged: