I can only get this VBA code to work if I add a column onto the end of the dataset.

dwmlr

New Member
Joined
Oct 7, 2016
Messages
13
Hello. I'm using this code, but can only seem to get it to work if I add a redundant column at the end of my dataset. In order to to that, I'm referencing column M, but in reality, I'd like to just be able to reference column H. I'm almost positive that it has to do with the offset and resize parameters?
VBA Code:
Option Explicit
Sub ExportLogisticsRecords()

Dim StatusCol As Range
Dim Status As Range
Dim PasteCell As Range

Set StatusCol = Sheet1.Range("M2:M10002")

For Each Status In StatusCol

    If Sheet5.Range("A2") = "" Then
        Set PasteCell = Sheet5.Range("A2")
    Else

        Set PasteCell = Sheet5.Range("A1").End(xlDown).Offset(1, 0)
    End If

    If Status Like "*-L" Then Status.Offset(0, -12).Resize(1, 12).Copy PasteCell


Next Status
        
End Sub
2023-10-04 22_29_25-Export Logistics.xlsm - Excel.png
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I'm almost positive that it has to do with the offset and resize parameters?
You're right, that's the problem. Rather than fix that, I'd like to suggest the following alternative code for you. Please try it on a copy of your workbook.
VBA Code:
Sub Copy_Dash_L()
    If Sheet1.AutoFilterMode Then Sheet1.AutoFilter.ShowAllData
    With Sheet1.Range("A1").CurrentRegion
        .AutoFilter 8, "*-L"
        If .SpecialCells(xlCellTypeVisible).Address <> .Rows(1).Address Then
            .Offset(1).Resize(.Rows.Count - 1).Copy Sheet5.Cells(Rows.Count, 1).End(xlUp).Offset(1)
        End If
        .AutoFilter
    End With
End Sub
 
Upvote 0
You're right, that's the problem. Rather than fix that, I'd like to suggest the following alternative code for you. Please try it on a copy of your workbook.
VBA Code:
Sub Copy_Dash_L()
    If Sheet1.AutoFilterMode Then Sheet1.AutoFilter.ShowAllData
    With Sheet1.Range("A1").CurrentRegion
        .AutoFilter 8, "*-L"
        If .SpecialCells(xlCellTypeVisible).Address <> .Rows(1).Address Then
            .Offset(1).Resize(.Rows.Count - 1).Copy Sheet5.Cells(Rows.Count, 1).End(xlUp).Offset(1)
        End If
        .AutoFilter
    End With
End Sub
That worked great, but it missed two records. I'm going to investigate now. Not to mention that it was soooooo much faster than the code I was using. LOL

Thank you very much Kevin.
 
Upvote 0
If it missed 2 records then it must be something to do with their value in column H. Possibly a trailing space at the end of their code? If that's the case, change the code to this:
VBA Code:
Sub Copy_Dash_L()
    If Sheet1.AutoFilterMode Then Sheet1.AutoFilter.ShowAllData
    With Sheet1.Range("A1").CurrentRegion
        .AutoFilter 8, "*-L*"
        If .SpecialCells(xlCellTypeVisible).Address <> .Rows(1).Address Then
            .Offset(1).Resize(.Rows.Count - 1).Copy Sheet5.Cells(Rows.Count, 1).End(xlUp).Offset(1)
        End If
        .AutoFilter
    End With
End Sub
 
Upvote 0
Solution
If it missed 2 records then it must be something to do with their value in column H. Possibly a trailing space at the end of their code? If that's the case, change the code to this:
VBA Code:
Sub Copy_Dash_L()
    If Sheet1.AutoFilterMode Then Sheet1.AutoFilter.ShowAllData
    With Sheet1.Range("A1").CurrentRegion
        .AutoFilter 8, "*-L*"
        If .SpecialCells(xlCellTypeVisible).Address <> .Rows(1).Address Then
            .Offset(1).Resize(.Rows.Count - 1).Copy Sheet5.Cells(Rows.Count, 1).End(xlUp).Offset(1)
        End If
        .AutoFilter
    End With
End Sub
That worked. :) I've been banging my head against my monitor and a few YouTube channels trying to figure this out. You way is literally 20 times faster.

Thank you so much Kevin.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

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