VBA Loop combining Lastrow and finding blank values










1















I am trying to build a "data check" type file where a series of macros look at a data set and copy/paste incorrect entries into separate sheets based on various criteria. One of these is looking at if the value in Column A is blank.



Below is the code that I currently have. It only takes the first instance of the blank, and I am trying have it loop to find all the blank values in column A.



Sub copy_blanks() 
Dim sr As Range
Dim blank As Long
Dim i As Integer
Dim s1 As Worksheet
Dim s2 As Worksheet

Set s1 = Worksheets("data")
Set s2 = Worksheets("No LoadID")

lr2 = s2.Cells(Rows.Count, 2).End(xlUp).Row

Set sr = Worksheets("data").Range("A:A").Find("")

If Not sr Is Nothing Then
blank = sr.Row
s1.Rows(blank).Copy
s2.Cells(lr2 + 1, 1).PasteSpecial xlPasteValues
End If
End Sub









share|improve this question
























  • Do you want the whole row to be copied when you have a blank?

    – urdearboy
    Nov 13 '18 at 14:10











  • @urdearboy Yes I would like to bring over the whole row, is this still satisfied with the below?

    – jh144
    Nov 13 '18 at 14:41











  • Yup. Looks like all 3 solutions are grabbing the entire row

    – urdearboy
    Nov 13 '18 at 14:50











  • Thank you to everyone for your help! All the answers proved to be useful and I will be able to integrate methods from each into the remainder of the file. Thanks!

    – jh144
    Nov 13 '18 at 15:00















1















I am trying to build a "data check" type file where a series of macros look at a data set and copy/paste incorrect entries into separate sheets based on various criteria. One of these is looking at if the value in Column A is blank.



Below is the code that I currently have. It only takes the first instance of the blank, and I am trying have it loop to find all the blank values in column A.



Sub copy_blanks() 
Dim sr As Range
Dim blank As Long
Dim i As Integer
Dim s1 As Worksheet
Dim s2 As Worksheet

Set s1 = Worksheets("data")
Set s2 = Worksheets("No LoadID")

lr2 = s2.Cells(Rows.Count, 2).End(xlUp).Row

Set sr = Worksheets("data").Range("A:A").Find("")

If Not sr Is Nothing Then
blank = sr.Row
s1.Rows(blank).Copy
s2.Cells(lr2 + 1, 1).PasteSpecial xlPasteValues
End If
End Sub









share|improve this question
























  • Do you want the whole row to be copied when you have a blank?

    – urdearboy
    Nov 13 '18 at 14:10











  • @urdearboy Yes I would like to bring over the whole row, is this still satisfied with the below?

    – jh144
    Nov 13 '18 at 14:41











  • Yup. Looks like all 3 solutions are grabbing the entire row

    – urdearboy
    Nov 13 '18 at 14:50











  • Thank you to everyone for your help! All the answers proved to be useful and I will be able to integrate methods from each into the remainder of the file. Thanks!

    – jh144
    Nov 13 '18 at 15:00













1












1








1


1






I am trying to build a "data check" type file where a series of macros look at a data set and copy/paste incorrect entries into separate sheets based on various criteria. One of these is looking at if the value in Column A is blank.



Below is the code that I currently have. It only takes the first instance of the blank, and I am trying have it loop to find all the blank values in column A.



Sub copy_blanks() 
Dim sr As Range
Dim blank As Long
Dim i As Integer
Dim s1 As Worksheet
Dim s2 As Worksheet

Set s1 = Worksheets("data")
Set s2 = Worksheets("No LoadID")

lr2 = s2.Cells(Rows.Count, 2).End(xlUp).Row

Set sr = Worksheets("data").Range("A:A").Find("")

If Not sr Is Nothing Then
blank = sr.Row
s1.Rows(blank).Copy
s2.Cells(lr2 + 1, 1).PasteSpecial xlPasteValues
End If
End Sub









share|improve this question
















I am trying to build a "data check" type file where a series of macros look at a data set and copy/paste incorrect entries into separate sheets based on various criteria. One of these is looking at if the value in Column A is blank.



