Page 1 of 2 12 LastLast
Results 1 to 10 of 12

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

This is a discussion on VBA to automatically update a row when another cell is updated. within the Excel Questions forums, part of the Question Forums category; Hi everyone, I have a VBA code that makes a function to evaluate the string in a cell as a ...

  1. #1
    New Member
    Join Date
    Jan 2013
    Posts
    10

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

    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!

  2. #2
    Board Regular GRTorres's Avatar
    Join Date
    Nov 2012
    Location
    Omaha, NE
    Posts
    344

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

    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?

    - Posting guidelines, forum rules and terms of use

    - Try searching for your answer first, see how

    - Read the FAQs

    - List of BB codes

    - Use code tags when posting vba projects. It saves space and makes it easier to follow.

  3. #3
    New Member
    Join Date
    Jan 2013
    Posts
    10

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

    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.


    ...

  4. #4
    Board Regular GRTorres's Avatar
    Join Date
    Nov 2012
    Location
    Omaha, NE
    Posts
    344

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

    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?

    - Posting guidelines, forum rules and terms of use

    - Try searching for your answer first, see how

    - Read the FAQs

    - List of BB codes

    - Use code tags when posting vba projects. It saves space and makes it easier to follow.

  5. #5
    New Member
    Join Date
    Jan 2013
    Posts
    10

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

    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.

  6. #6
    Board Regular GRTorres's Avatar
    Join Date
    Nov 2012
    Location
    Omaha, NE
    Posts
    344

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

    This is my set-up, change it to fit your needs:

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


    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.

    - Posting guidelines, forum rules and terms of use

    - Try searching for your answer first, see how

    - Read the FAQs

    - List of BB codes

    - Use code tags when posting vba projects. It saves space and makes it easier to follow.

  7. #7
    Board Regular GRTorres's Avatar
    Join Date
    Nov 2012
    Location
    Omaha, NE
    Posts
    344

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

    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

    - Posting guidelines, forum rules and terms of use

    - Try searching for your answer first, see how

    - Read the FAQs

    - List of BB codes

    - Use code tags when posting vba projects. It saves space and makes it easier to follow.

  8. #8
    New Member
    Join Date
    Jan 2013
    Posts
    10

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

    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?

  9. #9
    Board Regular GRTorres's Avatar
    Join Date
    Nov 2012
    Location
    Omaha, NE
    Posts
    344

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

    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.

    - Posting guidelines, forum rules and terms of use

    - Try searching for your answer first, see how

    - Read the FAQs

    - List of BB codes

    - Use code tags when posting vba projects. It saves space and makes it easier to follow.

  10. #10
    New Member
    Join Date
    Jan 2013
    Posts
    10

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

    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

Page 1 of 2 12 LastLast

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com