Hello lostnexcel,
There is nothing wrong with Charles' code. However, I have re-written it in a way that may enable you to understand it easier and, in consequence, perhaps modify it yourself. I did this because the cell references you mention in your latest post differ from the ones you requested earlier. Here is the code:
- Code: Select all
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("B6").Address Then
If IsNumeric(Range("B7").Value) Then
Range("B7").Select
Else
Range("C8").Select
End If
End If
End Sub
Please install it in the code sheet of the worksheet on which you want it to act.
To let you understand what it does:
- The code will run whenever a change is made in any cell on the worksheet
- If the changed cell was B6 action will be taken (otherwise nothing will happen)
- If B7 holds a number, B7 will be selected
- If B7 holds nothing or text, C8 will be selected.
You say that Charles' macro does "always the same" but you don't say what it does, select B7 or select C8.
If it always selects C8 the meaning is that B7 doesn't hold a number. It is possible to format the cell as "Text" and even if a number is entered it will not be recognized as such. Also, if you enter 32444kg in B7 this would be recognized as text, not as a number.
So, if you need other cells to be selected, I think you can change my code to test or select the cells you need selected or tested. If the numeric nature of B7 is in question, look at the cell format and the way you enter the weight. If you need to test for the cell being blank or non-blank change the above code by replacing
If IsNumeric(Range("B7").Value) Then with
- Code: Select all
If Not IsBlank(Range("B7")) Then
You can reverse the cell selection by removing the
Not.
Have a great day!
Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)