Below is the code that I currently have. It only takes the first instance of the blank, and I am trying have it loop to find all the blank values in column A.



Sub copy_blanks() 
Dim sr As Range
Dim blank As Long
Dim i As Integer
Dim s1 As Worksheet
Dim s2 As Worksheet

Set s1 = Worksheets("data")
Set s2 = Worksheets("No LoadID")

lr2 = s2.Cells(Rows.Count, 2).End(xlUp).Row

Set sr = Worksheets("data").Range("A:A").Find("")

If Not sr Is Nothing Then
blank = sr.Row
s1.Rows(blank).Copy
s2.Cells(lr2 + 1, 1).PasteSpecial xlPasteValues
End If
End Sub






excel vba excel-vba loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 15:00









Pᴇʜ

21k42650




21k42650










asked Nov 13 '18 at 14:06









jh144jh144

155




155












  • Do you want the whole row to be copied when you have a blank?

    – urdearboy
    Nov 13 '18 at 14:10











  • @urdearboy Yes I would like to bring over the whole row, is this still satisfied with the below?

    – jh144
    Nov 13 '18 at 14:41











  • Yup. Looks like all 3 solutions are grabbing the entire row

    – urdearboy
    Nov 13 '18 at 14:50











  • Thank you to everyone for your help! All the answers proved to be useful and I will be able to integrate methods from each into the remainder of the file. Thanks!

    – jh144
    Nov 13 '18 at 15:00

















  • Do you want the whole row to be copied when you have a blank?

    – urdearboy
    Nov 13 '18 at 14:10











  • @urdearboy Yes I would like to bring over the whole row, is this still satisfied with the below?

    – jh144
    Nov 13 '18 at 14:41











  • Yup. Looks like all 3 solutions are grabbing the entire row

    – urdearboy
    Nov 13 '18 at 14:50











  • Thank you to everyone for your help! All the answers proved to be useful and I will be able to integrate methods from each into the remainder of the file. Thanks!

    – jh144
    Nov 13 '18 at 15:00
















Do you want the whole row to be copied when you have a blank?

– urdearboy
Nov 13 '18 at 14:10





Do you want the whole row to be copied when you have a blank?

– urdearboy
Nov 13 '18 at 14:10













@urdearboy Yes I would like to bring over the whole row, is this still satisfied with the below?

– jh144
Nov 13 '18 at 14:41





@urdearboy Yes I would like to bring over the whole row, is this still satisfied with the below?

– jh144
Nov 13 '18 at 14:41













Yup. Looks like all 3 solutions are grabbing the entire row

– urdearboy
Nov 13 '18 at 14:50





Yup. Looks like all 3 solutions are grabbing the entire row

– urdearboy
Nov 13 '18 at 14:50













Thank you to everyone for your help! All the answers proved to be useful and I will be able to integrate methods from each into the remainder of the file. Thanks!

– jh144
Nov 13 '18 at 15:00





Thank you to everyone for your help! All the answers proved to be useful and I will be able to integrate methods from each into the remainder of the file. Thanks!

– jh144
Nov 13 '18 at 15:00












3 Answers
3






active

oldest

votes


















2














Have a look at the Range.SpecialCells Method.
You can use SpecialCells(xlCellTypeBlanks) to find all blank cells in a range.



Dim wsData As Worksheet
Set wsData = Worksheets("data")

Dim LastDataRow As Range
Set LastDataRow = wsData.Cells(Rows.Count, "A").End(xlUp) 'last used cell in column A

Dim wsNoID As Worksheet
Set wsNoID = Worksheets("No LoadID")

Dim BlankCells As Range
On Error Resume Next 'next line will throw an error if no blanks are found
Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) 'find all blank cells in column A until last data row
On Error Goto 0 're-activate error messages!

If Not BlankCells Is Nothing Then
BlankCells.EntireRow.Copy

wsNoID.Cells(Rows.Count, 2).End(xlUp).Offset(1, -1).PasteSpecial xlPasteValues
Else
MsgBox "No blanks found."
End If





