Find all values that match anything in a range

Digilypse

New Member
Joined
Apr 5, 2012
Messages
6
I just learned how to add in Excel a couple months ago and, although I'm doing the best I can with google, VBA is not easy for me to work with yet. So, while I certainly appreciate any sort of help, explanation-heavy answers would be doubly appreciated!

The excel file I've attached should explain it far more clearly than I can in words, but I'll try to summarize. I need to take a selection of cells and insert them into a table wherever text from a range show up.

Now, I should be - and have been - able to select the cells I want to insert and use the find method to find where to put them. Basically, find all instances of "packageA" and insert; I have a macro for this in the example worksheet. The problem is, I have a lot of different things to find. It needs to also insert on all the packageB's and C's and so on for a good 50 more, AND more are being added continuously. So, I can't just duplicate the macro for packageB and be done with it.

Here's what I'm using to find and insert:

Code:
Sub FindandInsert()


Dim rFoundCell As Range
Dim LastCell As Range
Dim FirstAddr As String




Sheets("Sheet3").Select


With Range("B1:B1195")
    Set LastCell = .Cells(.Cells.Count)
End With
Set rFoundCell = Range("B1:B1195").Find(What:="PackageA", After:=LastCell)




If Not rFoundCell Is Nothing Then
    FirstAddr = rFoundCell.Address
End If
Do Until rFoundCell Is Nothing
    Debug.Print rFoundCell.Address
    
    Set rFoundCell = Range("B1:B1195").FindNext(After:=rFoundCell)
    
    Range("InsertRange").Copy
        
    rFoundCell.Select
        Selection.Offset(0, -1).Insert Shift:=xlDown
    
    If rFoundCell.Offset(-4, 0).Address = FirstAddr Then
        Exit Do
    End If
Loop


End Sub



Simply, instead of using find method to find one thing, I need to find anything that is in a range.

I imagine I can figure out this next part on my own eventually, but, after it's done doing that it needs to go back through and clear out blank rows. Some of the inserts leave one or several blank rows at the top, depending on which type of package they have been inserted on top of.

Seems like it should be so simple but I've spent ages without figuring it out...guess I need more grounding in VBA first.

Crossposted with example file here: http://www.ozgrid.com/forum/showthread.php?t=164129
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Is there a different "Input range" specified for each search term (package)?
If so, is there a table that matches each package to its "Input Range" address?
Is the "Input Range" more than 1 column wide?
Do any of the inserted rows contain values in column B that would be insertmarkers for subsequent packages?
 
Upvote 0
InputRange is a set of Index+match formulas which look at the line below it and match with a table on another sheet. So, there is one InputRange for all of the packages.

The InputRange is 4 rows x 6 columns, the same number of columns as the table it is being inserted into. If the package has 4 kinds of items in it, all 4 rows load; if it has 3 kinds of items, 3 rows load and the 4th on top is Iferror="". B is the second column in the table, which is why I have it offset to the left when it inserts.

Come to think of it, there is a possibility that InputRange might load other sortmakers (if one package type contained other packages) which should not be loaded. I could get around this by finding a special way to handle those particular packages if necessary, but if there is a way around it, that would be extremely convenient.
 
Upvote 0
Wait, disregard that last part. I'll have to handle the package-in-package thing separately for another reason anyway. So no, nothing that would be an insertmaker for subsequent packages.
 
Upvote 0
Here is the InsertRange:





Excel Workbook
CDEFGH
2INSERT*****
312/4/2012Lidget15.265842.452867010.94343229.71636
412/4/2012Bidget0.264645.375365574.4504410720.097
512/4/2012Tridget1.6211164.215313397.05844134.8946
612/4/2012Widget7.6258347.956570017.54784594.3273
712/4/2012packagea2400004800024000
Sheet4


Excel Workbook
CDEFGH
3******
412/4/2012Lidget26.287674.486529209.38382175.21659
512/4/2012Fidget2621512.703225815.2438496.447
612/4/2012Gidget5850812.810360975.3723525.64976
712/4/2012packageb2400004800024000
Sheet4
 
