Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Auto Grab Word From Text String When Cell is Updated

0

Hi All,

I am trying to automate a process that will do the following:

  1. Detect when the data in a certain cell has been updated (in screenshot, its A32, but the words can be anything, as they change with each update)
  2. If it has, grab each word between the commas
  3. Deposit each of those words into their own separate cell
  4. Do the same for the next empty row, if the data in cell A32 is updated

Attached a sample worksheet. Thanks in advance!

Answer
Discuss

Discussion

Hello jdbs and welcome.

Screenshots cannot be uploaded - only Excel files; so we don't see the screenshot.
Regarding your point #3: where are the empty cells you want the words "deposited"?
Regarding your point #4: do you mean - the next time "A32" is updated the selected words will be captured and copied to the next empty row so all of the "A32" changes are recorded?
If you could update your qustion and upload a sample file it will be easier to provide a solution. Also, details about which words are to be captured.

Cheers   :-)
WillieD24 (rep: 557) Feb 8, '23 at 2:51 pm
Hi jbds. If possible, please answer Willie and edit your question to attach a representative Excel file using the Add Files button 
John_Ru (rep: 6142) Feb 8, '23 at 4:02 pm
Add to Discussion

Answers

0
Selected Answer

jbds

You didn't provide a file (at first) so I created one (the first file attached).

This event macro (behind sheet1) detects when A32 changes:

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("A32")) Is Nothing Then Exit Sub
' if A32 changed, run sub
Call ExtractWords

End Sub

It calls another macro (in Module 1) where you need to change the bits in bold (and use the comments to understand what is happening):

Sub ExtractWords()

Dim Inpt As Range, Inp As Range, Cl As Range
Dim xWords() As Variant, n As Long, Offs As Long

' list your words (case sensitive) and surround with spaces if you want whole words only
xWords = Array("who", "certain", "be", "words", "out")
' state range to check
Set Inpt = Range("A32:A33")
' say the number columns to the right the words will be copied, 1 or more
Offs = 2

'clear output area and draw borders
With Inpt
    With .Offset(0, Offs).Resize(.Rows.Count, UBound(xWords) + 1)
        .Clear
        .Borders.LineStyle = xlContinuous
        .VerticalAlignment = xlTop
    End With
End With

' loop though range
For Each Cl In Inpt
    'loop through array to check cells against words
    For n = LBound(xWords) To UBound(xWords)
        If InStr(Cl.Value, xWords(n)) > 0 Then
            ' list (in array order)
            Cl.Offset(0, Offs + n) = xWords(n)
        End If
    Next n
Next Cl
' tell user
MsgBox "Extracted words for range " & Inpt.Address(0, 0) & ", listed in bordered area"

End Sub