share|improve this answer




















  • 2





    Was going to answer with the SpecialCells method - would need to add an error handler in case there's no blank cells.

    – Darren Bartrup-Cook
    Nov 13 '18 at 14:21











  • @DarrenBartrup-Cook Correct. Thank you for pointing out. Fixed it.

    – Pᴇʜ
    Nov 13 '18 at 14:29






  • 3





    Sorry to stick my oar in again - Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) would throw the error. Could be a rare case of On Error Resume Next.

    – Darren Bartrup-Cook
    Nov 13 '18 at 14:33






  • 1





    @DarrenBartrup-Cook My bad again :( I'm getting a coffee now. Thanks again!

    – Pᴇʜ
    Nov 13 '18 at 14:36


















2














I prefer using autofilter for this sort of work since that will capture cells that are blank as the result of formulas (and thus contain "" so they look blank) as blank as well as actually blank cells. Code assumes headers are row 1 and actual data starts in row 2:



Sub copy_blanks()

Dim s1 As Worksheet
Dim s2 As Worksheet
Dim lr2 As Long

Set s1 = ActiveWorkbook.Worksheets("data")
Set s2 = ActiveWorkbook.Worksheets("No LoadID")

lr2 = s2.Cells(s2.Rows.Count, 2).End(xlUp).Row

With s1.Range("A1:A" & s1.Cells(s1.Rows.Count, "B").End(xlUp).Row)
.AutoFilter 1, "="
.Offset(1).EntireRow.Copy
s2.Cells(lr2 + 1, "A").PasteSpecial xlPasteValues
Application.CutCopyMode = False
.AutoFilter
End With

End Sub





share|improve this answer






























    1














    You question implies you will have more criteria to search for so I decided to keep the loop. You can add more criteria here as you go ~



    1. Loop through Column A

    2. If value is blank add the cell to a Union (collection of cells)

    3. Once loop is complete, copy the Union all at once


    This can be improved upon by switching from a For i loop to a For Each loop to go through a range. Another way to do this is simply filter Column A by blanks and copy/paste the visible rows that remain.



    Option Explicit

    Sub Blanks()

    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("data")
    Dim db As Worksheet: Set db = ThisWorkbook.Sheets("No LoadID")

    Dim LROw As Long, i As Long, Blanks As Range

    For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    If ws.Range("A" & i) = "" Then
    If Not Blanks Is Nothing Then
    Set Blanks = Union(Blanks, ws.Range("A" & i))
    Else
    Set Blanks = ws.Range("A" & i)
    End If
    End If
    Next i

    If Not Blanks Is Nothing Then
    Blanks.EntireRow.Copy db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1).Row
    End If

    End Sub





    share|improve this answer

























    • db.Range("A" & db.Rows.Count).End(xlUp).Offset(1) must be db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1) because there is no data in column A if you copy the rows (only having a blank in A).

      – Pᴇʜ
      Nov 13 '18 at 14:33










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282818%2fvba-loop-combining-lastrow-and-finding-blank-values%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Have a look at the Range.SpecialCells Method.
    You can use SpecialCells(xlCellTypeBlanks) to find all blank cells in a range.



    Dim wsData As Worksheet
    Set wsData = Worksheets("data")

    Dim LastDataRow As Range
    Set LastDataRow = wsData.Cells(Rows.Count, "A").End(xlUp) 'last used cell in column A

    Dim wsNoID As Worksheet
    Set wsNoID = Worksheets("No LoadID")

    Dim BlankCells As Range
    On Error Resume Next 'next line will throw an error if no blanks are found
    Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) 'find all blank cells in column A until last data row
    On Error Goto 0 're-activate error messages!

    If Not BlankCells Is Nothing Then
    BlankCells.EntireRow.Copy

    wsNoID.Cells(Rows.Count, 2).End(xlUp).Offset(1, -1).PasteSpecial xlPasteValues
    Else
    MsgBox "No blanks found."
    End If





    share|improve this answer




















    • 2





      Was going to answer with the SpecialCells method - would need to add an error handler in case there's no blank cells.

      – Darren Bartrup-Cook
      Nov 13 '18 at 14:21











    • @DarrenBartrup-Cook Correct. Thank you for pointing out. Fixed it.

      – Pᴇʜ
      Nov 13 '18 at 14:29






    • 3





      Sorry to stick my oar in again - Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) would throw the error. Could be a rare case of On Error Resume Next.

      – Darren Bartrup-Cook
      Nov 13 '18 at 14:33






    • 1





      @DarrenBartrup-Cook My bad again :( I'm getting a coffee now. Thanks again!

      – Pᴇʜ
      Nov 13 '18 at 14:36















    2














    Have a look at the Range.SpecialCells Method.
    You can use SpecialCells(xlCellTypeBlanks) to find all blank cells in a range.



    Dim wsData As Worksheet
    Set wsData = Worksheets("data")

    Dim LastDataRow As Range
    Set LastDataRow = wsData.Cells(Rows.Count, "A").End(xlUp) 'last used cell in column A

    Dim wsNoID As Worksheet
    Set wsNoID = Worksheets("No LoadID")

    Dim BlankCells As Range
    On Error Resume Next 'next line will throw an error if no blanks are found
    Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) 'find all blank cells in column A until last data row
    On Error Goto 0 're-activate error messages!

    If Not BlankCells Is Nothing Then
    BlankCells.EntireRow.Copy

    wsNoID.Cells(Rows.Count, 2).End(xlUp).Offset(1, -1).PasteSpecial xlPasteValues
    Else
    MsgBox "No blanks found."
    End If





    share|improve this answer




















    • 2





      Was going to answer with the SpecialCells method - would need to add an error handler in case there's no blank cells.

      – Darren Bartrup-Cook
      Nov 13 '18 at 14:21











    • @DarrenBartrup-Cook Correct. Thank you for pointing out. Fixed it.

      – Pᴇʜ
      Nov 13 '18 at 14:29






    • 3





      Sorry to stick my oar in again - Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) would throw the error. Could be a rare case of On Error Resume Next.

      – Darren Bartrup-Cook
      Nov 13 '18 at 14:33






    • 1





      @DarrenBartrup-Cook My bad again :( I'm getting a coffee now. Thanks again!

      – Pᴇʜ
      Nov 13 '18 at 14:36













    2












    2








    2







    Have a look at the Range.SpecialCells Method.
    You can use SpecialCells(xlCellTypeBlanks) to find all blank cells in a range.



    Dim wsData As Worksheet
    Set wsData = Worksheets("data")

    Dim LastDataRow As Range
    Set LastDataRow = wsData.Cells(Rows.Count, "A").End(xlUp) 'last used cell in column A

    Dim wsNoID As Worksheet
    Set wsNoID = Worksheets("No LoadID")

    Dim BlankCells As Range
    On Error Resume Next 'next line will throw an error if no blanks are found
    Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) 'find all blank cells in column A until last data row
    On Error Goto 0 're-activate error messages!

    If Not BlankCells Is Nothing Then
    BlankCells.EntireRow.Copy

    wsNoID.Cells(Rows.Count, 2).End(xlUp).Offset(1, -1).PasteSpecial xlPasteValues
    Else
    MsgBox "No blanks found."
    End If





    share|improve this answer















    Have a look at the Range.SpecialCells Method.
    You can use SpecialCells(xlCellTypeBlanks) to find all blank cells in a range.



    Dim wsData As Worksheet
    Set wsData = Worksheets("data")

    Dim LastDataRow As Range
    Set LastDataRow = wsData.Cells(Rows.Count, "A").End(xlUp) 'last used cell in column A

    Dim wsNoID As Worksheet
    Set wsNoID = Worksheets("No LoadID")

    Dim BlankCells As Range
    On Error Resume Next 'next line will throw an error if no blanks are found
    Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) 'find all blank cells in column A until last data row
    On Error Goto 0 're-activate error messages!

    If Not BlankCells Is Nothing Then
    BlankCells.EntireRow.Copy

    wsNoID.Cells(Rows.Count, 2).End(xlUp).Offset(1, -1).PasteSpecial xlPasteValues
    Else
    MsgBox "No blanks found."
    End If






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 14:34

























    answered Nov 13 '18 at 14:14









    PᴇʜPᴇʜ

    21k42650




    21k42650







    • 2





      Was going to answer with the SpecialCells method - would need to add an error handler in case there's no blank cells.

      – Darren Bartrup-Cook
      Nov 13 '18 at 14:21











    • @DarrenBartrup-Cook Correct. Thank you for pointing out. Fixed it.

      – Pᴇʜ
      Nov 13 '18 at 14:29






    • 3





      Sorry to stick my oar in again - Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) would throw the error. Could be a rare case of On Error Resume Next.

      – Darren Bartrup-Cook
      Nov 13 '18 at 14:33






    • 1





      @DarrenBartrup-Cook My bad again :( I'm getting a coffee now. Thanks again!

      – Pᴇʜ
      Nov 13 '18 at 14:36












    • 2





      Was going to answer with the SpecialCells method - would need to add an error handler in case there's no blank cells.

      – Darren Bartrup-Cook
      Nov 13 '18 at 14:21











    • @DarrenBartrup-Cook Correct. Thank you for pointing out. Fixed it.

      – Pᴇʜ
      Nov 13 '18 at 14:29






    • 3





      Sorry to stick my oar in again - Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) would throw the error. Could be a rare case of On Error Resume Next.

      – Darren Bartrup-Cook
      Nov 13 '18 at 14:33






    • 1





      @DarrenBartrup-Cook My bad again :( I'm getting a coffee now. Thanks again!

      – Pᴇʜ
      Nov 13 '18 at 14:36







    2




    2





    Was going to answer with the SpecialCells method - would need to add an error handler in case there's no blank cells.

    – Darren Bartrup-Cook
    Nov 13 '18 at 14:21





    Was going to answer with the SpecialCells method - would need to add an error handler in case there's no blank cells.

    – Darren Bartrup-Cook
    Nov 13 '18 at 14:21













    @DarrenBartrup-Cook Correct. Thank you for pointing out. Fixed it.

    – Pᴇʜ
    Nov 13 '18 at 14:29





    @DarrenBartrup-Cook Correct. Thank you for pointing out. Fixed it.

    – Pᴇʜ
    Nov 13 '18 at 14:29




    3




    3





    Sorry to stick my oar in again - Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) would throw the error. Could be a rare case of On Error Resume Next.

    – Darren Bartrup-Cook
    Nov 13 '18 at 14:33





    Sorry to stick my oar in again - Set BlankCells = wsData.Range("A1", LastDataRow).SpecialCells(xlCellTypeBlanks) would throw the error. Could be a rare case of On Error Resume Next.

    – Darren Bartrup-Cook
    Nov 13 '18 at 14:33




    1




    1





    @DarrenBartrup-Cook My bad again :( I'm getting a coffee now. Thanks again!

    – Pᴇʜ
    Nov 13 '18 at 14:36





    @DarrenBartrup-Cook My bad again :( I'm getting a coffee now. Thanks again!

    – Pᴇʜ
    Nov 13 '18 at 14:36













    2














    I prefer using autofilter for this sort of work since that will capture cells that are blank as the result of formulas (and thus contain "" so they look blank) as blank as well as actually blank cells. Code assumes headers are row 1 and actual data starts in row 2:



    Sub copy_blanks()

    Dim s1 As Worksheet
    Dim s2 As Worksheet
    Dim lr2 As Long

    Set s1 = ActiveWorkbook.Worksheets("data")
    Set s2 = ActiveWorkbook.Worksheets("No LoadID")

    lr2 = s2.Cells(s2.Rows.Count, 2).End(xlUp).Row

    With s1.Range("A1:A" & s1.Cells(s1.Rows.Count, "B").End(xlUp).Row)
    .AutoFilter 1, "="
    .Offset(1).EntireRow.Copy
    s2.Cells(lr2 + 1, "A").PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    .AutoFilter
    End With

    End Sub





    share|improve this answer



























      2














      I prefer using autofilter for this sort of work since that will capture cells that are blank as the result of formulas (and thus contain "" so they look blank) as blank as well as actually blank cells. Code assumes headers are row 1 and actual data starts in row 2:



      Sub copy_blanks()

      Dim s1 As Worksheet
      Dim s2 As Worksheet
      Dim lr2 As Long

      Set s1 = ActiveWorkbook.Worksheets("data")
      Set s2 = ActiveWorkbook.Worksheets("No LoadID")

      lr2 = s2.Cells(s2.Rows.Count, 2).End(xlUp).Row

      With s1.Range("A1:A" & s1.Cells(s1.Rows.Count, "B").End(xlUp).Row)
      .AutoFilter 1, "="
      .Offset(1).EntireRow.Copy
      s2.Cells(lr2 + 1, "A").PasteSpecial xlPasteValues
      Application.CutCopyMode = False
      .AutoFilter
      End With

      End Sub





      share|improve this answer

























        2












        2








        2







        I prefer using autofilter for this sort of work since that will capture cells that are blank as the result of formulas (and thus contain "" so they look blank) as blank as well as actually blank cells. Code assumes headers are row 1 and actual data starts in row 2:



        Sub copy_blanks()

        Dim s1 As Worksheet
        Dim s2 As Worksheet
        Dim lr2 As Long

        Set s1 = ActiveWorkbook.Worksheets("data")
        Set s2 = ActiveWorkbook.Worksheets("No LoadID")

        lr2 = s2.Cells(s2.Rows.Count, 2).End(xlUp).Row

        With s1.Range("A1:A" & s1.Cells(s1.Rows.Count, "B").End(xlUp).Row)
        .AutoFilter 1, "="
        .Offset(1).EntireRow.Copy
        s2.Cells(lr2 + 1, "A").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        .AutoFilter
        End With

        End Sub





        share|improve this answer













        I prefer using autofilter for this sort of work since that will capture cells that are blank as the result of formulas (and thus contain "" so they look blank) as blank as well as actually blank cells. Code assumes headers are row 1 and actual data starts in row 2:



        Sub copy_blanks()

        Dim s1 As Worksheet
        Dim s2 As Worksheet
        Dim lr2 As Long

        Set s1 = ActiveWorkbook.Worksheets("data")
        Set s2 = ActiveWorkbook.Worksheets("No LoadID")

        lr2 = s2.Cells(s2.Rows.Count, 2).End(xlUp).Row

        With s1.Range("A1:A" & s1.Cells(s1.Rows.Count, "B").End(xlUp).Row)
        .AutoFilter 1, "="
        .Offset(1).EntireRow.Copy
        s2.Cells(lr2 + 1, "A").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        .AutoFilter
        End With

        End Sub






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 14:18









        tigeravatartigeravatar

        20.9k42234




        20.9k42234





















            1














            You question implies you will have more criteria to search for so I decided to keep the loop. You can add more criteria here as you go ~



            1. Loop through Column A

            2. If value is blank add the cell to a Union (collection of cells)

            3. Once loop is complete, copy the Union all at once


            This can be improved upon by switching from a For i loop to a For Each loop to go through a range. Another way to do this is simply filter Column A by blanks and copy/paste the visible rows that remain.



            Option Explicit

            Sub Blanks()

            Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("data")
            Dim db As Worksheet: Set db = ThisWorkbook.Sheets("No LoadID")

            Dim LROw As Long, i As Long, Blanks As Range

            For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
            If ws.Range("A" & i) = "" Then
            If Not Blanks Is Nothing Then
            Set Blanks = Union(Blanks, ws.Range("A" & i))
            Else
            Set Blanks = ws.Range("A" & i)
            End If
            End If
            Next i

            If Not Blanks Is Nothing Then
            Blanks.EntireRow.Copy db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1).Row
            End If

            End Sub





            share|improve this answer

























            • db.Range("A" & db.Rows.Count).End(xlUp).Offset(1) must be db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1) because there is no data in column A if you copy the rows (only having a blank in A).

              – Pᴇʜ
              Nov 13 '18 at 14:33















            1














            You question implies you will have more criteria to search for so I decided to keep the loop. You can add more criteria here as you go ~



            1. Loop through Column A

            2. If value is blank add the cell to a Union (collection of cells)

            3. Once loop is complete, copy the Union all at once


            This can be improved upon by switching from a For i loop to a For Each loop to go through a range. Another way to do this is simply filter Column A by blanks and copy/paste the visible rows that remain.



            Option Explicit

            Sub Blanks()

            Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("data")
            Dim db As Worksheet: Set db = ThisWorkbook.Sheets("No LoadID")

            Dim LROw As Long, i As Long, Blanks As Range

            For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
            If ws.Range("A" & i) = "" Then
            If Not Blanks Is Nothing Then
            Set Blanks = Union(Blanks, ws.Range("A" & i))
            Else
            Set Blanks = ws.Range("A" & i)
            End If
            End If
            Next i

            If Not Blanks Is Nothing Then
            Blanks.EntireRow.Copy db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1).Row
            End If

            End Sub





            share|improve this answer

























            • db.Range("A" & db.Rows.Count).End(xlUp).Offset(1) must be db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1) because there is no data in column A if you copy the rows (only having a blank in A).

              – Pᴇʜ
              Nov 13 '18 at 14:33













            1












            1








            1







            You question implies you will have more criteria to search for so I decided to keep the loop. You can add more criteria here as you go ~



            1. Loop through Column A

            2. If value is blank add the cell to a Union (collection of cells)

            3. Once loop is complete, copy the Union all at once


            This can be improved upon by switching from a For i loop to a For Each loop to go through a range. Another way to do this is simply filter Column A by blanks and copy/paste the visible rows that remain.



            Option Explicit

            Sub Blanks()

            Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("data")
            Dim db As Worksheet: Set db = ThisWorkbook.Sheets("No LoadID")

            Dim LROw As Long, i As Long, Blanks As Range

            For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
            If ws.Range("A" & i) = "" Then
            If Not Blanks Is Nothing Then
            Set Blanks = Union(Blanks, ws.Range("A" & i))
            Else
            Set Blanks = ws.Range("A" & i)
            End If
            End If
            Next i

            If Not Blanks Is Nothing Then
            Blanks.EntireRow.Copy db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1).Row
            End If

            End Sub





            share|improve this answer















            You question implies you will have more criteria to search for so I decided to keep the loop. You can add more criteria here as you go ~



            1. Loop through Column A

            2. If value is blank add the cell to a Union (collection of cells)

            3. Once loop is complete, copy the Union all at once


            This can be improved upon by switching from a For i loop to a For Each loop to go through a range. Another way to do this is simply filter Column A by blanks and copy/paste the visible rows that remain.



            Option Explicit

            Sub Blanks()

            Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("data")
            Dim db As Worksheet: Set db = ThisWorkbook.Sheets("No LoadID")

            Dim LROw As Long, i As Long, Blanks As Range

            For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
            If ws.Range("A" & i) = "" Then
            If Not Blanks Is Nothing Then
            Set Blanks = Union(Blanks, ws.Range("A" & i))
            Else
            Set Blanks = ws.Range("A" & i)
            End If
            End If
            Next i

            If Not Blanks Is Nothing Then
            Blanks.EntireRow.Copy db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1).Row
            End If

            End Sub






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 14:49

























            answered Nov 13 '18 at 14:17









            urdearboyurdearboy

            6,3613726




            6,3613726












            • db.Range("A" & db.Rows.Count).End(xlUp).Offset(1) must be db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1) because there is no data in column A if you copy the rows (only having a blank in A).

              – Pᴇʜ
              Nov 13 '18 at 14:33

















            • db.Range("A" & db.Rows.Count).End(xlUp).Offset(1) must be db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1) because there is no data in column A if you copy the rows (only having a blank in A).

              – Pᴇʜ
              Nov 13 '18 at 14:33
















            db.Range("A" & db.Rows.Count).End(xlUp).Offset(1) must be db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1) because there is no data in column A if you copy the rows (only having a blank in A).

            – Pᴇʜ
            Nov 13 '18 at 14:33





            db.Range("A" & db.Rows.Count).End(xlUp).Offset(1) must be db.Range("B" & db.Rows.Count).End(xlUp).Offset(1, -1) because there is no data in column A if you copy the rows (only having a blank in A).

            – Pᴇʜ
            Nov 13 '18 at 14:33

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282818%2fvba-loop-combining-lastrow-and-finding-blank-values%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3