Macro Modification

CONFUSED_AS_USUAL

Board Regular
Joined
Jul 6, 2017
Messages
59
I am not well versed in VBA. Below was made for me a while back and works like a charm. However, I would like "Input Box" option removed and replaced with something I can just go into the code and modify the key word(s) myself as most of what I need moved is currently limited to the same 6 search criteria.

Thank you.

Sub DGB_REMOVE_TZ_QC()
'Auto_Filter_This_New()
Application.ScreenUpdating = False
'Modified 8-29-17 1:00 PM EDT
Dim Col As Long
Dim One As String
Dim Two As String
One = "DGP" 'Change sheet name here
Two = "TZANET710" 'Change sheet name here
Col = "13" ' Change Column to search here
Sheets(One).Activate
Sheets(One).Rows(1).Copy Sheets(Two).Rows(1)
Lastrow = Sheets(One).Cells(Rows.Count, Col).End(xlUp).Row
Lastrowa = Sheets(Two).Cells(Rows.Count, Col).End(xlUp).Row + 1
Dim ans As String
ans = InputBox("Enter value to search for")
With Worksheets(One).Rows("1:" & Lastrow)
.AutoFilter
.AutoFilter field:=Col, Criteria1:=ans
.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy Worksheets(Two).Range("A" & Lastrowa)
' .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.Offset(1, 0).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp


End With
ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
It is just as simple as replacing this line:
Code:
ans = InputBox("Enter value to search for")
with something like this:
Code:
ans = "Dog"
where you just type in whatever value you want to look for.

If you have a set of 6 words you are looking for that will always be the same, I would recommend storing them in an array, and then looping through the array.
 
Upvote 0
Hi Joe - Yes, that works well for 1. Unfortunately, I have no idea how to do an array of 6 different words. Any help would be appreciated.
 
