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

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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Will we need to look anywhere in each row or will the trigger data always be in a specific column?
 
Upvote 0
The data will always be in the same column regardless of row. My actual data set has four data columns, but I think the behavior of each should be the same when adding them to the new table rows. I'm not able to copy entire rows due to the columns not always being adjacent, however.

e.g. row in tblThings
Thing1Data1Data2not dataData3not dataData4not data

<tbody>
</tbody>

becomes in tblNew
Thing1Data1Data2FormulaData3Data4

<tbody>
</tbody>

I hope this helps explains things well enough.
 
Upvote 0
Here you go. I think you can modify to your needs.


Code:
Option Compare Text 'ignore text case
Sub getuniqueentry()
Dim tblThings As Worksheet
Dim tblNew As Worksheet
Set tblThings = Worksheets("Sheet1")
Set tblNew = Worksheets("Sheet2")
On Error Resume Next
lastrow1 = 1 'incase no data
lastrow2 = 1 'incase no data
lastrow1 = tblThings.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
lastrow2 = tblNew.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row + 1 'first blank row
Resume Next
'***set string to match***
itemName = "Thing1"
For x = 1 To lastrow1
If tblThings.Cells(x, 1) = itemName Then
tblNew.Cells(lastrow2, 1) = tblThings.Cells(x, 1)
tblNew.Cells(lastrow2, 2) = tblThings.Cells(x, 2)
lastrow2 = lastrow2 + 1
End If
Next x
MsgBox "Complete", vbInformation, "ALERT"
End Sub

Assumes destination sheet already has headers.
 
Last edited:
Upvote 0
Solution
Thank you! I should be able to adapt it to my case. If I have any other trouble I'll let you know. Again, thanks for your time.
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,288
Members
448,885
Latest member
LokiSonic

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