Copy values containing part text without duplicates from range to one column

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have dump of data in columns A-D. I tried to use formulas to filter them into one column, but so far no success, so I think VBA would be helpful in this situation.

I want to add a macro button in cell F2, that will:

Take all values from columns A-D that begin with:
  • 9F
  • 9W9
  • 9W0
  • 9P
and move them to one column starting from cell F3 (skip duplicates). Data needs to be pasted as values and sorted in the same order as listed above (so alphabetic rules do not apply).


Is it possible to make it using macro button?
 

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.
Are values spread across the columns F to I or all going to be listed in column F?
 
Upvote 0
Sorry for the mess. But it works 😃
VBA Code:
Sub buttonFunction()
    Dim values()    As String
    Dim lRow        As Integer
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    'Get values to an array
    Dim a           As Long
    a = 0
    For i = 1 To 4
        lRow = Cells(Rows.Count, i).End(xlUp).Row
        For ii = 1 To lRow
            ReDim Preserve values(a)
            values(a) = Cells(ii, i).Value
            a = a + 1
        Next
    Next
    'Get rid of duplicates and strings
    For i = a - 1 To 0 Step -1
        If InStr(1, values(i), "9F") + InStr(1, values(i), "9W9") + InStr(1, values(i), "9W0") + InStr(1, values(i), "9P") = 0 Then
            values(i) = ""
            i = i - 1
        End If
        For ii = i - 1 To 0 Step -1
            If values(i) = values(ii) Then
                values(ii) = ""
            End If
        Next
    Next
    'Write to Coulmn F
    ii = 4
    For i = 0 To a - 1
        If Not values(i) = "" Then
            Cells(ii, 6).Value = values(i)
            ii = ii + 1
        End If
    Next
End Sub
 
Last edited by a moderator:
Upvote 0
Please use the one below:
VBA Code:
Sub buttonFunction()
    Dim values() As String
    Dim lRow As Integer
    'Get values to an array
    Dim a As Long
    a = 0
    For i = 1 To 4
        lRow = Cells(Rows.Count, i).End(xlUp).Row
        For ii = 1 To lRow
            ReDim Preserve values(a)
            values(a) = Cells(ii, i).Value
            a = a + 1
        Next
    Next
    'Get rid of duplicates and strings
    For i = a - 1 To 0 Step -1
        If InStr(1, values(i), "9F") + InStr(1, values(i), "9W9") + InStr(1, values(i), "9W0") + InStr(1, values(i), "9P") = 0 Then
            values(i) = ""
            GoTo NextIteration
        End If
        For ii = i - 1 To 0 Step -1
            If values(i) = values(ii) Then
                values(i) = ""
                GoTo NextIteration
            End If
        Next
NextIteration:
    Next
    'Write to Column F
    ii = 3
    For i = 0 To a - 1
        If Not values(i) = "" Then
            Cells(ii, 6).Value = values(i)
            ii = ii + 1
        End If
    Next
End Sub
 
Upvote 0
Solution
Please use the one below:
VBA Code:
Sub buttonFunction()
    Dim values() As String
    Dim lRow As Integer
    'Get values to an array
    Dim a As Long
    a = 0
    For i = 1 To 4
        lRow = Cells(Rows.Count, i).End(xlUp).Row
        For ii = 1 To lRow
            ReDim Preserve values(a)
            values(a) = Cells(ii, i).Value
            a = a + 1
        Next
    Next
    'Get rid of duplicates and strings
    For i = a - 1 To 0 Step -1
        If InStr(1, values(i), "9F") + InStr(1, values(i), "9W9") + InStr(1, values(i), "9W0") + InStr(1, values(i), "9P") = 0 Then
            values(i) = ""
            GoTo NextIteration
        End If
        For ii = i - 1 To 0 Step -1
            If values(i) = values(ii) Then
                values(i) = ""
                GoTo NextIteration
            End If
        Next
NextIteration:
    Next
    'Write to Column F
    ii = 3
    For i = 0 To a - 1
        If Not values(i) = "" Then
            Cells(ii, 6).Value = values(i)
            ii = ii + 1
        End If
    Next
End Sub
Works perfect! Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,756
Members
448,990
Latest member
Buzzlightyear

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