Offset not working correctly

Francois084

New Member
Joined
Jun 23, 2022
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hi

I'm having trouble with my offset
I used my previous code and changed it so that it look for a specific word and search over multiple worksheets
It does find the word and copies it over to the new sheet.
but when it find the 2nd one it overwrites the previous line on the new sheet

Thank you for helping

VBA Code:
Sub OtherSearchString()

Dim iIndex As Integer
Dim ws As Excel.Worksheet
Dim lr As Long
Dim r As Long
Dim C As Range
Dim Target As Worksheet
b = 0
Set Target = ActiveWorkbook.Worksheets("Supp or Dept")

    For iIndex = 1 To ActiveWorkbook.Worksheets.Count
        Set ws = Worksheets(iIndex)
        ws.Activate
        '   Find last row in column E on Source sheet
    lr = ws.Cells(Rows.Count, "C").End(xlUp).Row

'   Loop through each row in column E backwards
    For r = lr To 1 Step -1
'       Build "c" range
        Set C = ws.Range("C" & r)
'       Proceed with rest of your code
        If C Like "*SUPP*" Then
            C.EntireRow.copy
            Target.Range("A" & Rows.Count).End(xlDown).Offset(1).PasteSpecial
            C.EntireRow.Delete
        ElseIf C Like "*DEPT*" Then
            C.EntireRow.copy
            b = Target.Range("A" & Rows.Count).End(xlUp).Offset(3).PasteSpecial
            b = b + 1
            C.EntireRow.Delete
            
        End If
    Next r
Next iIndex
End Sub
 
It works properly for me. This is what I get:
Book4
ABC
1ABC
2SUPPLIER 1
3SUPPLIER 2
4SUPPLIER 3
5SUPPLIER 4
6SUPPLIER 1
7SUPPLIER 2
8SUPPLIER 3
9SUPPLIER 4
Supp or Dept

Please put headers in row 1 of both source sheets. Also, the macro I suggested avoids using a loop which is very slow.
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You are using 2 different macros which is not necessary. Everything can be done with one macro. Are all your sheets in the same workbook? Could you upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
You are using 2 different macros which is not necessary. Everything can be done with one macro. Are all your sheets in the same workbook? Could you upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
I only started with VBA this week. :):)
Still learning. I will try and make 1 Sub form both

Thank you
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,174
Members
449,071
Latest member
cdnMech

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