Find instances of a string in a table and using their rows

Status
Not open for further replies.

Jimithy

New Member
Joined
Apr 5, 2018
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to have VBA count the number of instances of a string within a table, then save the table row of each instance. I then want to take these saved rows and add their data in new rows in a table elsewhere.

As an example, below are two tables (Let's call them "tblThings" and "tblNew"). tblThings includes two instances of "Thing1." My idea is that VBA finds Thing1 in rows 2 and 4 (including the header row) of the table. Then, using a For loop, a new row is added to tblNew. The data for each respective row is then added.

tblThings
NameData
Thing111
Thing233
Thing155
Thing377

<tbody>
</tbody>

tblNew (I should note other rows besides the new ones already existed, but are not shown here)
NameData
Thing111
Thing155

<tbody>
</tbody>

Code:
Sub Muffins()

Dim tblThings        As Object
Dim tblNew           As Object
Dim oNewRow          As ListRows
Dim itemName         As String
Dim itemData         As Long
Dim r                As Integer
Dim rng              As Range

Set tblThings = ActiveSheet.ListObjects("tblThings")
Set tblNew = ActiveSheet.ListObjects("tblNew")

itemName = "Thing1"

''''Create rng here

For Each r in rng
    Set oNewRow = tblNew.ListRows.Add(AlwaysInsert:=True)
    oNewRow.Range.Cells(1, 1) = itemName
    oNewRow.Range.Cells(1, 2) = itemData
Next

End Sub

I have been able to get the number of rows containing Thing1 using a CountIf line, but that isn't quite what I need. I can't seem to figure out any good way to do this. Thanks for any assistance!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Duplicate https://www.mrexcel.com/forum/excel...-instances-string-table-using-their-rows.html

Please do not post the same question multiple times. All clarifications, follow-ups, and bumps should be posted back to the original thread.
Per forum rules, posts of a duplicate nature will be locked or deleted (rule 12 here: Forum Rules).

Note that sometimes posts from new users require Moderator approval before you can see them on the public forums. When this happens, you should see a message to that effect when you try to post it.
Please be patient and do not attempt to post the question again.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,523
Messages
6,120,047
Members
448,940
Latest member
mdusw

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