Retain a data value and remove other cell contents.

Astronaut01

New Member
Joined
Jan 26, 2022
Messages
18
Office Version
  1. 2016
Platform
  1. Windows
Hello excel vba experts
Im quite new to excel vba and need to create a vba tool that can:
1. Search for all data value/s that has the "RTN000..." in column F. Some cells in column F has mulitple "RTNs..." and symbols and words.
2. Once found, it should copy that specific "RTN" together with all the row data, to a sheet i named Output.

Been trying to see if a use of wildcard would work but not sure how. Many thanks in advance for all your solutions.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
is that the VBA-version of doing by hand
* filter column F on "contains RTN000"
* copy the visible rows to sheet "output" ?
 
Upvote 0
is that the VBA-version of doing by hand
* filter column F on "contains RTN000"
* copy the visible rows to sheet "output" ?
Hi bsalv
You mean by recording macro?.. No if i understand. But im trying to create a vba code which uses an activeX button. The vba code should be able to just retain all RTNs in a cell.
Yes it should copy those rows containing RTNs found, to sheet output.
TIA
 
Upvote 0
VBA Code:
Sub Filteren()

     Set c = Sheets("dest").Range("a1")                         'destionation topleft
     c.Resize(1000, 10).ClearContents                           'clear

     With Sheets("mysheet").Range("$A$1").CurrentRegion
          .AutoFilter Field:=6, Criteria1:="=*RN000*"
          .SpecialCells(xlVisible).Copy c
     End With

End Sub
 
Upvote 0
hi bsalv
I tried your vba code and it is working but i think the code is returning all RTNs with three zeros in it. Is there a way to only return specific RTNs that I would put in my "RTN TO SEARCH" column in my "findap" sheet? Pls see my images. The steps would be:
1. In the "findap" sheet, i will enter specific RTNs under RTN TO SEARCH column, hit the Search RTN button... then step 2..
2. It then searches those RTNs from the Sheet1 which is the report file. Take note that there are multiple RTNs in some cells under column F "Value".
3. It should then output those specific RTNs in my "output" sheet.

Thanks in advance again bsalv. I am confident you will be able to help me with this one as it is almost there and just need to find the specific RTNs. Looking forward to your response.
 
Upvote 0
1st point is no problem, 2nd also, but the 1st combined with the 2nd is.
I saw no image of your data, perhaps later, 5 rows (without sensitive data).
In total how many rows is this ?

I think i already know how.
i assume the 1st column of your data is column A, what is the last column ?
Because i 'm adding an auxiliary column there.
 
Upvote 0
At the RHS, that green table are all your searchvalues.
The green range is a defined nama "Wanted"
The column H is that auxiliary column and is the 8th column of that table, that's the 8 in the VBA-code, you have to change that one to your situation.

If the formula in column H doesn't work, perhaps it's a matrixformula, in my 2021-version, i can't check that. So enter that with CTRL+SHIFT+Enter instead of a normal enter
VBA Code:
Sub Filteren()

     Set c = Sheets("dest").Range("a1")                         'destionation topleft
     c.Resize(1000, 10).ClearContents                           'clear

     With Sheets("mysheet").Range("$A$1").CurrentRegion
          .AutoFilter 8, 1
          .SpecialCells(xlVisible).Copy c
          .AutoFilter
     End With

End Sub
Map1
ABCDEFGHIJK
1abcdefgauxiliaryWanted
2A2B2C2D2E2RTN0002G20RTN0006
3A3B3C3D3E3RTN0003G30RTN0009
4A4B4C4D4E4RTN0004G40RTN00012
5A5B5C5D5E5RTN0005G50RTN00015
6A6B6C6D6E6RTN0006G61RTN00018
7A7B7C7D7E7RTN0007G70RTN00021
8A8B8C8D8E8RTN0008G80RTN00024
9A9B9C9D9E9xxxxxxxxxxxxxxx RTN0009xxxxxxxxxxxRTN0001G91RTN00027
10A10B10C10D10E10RTN00010G100RTN00030
11A11B11C11D11E11RTN00011G110
12A12B12C12D12E12RTN00012G121
13A13B13C13D13E13RTN00013G130
14A14B14C14D14E14RTN00014G140
15A15B15C15D15E15RTN00015G151
16A16B16C16D16E16RTN00016G160
17A17B17C17D17E17RTN00017G170
18A18B18C18D18E18RTN00018G181
19A19B19C19D19E19RTN00019G190
20A20B20C20D20E20G200
21A21B21C21D21E21RTN00021G211
22A22B22C22D22E22RTN00022G220
23A23B23C23D23E23RTN00023G230
24A24B24C24D24E24RTN00024G241
25A25B25C25D25E25RTN00025G250
26A26B26C26D26E26RTN00026G260
27A27B27C27D27E27RTN00027G271
28A28B28C28D28E28RTN00028G280
29A29B29C29D29E29RTN00029G290
MySheet
Cell Formulas
RangeFormula
A2:E29,G2:G29A2=CHAR(COLUMN()+64)&ROW()
H2:H29H2=MAX(--ISNUMBER(SEARCH(Wanted,F2)))
Named Ranges
NameRefers ToCells
Wanted=TBL_Wanted[Wanted]H2:H29
 
