VBA: If satisfying condition on partial match

Adar123

Board Regular
Joined
Apr 1, 2018
Messages
83
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
I am using the following code to copy data from one cell to another. The challenge I am having is with with the definition of the if condition as a partial match: The cells in column A contain information in the following format: RANDOM_TEXT_25DP.

I need to copy data whenever cell A contains _25DP at the end. How do I do that?

Current code

VBA Code:
Sub MoveData()

Dim k As Integer, kMove As Integer
Dim Kcolumn As Long, Ccolumn As Long
Dim refCell As Range, outputCell As Range

Kcolumn = 7                 ' destination
Ccolumn = 6                  ' reference

For k = 2 To 5000
    If Cells(k, 1) = "_25DP" Then
        kMove = k -1
        Set refCell = Cells(k, Ccolumn)
        Set outputCell = Cells(kMove, Kcolumn)
        outputCell.Value = refCell.Value
    End If
Next k

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try just changing your 'If' line to this

VBA Code:
If Right(Cells(k, 1), 5) = "_25DP" Then

or another option would be

VBA Code:
If Cells(k, 1) Like "*_25DP" Then
 
Upvote 0
Ah, something new, didn't know there is "Like"

Confirm. It works and thanks for the solution!
 
Upvote 0
Following this logic of the code with "if like" could I instruct deletion of lines? I tried the code below but I am ening up wiping all data for some reason.

Propose code:
VBA Code:
For k = 2 To lastRow
If Cells(k, 1) Like "*_25DP" Then
Rows(k).EntireRow.Delete
End If
Next k

Original code (using autofilter which works but seems to be quite heavy)
VBA Code:
With rng
.AutoFilter Field:=1, Criteria1:="=*_5DP*"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
ActiveSheet.AutoFilterMode = False

With rng
.AutoFilter Field:=1, Criteria1:="=*_15DP*"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
ActiveSheet.AutoFilterMode = False
 
Upvote 0
When deleting in a loop like that, best to start at the bottom & work up.
Try
VBA Code:
For k = lastRow to 2 step -1

works but seems to be quite heavy
By 'heavy' do you mean slow? If so, here is a much faster way. It assumes the *_25DP values are in column A & there is a header row.

VBA Code:
Sub Del_25DP()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) Like "*_25DP" Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A2").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Yes, under 'heavy' I mean slow. In fact, my newly proposed code takes even longer.

Does your proposed code support multiple matches with an OR equivalent? I have 10 parameters to check for. _5DP, 10DP, etc. This will become War and Piece macro if I keep copy-pasting this.
 
Upvote 0
I have 10 parameters to check for. _5DP, 10DP, etc.
Can we see what all the parameters actually are to look for similarities/patterns/lengths etc to consider the best way. I'm fairly confident there will be a fast way.

Any chance that we could also have a small sample (say 10-20 rows) of the data (preferably with XL2BB) that has to be assessed for these parameters so we have something realistic to test with?
Make sure some of the sample include a 'delete parameter' and some don't.
 
Upvote 0
DataSet.csv
ABC
1KeyCurrencyPrice
2W_ROUNDED:FALSE_SEP2020_5DPUSD0.007461
3W_ROUNDED:FALSE_SEP2020_10DPUSD0.015774
4W_ROUNDED:FALSE_SEP2020_15DPUSD0.025059
5C_ROUNDED:FALSE_SEP2020_20DPUSD0.035233
6C_ROUNDED:FALSE_SEP2020_25DPUSD0.046266
7C_ROUNDED:FALSE_SEP2020_30DPUSD0.058183
8C_ROUNDED:FALSE_SEP2020_35DPUSD0.071341
9C_ROUNDED:FALSE_SEP2020_40DPUSD0.086086
10C_ROUNDED:FALSE_SEP2020_45DPUSD0.102748
11C_ROUNDED:FALSE_SEP2020_50DPUSD0.121778
12C_ROUNDED:FALSE_SEP2020_ATMUSD0.114699
13C_ROUNDED:FALSE_SEP2020_50DCUSD0.109032
14C_ROUNDED:FALSE_SEP2020_45DCUSD0.093831
15C_ROUNDED:FALSE_SEP2020_40DCUSD0.080055
DataSet


Thanks, this is the snapshot of a dataset.

I need to delete all but the ones containing _5DP, _40DP and _40DC.
 
Upvote 0
Thanks for the sample data.

I need to delete all but the ones containing _5DP, _40DP and _40DC
OK, try this version for that

VBA Code:
Sub Del_Except()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    Select Case Mid(a(i, 1), InStrRev(a(i, 1), "_"))
      Case "_5DP", "_40DP", "_40DC"   '<- Keep these
      Case Else                       '<- Delete everything else
        b(i, 1) = 1
        k = k + 1
    End Select
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A2").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
This code is above my skills, based on my test it works. Thanks for that.
 
Upvote 0

Forum statistics

Threads
1,215,087
Messages
6,123,050
Members
449,092
Latest member
ikke

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