VBA to automatically update a row when another cell is updated.

prondish

New Member
Joined
Jan 3, 2013
Messages
10
Hi everyone,

I have a VBA code that makes a function to evaluate the string in a cell as a function itself. It looks like this:

Function Evalu(ByVal S As String) As String
Evalu = Evaluate(S)
End Function

The idea is that the user can write a term like "sports" into one cell (A2) in a different sheet, and then using a bunch of concatenate formulas, it writes a code like this:

=CONCATENATE("IF(SUM(COUNTIF($B3,{"&A2&"}))>0,1,0))"

So that in the end, the concatenated string is searching another cell to see if it contains "sports". The idea is that this is done for a bunch of rows, so that I can label all the rows 0 or 1, to determine if they contain "sports".

However, if you write something different into A2, like "movies", I have to manually go back and auto fill the column that uses my eval function to relabel all my rows 0 or 1 according to "movies" now. Is there some way I can set it to automatically update the column where my VBA EVAL function is running? If possible, I'd like to do for just that column, since rebuilding the entire worksheet is quite time consuming.

Thanks everyone!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Use a change event VBA ie.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$A$2" Then

'your code here'

End If

End Sub

That way it will trigger your formula everytime you change cell A2.

Are you just trying to search for words within strings?
 
Upvote 0
Hi GR,

I am looking to search for words within strings, yes. Also, I tried...

"Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$E$5" Then

Function Evalu(ByVal S As String) As String
Evalu = Evaluate(S)
End Function

End If

End Sub" ...

but I got a compiler error. I'm super new to VBA, so I'm not sure how to handle it.


...
 
Upvote 0
Ok, we can work on fixing or a work around. To sum it all up, what would be your perfect scenario?

Ex:
1) user types word into one direct cell.
2) from this cell, either a formula or macro reads it
3) after reading it, runs thru each row in a range
4) if found places a counter next to the row in a designated column
5) You add up all the found counters?

Something like that?
 
Upvote 0
HI GR,

Yep! That's basically exactly what I want it to do! Is there an easier way to do this you think? I looked around forever and this was the best way I could figure out to do it.
 
Upvote 0
This is my set-up, change it to fit your needs:

1 </SPAN>Column A</SPAN>Column B</SPAN>
2 </SPAN>"Search Word"</SPAN>Sentences</SPAN>
3 </SPAN> Return Values of '1' or '0'</SPAN>I like sports</SPAN>
4 </SPAN>I like sports movies</SPAN>
5 </SPAN>I like sports and cookies</SPAN>
6 </SPAN>I like popcorn and pop</SPAN>
7 </SPAN>I like movies and popcorn</SPAN>
8 </SPAN>I like movies</SPAN>

<TBODY>
</TBODY><COLGROUP><COL><COL><COL></COLGROUP>


Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Set a = Sheets("Sheet1")
Dim i As Long
Dim pickValue As Variant

If Target.Address = "$A$2" Then
pickValue = a.Range("A2")
For i = 3 To a.Range("B" & Rows.Count).End(xlUp).Row
Set s = a.Range("B" & i).Find(pickValue, LookIn:=xlValues)
If Not s Is Nothing Then
a.Range("B" & i).Offset(0, -1) = 1
Else: a.Range("B" & i).Offset(0, -1) = 0
End If
Next i
End If

End Sub

Place this in the vba project on the applicable sheet you have data on. Also, Make sure to change the sheet name "Sheet1" if you have it called something else.
 
Upvote 0
That messed up my table but in Column A I have the search word desginated in cell A2 with blanks below it, the sentences are all in column B starting on row 3
 
Upvote 0
HI GR,

Totally sexy! Will this work if I instead have the data in a different sheet? I'm trying to make it all nice and user friendly, and so the place where a user can insert the specified string is in a specific user-interface on a different sheet. How would I deal with that?
 
Upvote 0
You can just add another variable.

Example, add:

Set b = Sheets("Sheet2")

So if Sheet1 is where you have the user enter the word and Sheet2 is where you have the data.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Set a = Sheets("Sheet1")
Set b = Sheets("Sheet2")
Dim i As Long
Dim pickValue As Variant

If Target.Address = "$A$2" Then
    pickValue = a.Range("A2")
    For i = 3 To b.Range("B" & Rows.Count).End(xlUp).Row
        Set s = b.Range("B" & i).Find(pickValue, LookIn:=xlValues)
        If Not s Is Nothing Then
        b.Range("B" & i).Offset(0, -1) = 1
        Else: b.Range("B" & i).Offset(0, -1) = 0
        End If
    Next i
End If

End Sub

Something to that affect, just changing up the variables. Of course you have to move ranges if necesarry but just as easily.
 
Upvote 0
HI GR Torres,

Awesome Idea! So in my particular workbook, my data and VBA function are in column J of Sheet2, and the interface where you can change the cell is in E3 of Sheet1. My formula looks like this:

Private Sub Worksheet_Change(ByVal Target As Range)
Set a = Sheets("Sheet1")
Set b = Sheets("Sheet2")
Dim i As Long
Dim pickValue As Variant

If Target.Address = "$E$3" Then
pickValue = a.Range("E3")
For i = 3 To b.Range("J" & Rows.Count).End(xlUp).Row
Set S = b.Range("J" & i).Find(pickValue, LookIn:=xlValues)
If Not S Is Nothing Then
b.Range("J" & i).Offset(0, -1) = 1
Else: b.Range("J" & i).Offset(0, -1) = 0
End If
Next i
End If

End Sub



However, this doesn't seem to do it. I still have to go back to sheet2 and double click the autofill button to re-calculate with my VBA function. Any thoughts?
Pete
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,986
Members
448,538
Latest member
alex78

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