Upvote 0
Hi bsalv
The code works. However, i'd like to display the result on my "output" sheet. Problem is, my "findap" sheet is only allowing me to put 2 RTNs at a time. It wont return the results if more than 2 search values. below is my code:
Private Sub CommandButton1_Click()
Dim Sheet1sh As Worksheet
Dim findapsh As Worksheet
Dim outputsh As Worksheet
Set Sheet1sh = ThisWorkbook.Sheets("Sheet1")
Set findapsh = ThisWorkbook.Sheets("findap")
Set outputsh = ThisWorkbook.Sheets("output")

outputsh.UsedRange.Clear
Sheet1sh.AutoFilterMode = False
'-----
Dim LastRow As Long
Dim Rng As Range, Cell As Range

'finds last row with data in column A
LastRow = findapsh.Cells(findapsh.Rows.Count, 1).End(xlUp).row

'sets the range in column A
Set Rng = Range("A2:A" & LastRow)

For Each Cell In Rng
Cell.value = "*" & Cell.value & "*"
Next Cell
'-----
Dim rtnlist() As String
Dim n As Integer
'this part below, im trying to fix as it only allows me to search max of two RTNs on column A of the "findap" sheet.
'it only put the 1st and 2nd RTN value in the "output" sheet.
'it ignores the 3rd, 4th...RTNs that need to be searched.

n = Application.WorksheetFunction.CountA(findapsh.Range("A:A")) - 2
ReDim rtnlist(n) As String
Dim i As Integer
For i = 0 To n
rtnlist(i) = findapsh.Range("A" & i + 2)
Next i
Sheet1sh.UsedRange.AutoFilter 6, rtnlist(), xlFilterValues
Sheet1sh.UsedRange.Copy outputsh.Range("A1")
Sheet1sh.AutoFilterMode = False
MsgBox ("Search result copied to output sheet.")
End Sub

1643935876090.png

1643935921597.png
 
Upvote 0
At the RHS, that green table are all your searchvalues.
The green range is a defined nama "Wanted"
The column H is that auxiliary column and is the 8th column of that table, that's the 8 in the VBA-code, you have to change that one to your situation.

If the formula in column H doesn't work, perhaps it's a matrixformula, in my 2021-version, i can't check that. So enter that with CTRL+SHIFT+Enter instead of a normal enter
VBA Code:
Sub Filteren()

     Set c = Sheets("dest").Range("a1")                         'destionation topleft
     c.Resize(1000, 10).ClearContents                           'clear

     With Sheets("mysheet").Range("$A$1").CurrentRegion
          .AutoFilter 8, 1
          .SpecialCells(xlVisible).Copy c
          .AutoFilter
     End With

End Sub
Map1
ABCDEFGHIJK
1abcdefgauxiliaryWanted
2A2B2C2D2E2RTN0002G20RTN0006
3A3B3C3D3E3RTN0003G30RTN0009
4A4B4C4D4E4RTN0004G40RTN00012
5A5B5C5D5E5RTN0005G50RTN00015
6A6B6C6D6E6RTN0006G61RTN00018
7A7B7C7D7E7RTN0007G70RTN00021
8A8B8C8D8E8RTN0008G80RTN00024
9A9B9C9D9E9xxxxxxxxxxxxxxx RTN0009xxxxxxxxxxxRTN0001G91RTN00027
10A10B10C10D10E10RTN00010G100RTN00030
11A11B11C11D11E11RTN00011G110
12A12B12C12D12E12RTN00012G121
13A13B13C13D13E13RTN00013G130
14A14B14C14D14E14RTN00014G140
15A15B15C15D15E15RTN00015G151
16A16B16C16D16E16RTN00016G160
17A17B17C17D17E17RTN00017G170
18A18B18C18D18E18RTN00018G181
19A19B19C19D19E19RTN00019G190
20A20B20C20D20E20G200
21A21B21C21D21E21RTN00021G211
22A22B22C22D22E22RTN00022G220
23A23B23C23D23E23RTN00023G230
24A24B24C24D24E24RTN00024G241
25A25B25C25D25E25RTN00025G250
26A26B26C26D26E26RTN00026G260
27A27B27C27D27E27RTN00027G271
28A28B28C28D28E28RTN00028G280
29A29B29C29D29E29RTN00029G290
MySheet
Cell Formulas
RangeFormula
A2:E29,G2:G29A2=CHAR(COLUMN()+64)&ROW()
H2:H29H2=MAX(--ISNUMBER(SEARCH(Wanted,F2)))
Named Ranges
NameRefers ToCells
Wanted=TBL_Wanted[Wanted]H2:H29
Hi Bsalv
Any update on my latest screenshot?
Tia
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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