Upvote 0
Maybe something like this:
Code:
Sub DGB_REMOVE_TZ_QC()
'Auto_Filter_This_New()
Application.ScreenUpdating = False
'Modified 8-29-17 1:00 PM EDT
Dim Col As Long
Dim One As String
Dim Two As String
[COLOR=#ff0000]Dim ans As Variant
Dim i As Long[/COLOR]
    
[COLOR=#ff0000]ans = Array("Dog", "Cat", "Mouse", "Frog", "Toad", "Bear") ' 6 words to look for[/COLOR]
One = "DGP" 'Change sheet name here
Two = "TZANET710" 'Change sheet name here
Col = "13" ' Change Column to search here

Sheets(One).Activate
Sheets(One).Rows(1).Copy Sheets(Two).Rows(1)
Lastrow = Sheets(One).Cells(Rows.Count, Col).End(xlUp).Row
Lastrowa = Sheets(Two).Cells(Rows.Count, Col).End(xlUp).Row + 1

[COLOR=#ff0000]For i = LBound(ans) To UBound(ans)[/COLOR]
    With Worksheets(One).Rows("1:" & Lastrow)
        .AutoFilter
        .AutoFilter field:=Col, Criteria1:=[COLOR=#ff0000]ans(i)[/COLOR]
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy Worksheets(Two).Range("A" & Lastrowa)
'        .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
    End With
[COLOR=#ff0000]Next i
[/COLOR]
ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True

End Sub
 
Upvote 0
I locates and deletes all the words it is looking for. However, it is not transferring all of those records over to the "TZANET710" sheet. In the test I did, I put in 4 words that were in fact in the "DGP" sheet and left the "Toad" and "Bear" there as is. The last two were copied over - not the first two. Keep in mind that there can be more than 1 instance of the same word. Not sure if that makes a difference with your code.

Thks
 
Upvote 0
I did not analyze the rest of your code, I was just showing you how to apply an array.
I think the way that filtering is done might not work "as-is" with a loop (instead of just for one single value) without making some adjustments.
I will need some time to recreate the scenario with data. I should have some time to try to do that later on today.
 
Upvote 0
OK. I think I kind of need to understand the original structure of your data, so I can try to re-create the same scenario.
Can you post some sample data, or at least let me know which columns you have populated?
 
Upvote 0
Joe,

I have about 30 columns populated. Here are the first 13. The "word" is being searched in the 13th column.

Here is a snippet.

COMMDateDoc NumItem CodeDescriptionQuantityTotalPriceCostTotal Cost% Gross ProfitDiscount %Code
ok20171201380724xxxxxx72113.041.57001-1TZA710
ok20171208380729xxxxxx19.99.900110BROCCA180
20171202380750xxxxxx1440010TZA710
20171205380754xxxxxx21.20.60010IVEND17
20171201380757xxxxxx26130.50010PETINOS590
20171204380792xxxxxx11.991.990010IVEND17
20171208380794xxxxxx124.2624.260019.98IVEND17
20171130380796xxxxxx136.2336.230019.99TIKIMING401
20171130380804xxxxxx124.8924.890019.98FORCIER435
20171130380815xxxxxx11.081.0800110IVEND17
20171201380815xxxxxx10.680.680019.33IVEND17
20171130380815xxxxxx12.252.2500110IVEND17
20171205380830xxxxxx322.57.50010IVEND17
20171130380849xxxxxx16.756.7500110IVEND17
20171205380861xxxxxx11.761.760019.74BARBIES15
20171205380862xxxxxx13253250017.14BOULANGERIE2180
20171208380878xxxxxx125.880.490010IVEND17
paul 20171202380880xxxxxx17.167.160019.94IVEND17
20171201380881xxxxxx190900010IVEND17
20171208380885xxxxxx37.472.490010IVEND17
20171205380888xxxxxx452.213.0500110SHERBROOKE5121
20171202380893xxxxxx1128.5128.50010IVEND17
20171201380896xxxxxx10.750.750010IVEND17
20171207380905xxxxxx16.36.300110IVEND17
20171130380906xxxxxx151.351.300110IVEND17
20171201380952xxxxxx62.40.40010TZA710
mike ??20171204380957xxxxxx1028.32.830010TZA710
20171202380969xxxxxx124840010TZA710
20171204380975xxxxxx110100010TZA710
20171207380980xxxxxx10.720.7200110IVEND17
20171204380982xxxxxx7297.21.3500110MIKES621
20171207380983xxxxxx120.820.80010IVEND17
20171207380993xxxxxx112120010IVEND17
20171207380996xxxxxx120020000118.36GRANDEROUE1250
20171205380997xxxxxx28.94.450010IVEND17
20171205380717xxxxxx4962901.65.855.852901.600TZA710
2017120238526xxxxxx-288-4321.5-0.89256.321.590resthotel1318
20171207380990xxxxxx1002102.10.55550.7408710759CAN.INC.
ok20171201380925xxxxxx2403001.250.952280.240ROMA8050
20171207380728xxxxxx288167.040.580.57164.160.02-1TZA710
20171130380864xxxxxx192336.961.761.33255.360.249.74MARATHON3313
20171208380756xxxxxx108264.62.451.35145.80.450FOGO20820
2017120738534xxxxxx-120-6125.1-3.654381.720GRANDEROUE1250
20171207380743xxxxxx1004954.953.113110.3713.91SHERATON1201
20171207380990xxxxxx72788.410.955.1367.20.5308710759CAN.INC.
20171207380916xxxxxx36812.250.5820.880.740IVEND17
20171204380917xxxxxx84247.82.951.87157.080.370OTTAVIOS6880
20171207380990xxxxxx71688.79.75.72406.120.4108710759CAN.INC.
20171208380990xxxxxx723244.52.89208.080.3608710759CAN.INC.
20171207380990xxxxxx72644.48.955.83419.760.3508710759CAN.INC.
20171205380746xxxxxx10889.640.830.8288.560.01-1TZA710
20171208380724xxxxxx108140.41.31.29139.320.01-1TZA710
20171202380901xxxxxx69227.73.32.29158.010.310SAPORE4490
20171207380728xxxxxx96125.761.311.28122.880.02-1TZA710
20171201380982xxxxxx72116.641.621.1985.680.2710MIKES621
20171202380871xxxxxx7268.40.950.750.40.260IVEND17
20171130380758xxxxxx7275.61.050.7956.880.250IVEND17
20171207380990xxxxxx72615.68.556.56472.320.2308710759CAN.INC.
20171208380917xxxxxx72212.42.952.33167.760.210OTTAVIOS6880
20171130380990xxxxxx603305.53.652190.3408710759CAN.INC.
2017120138528xxxxxx-48-237.64.95-2.67128.161.540SCAROLIES950
20171130380746xxxxxx84197.42.352.23187.320.05-1TZA710
20171205380782xxxxxx2440.561.690.4611.040.730ALEXANDRE518
20171205380863xxxxxx2440.561.690.4611.040.730Daou 519
20171201380990xxxxxx50122.52.451.3969.50.4308710759CAN.INC.
20171201380972xxxxxx367220.8229.520.590playground1500
20171201380761xxxxxx48429.848.965.65271.20.379.95MARCHEBK4245
20171202381013xxxxxx48309.66.454.2201.60.354.44KAMPAI1616
20171207380843xxxxxx4888.81.851.2158.080.350IVEND17
2017120538531xxxxxx-48-88.81.85-1.2158.081.650BARBIES70
20171201380728xxxxxx7251.080.550.750.40.010TZA710
20171201380728xxxxxx7232.40.450.4532.400TZA710
20171208380916xxxxxx3666.61.850.9333.480.50IVEND17
20171201380782xxxxxx3641.41.150.621.60.480ALEXANDRE518
20171201380728xxxxxx6839.440.580.5839.4400TZA710
20171205380808xxxxxx40701.751.05420.40IVEND17
20171205380812xxxxxx3671.641.991.0838.880.460barbie7850
20171201380990xxxxxx48516.4810.767.88378.240.27108710759CAN.INC.
20171202380736xxxxxx2463.722.660.9823.520.639.83ROS1000
20171207380898xxxxxx4881.121.691.2961.920.240IVEND17
20171208381013xxxxxx483246.755.18248.640.230KAMPAI1616
20171208381013xxxxxx48309.66.455.18248.640.24.44KAMPAI1616
20171207380990xxxxxx302558.54.46133.80.4808710759CAN.INC.
20171207381013xxxxxx45290.256.455.18233.10.24.44KAMPAI1616
20171201380756xxxxxx3632.40.90.5921.240.3410FOGO20820
20171202380990xxxxxx30510179.48284.40.4408710759CAN.INC.
20171207380861xxxxxx1826.731.490.590.669.7BARBIES15
20171201380831xxxxxx329.859.950.571.710.940IVEND17
20171201380882xxxxxx24271.130.5312.720.539.6IVEND17
20171204380738xxxxxx36114.843.192.3283.520.270IVEND17
20171207380969xxxxxx48174.612.823.54169.920.030TZA710
20171207380746xxxxxx48122.882.562.51200.02-2.5TZA710
20171207380765xxxxxx20.980.490.020.040.960IVEND17
20171204380724xxxxxx4894.561.971.9593.60.01-1TZA710
20171205380724xxxxxx4862.881.311.362.40.01-1TZA710
20171205380724xxxxxx4862.881.311.362.40.01-1TZA710
20171205380782xxxxxx36106.22.952.279.20.250ALEXANDRE518
20171207380740xxxxxx4852.81.11.152.800TZA710
20171208380969xxxxxx4872.961.521.5272.9600TZA710
20171130380978xxxxxx2439.841.660.8319.920.50LECOING8297
20171201380718xxxxxx24107.044.462.25540.510MONZA1197
20171207380827xxxxxx367221.5957.240.210BARBIES70
20171130380986xxxxxx129.840.820.222.640.730IVEND17
20171207380771xxxxxx1042.754.280.979.70.779.9DEUX92
20171207380932xxxxxx30121.234.042.8585.50.2910.02IVEND17
20171202380719xxxxxx36210.65.854.96178.560.1510NARCISSE93
20171207380756xxxxxx3237.441.170.928.80.2310FOGO20820
20171205380811xxxxxx24141.365.893.4783.280.410HOPKINS5626
20171204380850xxxxxx201356.753.3366.60.5110ELIXOR3237
20171207380880xxxxxx2015.30.770.387.60.59.41IVEND17

<colgroup><col width="92" span="2" style="width:69pt"> <col width="92" span="11" style="width:69pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
OK, I see the problem. Since the last row on sheet 2 keeps growing as we add data to it, we need to recalculate that value with every iteration of our loop.
So I just moved that line inside the loop, i.e.
Code:
Sub DGB_REMOVE_TZ_QC()
'Auto_Filter_This_New()
Application.ScreenUpdating = False
'Modified 8-29-17 1:00 PM EDT
Dim Col As Long
Dim One As String
Dim Two As String
Dim ans As Variant
Dim i As Long
    
ans = Array("TZA710", "IVEND17", "BARBIES15", "Frog", "Toad", "Bear") ' 6 words to look for
One = "DGP" 'Change sheet name here
Two = "TZANET710" 'Change sheet name here
Col = "13" ' Change Column to search here

Sheets(One).Activate
Sheets(One).Rows(1).Copy Sheets(Two).Rows(1)
Lastrow = Sheets(One).Cells(Rows.Count, Col).End(xlUp).Row

For i = LBound(ans) To UBound(ans)
    Lastrowa = Sheets(Two).Cells(Rows.Count, Col).End(xlUp).Row + 1
    With Worksheets(One).Rows("1:" & Lastrow)
        .AutoFilter
        .AutoFilter field:=Col, Criteria1:=ans(i)
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy Worksheets(Two).Range("A" & Lastrowa)
'        .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
    End With
Next i

ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,192
Messages
6,129,443
Members
449,509
Latest member
ajbooisen

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