Need help VBA modify that find (x) number if not find 1 lower?

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,362
Office Version
  1. 2010
Using Excel 2010

The following VBA first does look the number (3) in the column (I) from top to bottom then copy left side range B7:G7... And paste in to Column L below first empty cell which is working excellent.

If in the case value (3) does not find pop up massage “The criteria value 3 was not found”.

Here I need your help (step-1, I will put in the search value (3)).... if the value (3) is not found, is it possible that the VBA look for the value (2) and if the value (2) also is not found look for the value (1) finally if the value (1) is also not find then pop up massage “The criteria value 0 was not found”. Exit Sub

S.Nn1n2n3n4n5n6Find X Numn1n2n3n4n5n6
112461215512461215
2124612164Step 2 <---After Finding 2 Copy Range B20:G20 And Paste into Column L below the first empty cell
3124612264
4124615414
5124615434
6124616263Step 1 <-----Find 1st 2 in column I and copy left 6 numbers from range B:G....Paste in to Column L below the first empty cell
7124628433
8124637433
9124641433
101241215164
111241215264
121241215284
131241215314
141241516263
151241516283
161241516373
171241516413
181241516433
191241626282
201241626312
211241626372
221241626412
231241626432
241241628312
251241628372
261626283141430
271626283741430
281626313741430

VBA Code:
Sub CopyRow()

    Dim ws As Worksheet
    Dim srcRng As Range, critRng As Range
    Dim srcRowLast As Long, destRowLast As Long, critRow As Long
    Dim srcCols As Long
    Dim critVal As Long                     ' <--- Change to string or double if text or decimal
   
    critVal = 3                             ' <--- Change to value to find or use input box or input cell
    Set ws = Worksheets("Data")             ' <--- Change to your worksheet name
    With ws
        srcRowLast = .Range("I" & Rows.Count).End(xlUp).Row
        Set srcRng = .Range("B1:G" & srcRowLast)
        srcCols = srcRng.Columns.Count
        Set critRng = .Range("I1:I" & srcRowLast)
        destRowLast = .Range("L" & Rows.Count).End(xlUp).Row
    End With
   
    With Application
        critRow = .IfError(.Match(critVal, critRng, 0), 0)
    End With
   
    If critRow = 0 Then
        MsgBox "The criteria value " & critVal & " was not found"
        Exit Sub
    End If
    destRowLast = destRowLast + 1
    ws.Range("L" & destRowLast).Resize(, srcCols).Value = srcRng.Rows(critRow).Value

End Sub

Regards,
Moti
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
try this:
VBA Code:
Sub CopyRow()

    Dim ws As Worksheet
    Dim srcRng As Range, critRng As Range
    Dim srcRowLast As Long, destRowLast As Long, critRow As Long
    Dim srcCols As Long
    Dim critVal As Long                     ' <--- Change to string or double if text or decimal
  
'    critVal = 3                             ' <--- Change to value to find or use input box or input cell
    
    Set ws = Worksheets("Data")             ' <--- Change to your worksheet name
 
    With ws
        srcRowLast = .Range("I" & Rows.Count).End(xlUp).Row
        Set srcRng = .Range("B1:G" & srcRowLast)
        srcCols = srcRng.Columns.Count
        Set critRng = .Range("I1:I" & srcRowLast)
        destRowLast = .Range("L" & Rows.Count).End(xlUp).Row
    End With
  For critVal = 3 To 1 Step -1 
    With Application
        critRow = .IfError(.Match(critVal, critRng, 0), 0)
        If critRow > 0 Then Exit For
    End With
   Next critVal
    If critRow = 0 Then
        MsgBox "The criteria value " & critVal & " was not found"
        Exit Sub
    End If
    destRowLast = destRowLast + 1
    ws.Range("L" & destRowLast).Resize(, srcCols).Value = srcRng.Rows(critRow).Value

End Sub
 
Upvote 1
Solution
try this:
VBA Code:
Sub CopyRow()

    Dim ws As Worksheet
    Dim srcRng As Range, critRng As Range
    Dim srcRowLast As Long, destRowLast As Long, critRow As Long
    Dim srcCols As Long
    Dim critVal As Long                     ' <--- Change to string or double if text or decimal
 
'    critVal = 3                             ' <--- Change to value to find or use input box or input cell
  
    Set ws = Worksheets("Data")             ' <--- Change to your worksheet name
 
    With ws
        srcRowLast = .Range("I" & Rows.Count).End(xlUp).Row
        Set srcRng = .Range("B1:G" & srcRowLast)
        srcCols = srcRng.Columns.Count
        Set critRng = .Range("I1:I" & srcRowLast)
        destRowLast = .Range("L" & Rows.Count).End(xlUp).Row
    End With
  For critVal = 3 To 1 Step -1
    With Application
        critRow = .IfError(.Match(critVal, critRng, 0), 0)
        If critRow > 0 Then Exit For
    End With
   Next critVal
    If critRow = 0 Then
        MsgBox "The criteria value " & critVal & " was not found"
        Exit Sub
    End If
    destRowLast = destRowLast + 1
    ws.Range("L" & destRowLast).Resize(, srcCols).Value = srcRng.Rows(critRow).Value

End Sub
offthelip, I am happy and appreciate your help, your VBA modification is working spot-on! (y)

I wish you a healthy and happy new year 2024. Good Luck!

My Best Regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,094
Latest member
mystic19

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