The array of words could be any size but see the note above that line (and notice what "be" is listed since it's part of "maybe" in my example text).

That macro can be run manually from the blue button on the sheet too.

REViSION 10 February 2023

Given your sample file and clarification that the changed text will always have words separated by commas, the second file below uses the Worksheet_Change event to detect when the cell A32 (in bold below) changes and extract the words into the next free row below that cell. I've added comments so you can see what's happening:

Private Sub Worksheet_Change(ByVal Target As Range)

' define which cell to check (and do nothing if some other cell changed)
If Intersect(Target, Range("A32")) Is Nothing Or Target.CountLarge > 1 Then Exit Sub

Dim xWords As Variant, lRw As Long

' stop this macro being retriggered by actions
Application.EnableEvents = False
'get last used row in target's column
lRw = Cells(Rows.Count, Target.Column).End(xlUp).Row

' put words or phrases (each separated by a comma) into an array
xWords = Split(Target.Value, ",")
' copy the array into next free row
Cells(lRw + 1, Target.Column).Resize(, UBound(xWords) + 1) = xWords

' allow events
Application.EnableEvents = True
' tell user
MsgBox "Extracted " & UBound(xWords) + 1 & " from " & Target.Address(0, 0) & ", now listed in row " & lRw + 1

End Sub

Hope this helps. if so, please mark this Answer as Seelected (in accordance with the rules of the Forum)

Discuss

Discussion

Hi John_Ru, WillieD24, my apologies, I just uploaded a sample excel sheet. Thanks for your help, and for the code sample. The one thing I wasn't clear about was that the words are not known in advance, they could be different with each update, but they'll come into A32 separated by commas.

I need to grab each word between the commas and place them into the cells on the spreadsheet, and then when A32 is updated again, repeat the process automatically but place the new words on the next availble row. 

Thanks again for your help!
jbds (rep: 2) Feb 9, '23 at 9:05 pm
Jdbs. Thanks for the clarification (next time please do that in the original question). Please see the revision and new file added to my Answer. Hope it works for you so you can mark it Selected!
John_Ru (rep: 6142) Feb 10, '23 at 11:31 am
Wow, thank you so much, this works! I did have one last question. Would it be possible to perform the same operation if cell A32 were on another sheet?

Basically, if there were a new 'Sheet 2', that had nothing but a single cell that contained the same comma separated words (rather than just a particular cell like an A32), then send the data from the single cell in Sheet 2 to Sheet 1 in the same way?

Sorry if I'm not making good sense with the question.

Thanks!
jbds (rep: 2) Feb 10, '23 at 12:17 pm
Thanks for selecting my Answer, Jbds.

If the revised macro below were "behind" your Sheet2 (and pointing to a cell A1), you could get it to write to Sheet1 by changing the code to:
Private Sub Worksheet_Change(ByVal Target As Range)
 
' define which cell to check (and do nothing if some other cell changed)
If Intersect(Target, Range("A1")) Is Nothing Or Target.CountLarge > 1 Then Exit Sub
 
Dim xWords As Variant, lRw As Long
 
' stop this macro being retriggered by actions
Application.EnableEvents = False
 
' in another sheet
With Worksheets("Sheet1")
        'get last used row in target's column
        lRw = .Cells(.Rows.Count, Target.Column).End(xlUp).Row
    
    ' put words or phrases (each separated by a comma) into an array
    xWords = Split(Target.Value, ",")
    ' copy the array into next free row
    .Cells(lRw + 1, Target.Column).Resize(, UBound(xWords) + 1) = xWords
 
End With
 
' allow events
Application.EnableEvents = True
' tell user
MsgBox "Extracted " & UBound(xWords) + 1 & " words from " & Target.Address(0, 0) & ", now listed in row " & lRw + 1
 
End Sub
.
John_Ru (rep: 6142) Feb 10, '23 at 1:07 pm
Thank you John_Ru, this worked. The only hiccup is that when it adds the data to the next new row in Sheet1, its adding the data to the first available row outside of the table, even though the rows inside the table are also empty.

I think it may have to do with the line of code '' copy the array into next free row     .Cells(lRw + 1, Target.Column).Resize(, UBound(xWords) + 1) = xWords

I think the program is seeing any rows that are a part of the Table as not free, and therefore puts it in the first row outside the Table. 

Anyway to adjust the code to deposit into the next free row within the Table?

Thanks again.
jbds (rep: 2) Feb 10, '23 at 2:08 pm
Jbds

You're new to the Forum but please note for the future  that we generally don't like follow-on questions (once the original question has been answered.

I don't have time to adjust the code for a table (and adding ListRows) but suggest you:

1) delete all data rows from your table on Sheet1 (so you're left with headers and one blank row)

2) use this code in the following Discussion point instead (changes in bold).

You'll see that column A (Raw Data) now has text telling you when the text was changed plus the text e.g.
11/02/2023 11:21:10
My, Forum, user, name, is, John_, Ru

(where text echoes that from Sheet1 A1 and the date part above it is in UK format above but will be according to your Locale).
John_Ru (rep: 6142) Feb 11, '23 at 6:24 am
Private Sub Worksheet_Change(ByVal Target As Range)
 
' define which cell to check (and do nothing if some other cell changed)
If Intersect(Target, Range("A1")) Is Nothing Or Target.CountLarge > 1 Then Exit Sub
 
Dim xWords As Variant, lRw As Long
 
' stop this macro being retriggered by actions
Application.EnableEvents = False
 
' in another sheet
With Worksheets("Sheet1")
        'get last used row in column A
        lRw = .Cells(.Rows.Count, 1).End(xlUp).Row
        ' but adjust for empty table
        If lRw = 2 And .Cells(2, 1) = "" Then lRw = 1
    ' put words or phrases (each separated by a comma) into an array
    xWords = Split(Target.Value, ",")
    ' put date and time plus full text in column A
    .Cells(lRw + 1, 1).Value = Now & vbLf & Target.Value
    ' copy the array into next free row from column B
    .Cells(lRw + 1, 2).Resize(, UBound(xWords) + 1) = xWords
 
End With
 
' allow events
Application.EnableEvents = True
' tell user
MsgBox "Extracted " & UBound(xWords) + 1 & " words from " & Target.Address(0, 0) & ", now listed in row " & lRw + 1
 
End Sub

(Linked to explanation above)

Hope this helps. If you have other questions, please raise them as new questions. Have a good weekend!
John_Ru (rep: 6142) Feb 11, '23 at 6:24 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login