Find (x) number copy some left cell and paste into another location

motilulla

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

I want a VBA which can look first (x) number from top to bottom in the column (I) in this example look for number (2) which is finding in cell (I20) copy left side range B20:G20... And paste in to Column L below the first empty cell

Note: if search number is not found exit sub

S.Nn1n2n3n4n5n6Find X Numn1n2n3n4n5n6
112461215512461215
2124612164124162628Step 2 <---After Finding 2 Copy Range B20:G20 And Paste into Column L below the first empty cell
3124612264
4124615414
5124615434
6124616263
7124628433
8124637433
9124641433
101241215164
111241215264
121241215284
131241215314
141241516263
151241516283
161241516373
171241516413
181241516433
191241626282Step 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
201241626312
211241626372
221241626412
231241626432
241241628312
251241628372
261626283141430
271626283741430
281626313741430

Regards,
Moti
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Do you only want the first occurence of "2" copied ?
Do you want the copied data removed from the left side ?
 
Upvote 0
Let's start with 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 = 2                             ' <--- 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
 
Upvote 1
Solution
Do you only want the first occurence of "2" copied ?
Do you want the copied data removed from the left side ?
Let's start with 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 = 2                             ' <--- 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
Alex Blakenburg, yes I wanted the first occurrence of "2" should only be copied, and that is exactly your VBA is working spot on it. (y)

I am happy and appreciate your kind help very much.

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

My Best Regards,
Moti :)

I did not even thought about 2nd options you gave me. That the copied data can be removed from the left side? I am thinking now it would be good idea please if it is not much trouble for you can you make it possible. Thank you
 
Upvote 0
Thanks for letting me know.
To delete from A:I in the selected row replace:
VBA Code:
   ws.Range("L" & destRowLast).Resize(, srcCols).Value = srcRng.Rows(critRow).Value

With this:
VBA Code:
    With srcRng.Rows(critRow)
        ws.Range("L" & destRowLast).Resize(, srcCols).Value = .Value
        .Offset(, -1).Resize(, .Columns.Count + 3).Delete Shift:=xlUp        ' Expand range to A:I and delete
    End With
 
Upvote 1
Thanks for letting me know.
To delete from A:I in the selected row replace:
VBA Code:
   ws.Range("L" & destRowLast).Resize(, srcCols).Value = srcRng.Rows(critRow).Value

With this:
VBA Code:
    With srcRng.Rows(critRow)
        ws.Range("L" & destRowLast).Resize(, srcCols).Value = .Value
        .Offset(, -1).Resize(, .Columns.Count + 3).Delete Shift:=xlUp        ' Expand range to A:I and delete
    End With
Alex Blakenburg, I am grateful to you for this modification. It is really yours amazing thought to remove copied data from the left side. It has been really useful.🙏

I am so happy. Good Luck! Happy Year 2024! Stay Blessed.

My Best Regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,598
Members
449,109
Latest member
Sebas8956

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