Lil Stinker
Board Regular
- Joined
- Feb 16, 2022
- Messages
- 143
- Office Version
- 2019
- Platform
- Windows
Hey folks-
I'm still learning my way through Excel VBA and I've hit another seemingly simple roadblock. Maybe you can help me understand what's going on here or provide better code.
I would like to have a shape display whenever any cell within a range has a specific word appear. My code is pretty straightforward, and it works whenever I enter the word but what I don't understand is why it doesn't work when I load the same word into the cell range from another worksheet.
Specifically, if the word "cat" appears in any one cell in the Range ("D16:D42"), I want Shape1 to display. If all the cells in the range say "cat", I want Shape1 to appear just once, not duplicated for every instance "cat" comes up. If none of the cells show "cat" then Shape1 should not appear. My cells in the range are merged so technically the range may be "D16:E42". They also have the same data validation list embedded for each. If I select anything from the list that has the word "cat" in it or I type "cat" in any of these cells, Shape1 appears. However, there is another way this range is populated and that is by loading saved details from Sheet2 into Sheet1. Essentially, Sheet1 is a form for users to fill out using the drop-down options in D16:D42 while Sheet2 is the record where previous form details are saved. Whenever the details are loaded from Sheet2 into the same cell range on Sheet1 and the word "cat" appears, Shape1 does not display unless I click the drop-down and select the same line again. Not very practical since Shape1 should display automatically.
Transferring the details is a simple value for value transfer:
Even when I run a Debug.Print to see what cells are changing during the change_event, D16:D42 appear. What is it I'm missing? Shouldn't Shape1 appear whether I enter cat into the cell or whether Excel does?
I realize since D:E are merged Target.CountLarge should be > 2 but even when I played with that, it made no difference. I also played with changing the Range from ("D16:D42") to ("D16:E42"). Still nothing. I also took out "Else Shapes("shape1").Visible=msoFalse". Still nothing.
Please help
I'm still learning my way through Excel VBA and I've hit another seemingly simple roadblock. Maybe you can help me understand what's going on here or provide better code.
I would like to have a shape display whenever any cell within a range has a specific word appear. My code is pretty straightforward, and it works whenever I enter the word but what I don't understand is why it doesn't work when I load the same word into the cell range from another worksheet.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Range("D16:D42")) Is Nothing Then
If InStr(Target.Value, "cat") Then
Shapes("shape1").Visible = msoCTrue 'show image
Else
Shapes("shape1").Visible = msoFalse 'hide image
End If
End If
Specifically, if the word "cat" appears in any one cell in the Range ("D16:D42"), I want Shape1 to display. If all the cells in the range say "cat", I want Shape1 to appear just once, not duplicated for every instance "cat" comes up. If none of the cells show "cat" then Shape1 should not appear. My cells in the range are merged so technically the range may be "D16:E42". They also have the same data validation list embedded for each. If I select anything from the list that has the word "cat" in it or I type "cat" in any of these cells, Shape1 appears. However, there is another way this range is populated and that is by loading saved details from Sheet2 into Sheet1. Essentially, Sheet1 is a form for users to fill out using the drop-down options in D16:D42 while Sheet2 is the record where previous form details are saved. Whenever the details are loaded from Sheet2 into the same cell range on Sheet1 and the word "cat" appears, Shape1 does not display unless I click the drop-down and select the same line again. Not very practical since Shape1 should display automatically.
Transferring the details is a simple value for value transfer:
VBA Code:
lastitemRESULTROW = Sheet2.Range("M1048576").End(xlUp).Row
If lastitemRESULTROW < 3 Then GoTo NoItems
For resultROW = 3 To lastitemRESULTROW
qtITEMROW = Sheet2.Range("T" & resultROW).Value 'form result row
If qtITEMROW < 3 Then
Clear_Form 'entry has been deleted, move on, nothing to load here
GoTo NoItems
End If
.Range("D" & qtITEMROW & ":H" & qtITEMROW).Value = Sheet2.Range("O" & resultROW & ":S" & resultROW).Value 'copy result dsc, st date, qty, rate
.Range("AA" & qtITEMROW).Value = Sheet2.Range("U" & resultROW).Value 'copy result item row
Next resultROW
NoItems:
Even when I run a Debug.Print to see what cells are changing during the change_event, D16:D42 appear. What is it I'm missing? Shouldn't Shape1 appear whether I enter cat into the cell or whether Excel does?
I realize since D:E are merged Target.CountLarge should be > 2 but even when I played with that, it made no difference. I also played with changing the Range from ("D16:D42") to ("D16:E42"). Still nothing. I also took out "Else Shapes("shape1").Visible=msoFalse". Still nothing.
Please help