VB Code only writing one row to Results tab.

Galland

New Member
Joined
May 9, 2023
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Desired results is to read from the criteria tab
Find the corresponding match in the Data tab
And write the results in the Dest Tab
My problem is my VB script is only writing one value and not all of them.
Dim rng As Range, cell As Range
Application.ScreenUpdating = False
Set wsData = Sheets("Data")
Set wsCriteria = Sheets("Criteria")
Set wsDest = Sheets("Dest")


lr = wsCriteria.Cells(Rows.Count, "A").End(xlUp).Row
Set rng = wsCriteria.Range("A2:A" & lr)

If wsData.FilterMode Then wsData.ShowAllData


For Each cell In rng
With wsData.Range("A1").CurrentRegion
.AutoFilter field:=1, Criteria1:=cell.Value

wsData.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A1")
wsDest.UsedRange.Columns.AutoFit
End With

Next cell
wsData.AutoFilterMode = False
wsData.Activate
Application.ScreenUpdating = True
End Sub

Data Tab
nameagecitystatezipcelldesk
Bob
20​
JacksonNY
12345​
123​
234​
ZZZXCarol
21​
HazardKY
12346​
123​
234​
Danielle
22​
LondonTN
12347​
123​
234​
Steve
23​
PinevilleMi
12348​
123​
234​
123Jen456
24​
SomersetWY
12349​
123​
234​
Naresh
25​
ManchesterPA
12350​
123​
234​
Ron
26​
MonticelloVA
12351​
123​
234​
Sam XXX
27​
AlbanyUT
12352​
123​
234​
Vinny
28​
MoreheadAL
12353​
123​
234​

Criteria TAB
name
Bob
Carol
Danielle
Steve
Jen
Naresh
Ron
Sam
Vinny

Dest TAB

nameagecitystatezipcelldesk
Vinny
28​
MoreheadAL
12353​
123​
234​
 
When finished, the Dest tab contains the name 123Jen456 from the Data tab since it matched Jen in the Criteria tab.
Is it possible to return the Jen from the Criteria tab when it finds the match of 123Jen456in the Data tab?
If not its no big deal since I will have all my matching rows.
You are welcome - thanks for the reply.
As for your latest "one more thing", I'd like to start with the exact code you are using now to make modifications. Please post the full code you are currently using and I'll take a shot at modifying same to incorporate this latest request.
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Thanks Joe!

Option Explicit
Sub FilterAndCopyData()
Dim wsData As Worksheet, wsCriteria As Worksheet, wsDest As Worksheet
Dim lr As Long
Dim rng As Range, cell As Range
Dim nxtcell As Long


Application.ScreenUpdating = False
Set wsData = Sheets("Data")
Set wsCriteria = Sheets("Criteria")
Set wsDest = Sheets("Dest")


lr = wsCriteria.Cells(Rows.Count, "A").End(xlUp).Row
Set rng = wsCriteria.Range("A1:A" & lr)

If wsData.FilterMode Then wsData.ShowAllData


For Each cell In rng
With wsData.Range("A1").CurrentRegion
.AutoFilter field:=1, Criteria1:="*" & cell.Value & "*" ' remember the field:=1 is the search field

nxtcell = wsDest.Cells(Rows.Count, "A").End(xlUp).Row + 1

wsData.Range("A1").CurrentRegion.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A" & nxtcell)
wsDest.UsedRange.Columns.AutoFit


End With

Next cell

wsData.AutoFilterMode = False
wsData.Activate
Application.ScreenUpdating = True

End Sub
 
Upvote 0
OK, here's a lightly tested modification to do what you have requested. Note that if you have Names like Robert and Rob in your data sheet you will get a duplicated output of the record for Robert in the destination sheet because when filtering for Rob the output will also include Robert via the wildcards used in the filter criteria. I have removed duplicate records from the Dest sheet to handle this, but there are likely to be other gotchas of this type. Also note that I've assumed that the Name column on the Data sheet is populated with constants - no formulas in that column.
VBA Code:
Option Explicit
Sub FilterAndCopyData()
Dim wsData As Worksheet, wsCriteria As Worksheet, wsDest As Worksheet
Dim lr As Long
Dim rng As Range, Rdata As Range, cell As Range, V As Variant
Dim nxtcell As Long, x, cd As Range
Application.ScreenUpdating = False
Set wsData = Sheets("Data")
Set Rdata = wsData.Range("A2:A" & wsData.Cells(Rows.Count, "A").End(xlUp).Row)
V = Rdata.Value
Set wsCriteria = Sheets("Criteria")
Set wsDest = Sheets("Dest")
lr = wsCriteria.Cells(Rows.Count, "A").End(xlUp).Row
Set rng = wsCriteria.Range("A2:A" & lr) 'assumes there's a header on Criteria col A
If wsData.FilterMode Then wsData.ShowAllData
For Each cell In rng
    With wsData.Range("A1").CurrentRegion
        .AutoFilter field:=1, Criteria1:="*" & cell.Value & "*" ' remember the field:=1 is the search field
        For Each cd In .Offset(1, 0).SpecialCells(xlCellTypeVisible).Columns(1)
            x = Application.Match(cd.Value, rng, 0)
            If IsError(x) Then
                If InStr(1, cd.Value, cell.Value) > 0 Then cd.Value = cell.Value
            End If
        Next cd
        nxtcell = wsDest.Cells(Rows.Count, "A").End(xlUp).Row + 1
        wsData.Range("A1").CurrentRegion.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A" & nxtcell)
        wsDest.UsedRange.Columns.AutoFit
    End With
Next cell
Rdata.Value = V
wsDest.Range("A1").CurrentRegion.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7), Header:=xlYes
wsData.AutoFilterMode = False
wsData.Activate
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Wow Joe this worked perfectly!
I never would have figured that out.

Thanks great job!!
 
Upvote 0
Wow Joe this worked perfectly!
I never would have figured that out.

Thanks great job!!
You are welcome - thanks for the reply ...... and if you think I provided a solution, perhaps you can mark post #13 to show this.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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