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​
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Looks like you are always pasting the results to the same cell on the destination tab, thereby overwriting previous entries. You need to advance the destination cell for the paste each time you loop. Try changing the With-End With block to something like below:
VBA Code:
Dim NxtCell As Long
With wsData.Range("A1").CurrentRegion
    .AutoFilter field:=1, Criteria1:=cell.Value
    NxtCell = wsdest.Cells(Rows.Count, "A").End(xlUp).Row + 1
    wsData.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy wsdest.Range("A" & NxtCell)
    wsdest.UsedRange.Columns.AutoFit
End With
 
Upvote 0
thanks Joe that was very helpful!
Is there a way to address the wildcard filter so I would get the results for 123Jen456 as Jen?
 
Upvote 0
thanks Joe that was very helpful!
Is there a way to address the wildcard filter so I would get the results for 123Jen456 as Jen?
You are welcome- thanks for the reply.

Sorry, I have no idea what your question is referring to. Did I miss something in your OP?
 
Upvote 0
No you didnt miss anything I failed to mention that as part of the result.

I need to see if I can filter the Data list based on the criteria list and write to Dest tab.
What I ended up missing was the names that were embedded within other text like 123Jen456 didnt get written to the Dest tab.
 
Upvote 0
No you didnt miss anything I failed to mention that as part of the result.

I need to see if I can filter the Data list based on the criteria list and write to Dest tab.
What I ended up missing was the names that were embedded within other text like 123Jen456 didnt get written to the Dest tab.
Try changing this:
VBA Code:
With wsData.Range("A1").CurrentRegion
       .AutoFilter field:=1, Criteria1:=cell.Value
To this:
VBA Code:
With wsData.Range("A1").CurrentRegion
       .AutoFilter field:=1, Criteria1:= "*" & cell.Value & "*"
 
Upvote 0
Joe thanks this worked great!
one thing when it writes the results to Dest tab it always includes the column headers from the Data tab, anyway to just write the results from the filter and not the header too?
 
Upvote 0
Joe thanks this worked great!
one thing when it writes the results to Dest tab it always includes the column headers from the Data tab, anyway to just write the results from the filter and not the header too?
Untested, but try changing this:
VBA Code:
wsData.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy wsdest.Range("A" & NxtCell)
to this:
VBA Code:
wsData.Range("A1").CurrentRegion.Offset(1,0).SpecialCells(xlCellTypeVisible).Copy wsdest.Range("A" & NxtCell)
 
Upvote 0
Joe this worked perfect thank you so much for your help!!
 
Upvote 0
Joe this worked perfect thank you so much for your help!!
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.
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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