Upvote 0
And here is the table where the InsertRange is to be inserted, and the table with the info for the packages

Excel Workbook
ABCDEF
1DateItems BoughtQuantityPriceFeesPer Item
23/28/2012PackageA111,5001295012,950
33/29/2012PackageB122,0002511525,115
43/30/2012PackageB366,0007534525,115
53/31/2012Widget11,60016001,600
64/1/2012Widget11,00010001,000
74/2/2012Bidget1400400400
84/3/2012Gidget461,058105823
94/4/2012Tridget1600600600
104/5/2012Widget1300300300
114/6/2012Widget1250250250
124/7/2012PackageA120,0002145021,450
134/8/2012Widget11,50015001,500
144/9/2012Tridget1250250250
154/10/2012Widget11,24012401,240
Sheet3
Excel Workbook
CDEFG
14**Quantity(averaged)Can Sell for% of package value
15PackageAWidget3.81600073%
16PackageATridget0.81540014%
17PackageABidget0.13140006%
18PackageALidget7.633007%
19*****
20PackageBGidget2990064%
21PackageBFidget1385027%
22PackageBLidget13.1430010%
Sheet4
 
Upvote 0
I added the PackageInsert keywords on Sheet5:
Excel Workbook
A
1Insert Values
2PackageA
3PackageB
4PackageC
5PackageD
Sheet5


and a dynamic named range called Insert_Values which is set to:
=OFFSET(Sheet5!$A$1,1,0,COUNTA(Sheet5!$A:$A)-1,1)

This code seems to do what you want, but I don't quite follow what the formulas in the input block are doing, so I cannot be sure.
Code:
Option Explicit

Sub FindandInsert()

  Dim lActiveRow As Long
  Dim vInsertValues() As Variant
  Dim lX As Long
  Dim lY As Long
  
  With Sheets("Sheet3")
  
    'Find the last populated cell in column B (2)
    lActiveRow = .Cells(Rows.Count, 2).End(xlUp).Row
    
    'Get the named range into an array in memory
    vInsertValues() = ThisWorkbook.Sheets(5).Range("Insert_Values").Value
    
    For lX = lActiveRow To 2 Step -1 'Start from the bottom of the data so no lines are missed
      For lY = LBound(vInsertValues()) To UBound(vInsertValues()) 'Check each element of the 'Insert Values' range
        If UCase(.Cells(lX, 2).Value) = UCase(vInsertValues(lY, 1)) Then
          'Debug.Print "Active Row=" & lX, lY, vInsertValues(lY, 1)
          'Stop
          Range("InsertRange").Copy
          .Cells(lX, 2).Offset(0, -1).Insert Shift:=xlDown
          
          'If the inserted values in column A are supposed to be dates uncomment this line:
          '.Range(Cells(lX, 1), Cells(lX + 3, 1)).NumberFormat = "m/d/yyyy"
        End If
      Next
    Next
  End With
  
End Sub

Sub DeleteBlankRows()

  'Not sure if this will be needed
  'Convert all formulas to text
  'With Worksheets("Sheet3").Range("A1").CurrentRegion
  '  .Value = .Value
  'End With
  
  'Filter for blanks in column C; delete visible rows
  With Worksheets("Sheet3")
    .AutoFilterMode = False 'Turn off autofilter to ensure nothing is filtered
    .Range("A1").CurrentRegion.AutoFilter Field:=3, Criteria1:="=" 'look for blanks in column C
    'If there are any visible cells in column A, other than the 1 header row
    'select all cells, less the top row, that are visible and delete the rows containing them
    If WorksheetFunction.Subtotal(3, .Columns(1)) > 1 Then _
      .Range("A1").CurrentRegion.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .AutoFilterMode = False 'Turn off autofilter
  End With
  
End Sub

Let me know what is not working if there is a problem.
 
Upvote 0
Oh, wow. That works PERFECTLY. After hours of trying to get this project working only to get stuck on this one piece, it feels amazing to see everything flowing and matching up just like I imagined. Thank you so much!!
 
Upvote 0

Forum statistics

Threads
1,215,730
Messages
6,126,527
Members
449,316
Latest member
sravya

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