pull rows using VBA if condition met- My code working for = condition but not for >











up vote
-1
down vote

favorite












I am using below VBA code to fetch all rows if date is greater than 10/31/2013 but my code is not working. My code is working fine if i am usine date = 10/31/2013. Can anyone help me why my code is not working for greater than 10/31/2013 condition.My date column also has null values.



Private Sub CommandButton21_Click()

a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To a

If Worksheets("Sheet1").Cells(i, 7).Value > "10/31/2013" Then

Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet2").Activate
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate

End If

Next

Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select

End Sub


Here is the snapshot of data










share|improve this question
























  • We don't have any of your data. How are we supposed to answer this?
    – Ken White
    Nov 19 at 18:06






  • 4




    If you have dates formatted as dates on your sheet, then you need to use dates in your code, not strings.
    – Tim Williams
    Nov 19 at 18:07






  • 2




    Option Explicit Option Explicit Option Explicit. Please add that to the top of your code. You have undeclared variables and this will show you exactly where
    – urdearboy
    Nov 19 at 18:08






  • 1




    Yes to all of the above, but the reason your > is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this: If Worksheets("Sheet1").Cells(i, 7).Value > #10/31/2013# Then...
    – ArcherBird
    Nov 19 at 18:14






  • 3




    Did not work is not a useful explanation when troubleshooting. Did your computer spontaneously combust? Did you get an error? Did nothing happen at all? Did the wrong thing get copied?
    – urdearboy
    Nov 19 at 18:20

















up vote
-1
down vote

favorite












I am using below VBA code to fetch all rows if date is greater than 10/31/2013 but my code is not working. My code is working fine if i am usine date = 10/31/2013. Can anyone help me why my code is not working for greater than 10/31/2013 condition.My date column also has null values.



Private Sub CommandButton21_Click()

a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To a

If Worksheets("Sheet1").Cells(i, 7).Value > "10/31/2013" Then

Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet2").Activate
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate

End If

Next

Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select

End Sub


Here is the snapshot of data










share|improve this question
























  • We don't have any of your data. How are we supposed to answer this?
    – Ken White
    Nov 19 at 18:06






  • 4




    If you have dates formatted as dates on your sheet, then you need to use dates in your code, not strings.
    – Tim Williams
    Nov 19 at 18:07






  • 2




    Option Explicit Option Explicit Option Explicit. Please add that to the top of your code. You have undeclared variables and this will show you exactly where
    – urdearboy
    Nov 19 at 18:08






  • 1




    Yes to all of the above, but the reason your > is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this: If Worksheets("Sheet1").Cells(i, 7).Value > #10/31/2013# Then...
    – ArcherBird
    Nov 19 at 18:14






  • 3




    Did not work is not a useful explanation when troubleshooting. Did your computer spontaneously combust? Did you get an error? Did nothing happen at all? Did the wrong thing get copied?
    – urdearboy
    Nov 19 at 18:20















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am using below VBA code to fetch all rows if date is greater than 10/31/2013 but my code is not working. My code is working fine if i am usine date = 10/31/2013. Can anyone help me why my code is not working for greater than 10/31/2013 condition.My date column also has null values.



Private Sub CommandButton21_Click()

a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To a

If Worksheets("Sheet1").Cells(i, 7).Value > "10/31/2013" Then

Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet2").Activate
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate

End If

Next

Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select

End Sub


Here is the snapshot of data










share|improve this question















I am using below VBA code to fetch all rows if date is greater than 10/31/2013 but my code is not working. My code is working fine if i am usine date = 10/31/2013. Can anyone help me why my code is not working for greater than 10/31/2013 condition.My date column also has null values.



Private Sub CommandButton21_Click()

a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To a

If Worksheets("Sheet1").Cells(i, 7).Value > "10/31/2013" Then

Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet2").Activate
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate

End If

Next

Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select

End Sub


Here is the snapshot of data







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 18:46









TylerH

15.3k105067




15.3k105067










asked Nov 19 at 18:03









DMani

96




96












  • We don't have any of your data. How are we supposed to answer this?
    – Ken White
    Nov 19 at 18:06






  • 4




    If you have dates formatted as dates on your sheet, then you need to use dates in your code, not strings.
    – Tim Williams
    Nov 19 at 18:07






  • 2




    Option Explicit Option Explicit Option Explicit. Please add that to the top of your code. You have undeclared variables and this will show you exactly where
    – urdearboy
    Nov 19 at 18:08






  • 1




    Yes to all of the above, but the reason your > is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this: If Worksheets("Sheet1").Cells(i, 7).Value > #10/31/2013# Then...
    – ArcherBird
    Nov 19 at 18:14






  • 3




    Did not work is not a useful explanation when troubleshooting. Did your computer spontaneously combust? Did you get an error? Did nothing happen at all? Did the wrong thing get copied?
    – urdearboy
    Nov 19 at 18:20




















  • We don't have any of your data. How are we supposed to answer this?
    – Ken White
    Nov 19 at 18:06






  • 4




    If you have dates formatted as dates on your sheet, then you need to use dates in your code, not strings.
    – Tim Williams
    Nov 19 at 18:07






  • 2




    Option Explicit Option Explicit Option Explicit. Please add that to the top of your code. You have undeclared variables and this will show you exactly where
    – urdearboy
    Nov 19 at 18:08






  • 1




    Yes to all of the above, but the reason your > is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this: If Worksheets("Sheet1").Cells(i, 7).Value > #10/31/2013# Then...
    – ArcherBird
    Nov 19 at 18:14






  • 3




    Did not work is not a useful explanation when troubleshooting. Did your computer spontaneously combust? Did you get an error? Did nothing happen at all? Did the wrong thing get copied?
    – urdearboy
    Nov 19 at 18:20


















We don't have any of your data. How are we supposed to answer this?
– Ken White
Nov 19 at 18:06




We don't have any of your data. How are we supposed to answer this?
– Ken White
Nov 19 at 18:06




4




4




If you have dates formatted as dates on your sheet, then you need to use dates in your code, not strings.
– Tim Williams
Nov 19 at 18:07




If you have dates formatted as dates on your sheet, then you need to use dates in your code, not strings.
– Tim Williams
Nov 19 at 18:07




2




2




Option Explicit Option Explicit Option Explicit. Please add that to the top of your code. You have undeclared variables and this will show you exactly where
– urdearboy
Nov 19 at 18:08




Option Explicit Option Explicit Option Explicit. Please add that to the top of your code. You have undeclared variables and this will show you exactly where
– urdearboy
Nov 19 at 18:08




1




1




Yes to all of the above, but the reason your > is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this: If Worksheets("Sheet1").Cells(i, 7).Value > #10/31/2013# Then...
– ArcherBird
Nov 19 at 18:14




Yes to all of the above, but the reason your > is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this: If Worksheets("Sheet1").Cells(i, 7).Value > #10/31/2013# Then...
– ArcherBird
Nov 19 at 18:14




3




3




Did not work is not a useful explanation when troubleshooting. Did your computer spontaneously combust? Did you get an error? Did nothing happen at all? Did the wrong thing get copied?
– urdearboy
Nov 19 at 18:20






Did not work is not a useful explanation when troubleshooting. Did your computer spontaneously combust? Did you get an error? Did nothing happen at all? Did the wrong thing get copied?
– urdearboy
Nov 19 at 18:20














3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










You are not properly qualifying your objects with a sheet which could very well be the issue. I adapted your code to properly qualify all objects & this will also be much faster since it will only copy/paste outside of the loop once.



For instance, say you have 500 rows that meet your criteria ( Range > Date). This means you will have 500 instances of rows being copied and pasted over and over inside your loop. The below method will only have one instance of copy/paste and is independent of how many rows meet your criteria. The more rows to be copied, the more you will benefit from this solution.



Another possible solution is to just simply filter by your criteria and copy/paste the visible cells only





Updated for more criteria added in comments - tested and working fine on my end



Option Explicit

Private Sub CommandButton21_Click()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim i As Long, MyUnion As Range, LRow As Long

For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
If ws.Range("G" & i) > #10/31/2013# Or ws.Range("AA" & i) = "Investigate" Or ws.Range("AA" & i) = "Leave Open" Then
If Not MyUnion Is Nothing Then
Set MyUnion = Union(MyUnion, ws.Range("G" & i))
Else
Set MyUnion = ws.Range("G" & i)
End If
End If
Next i

If Not MyUnion Is Nothing Then
With ThisWorkbook.Sheets("Sheet2")
LRow = .Range("A" & .Rows.Count).End(xlUp).Offset(1).Row
MyUnion.EntireRow.Copy .Range("A" & LRow)
End With
End If

End Sub




Before & After



enter image description here



enter image description here






share|improve this answer























  • I also want to insert few columns in the beginning in Sheet2 when Macro copy data from Sheet 1 based on criteria that you helped me last time as mentioned in the above code.
    – DMani
    Nov 30 at 17:18












  • That is off topic here. If questions were allowed to continuously evolve, this site would never have a final answer. You need to post a new question as this is not what the original question was
    – urdearboy
    Nov 30 at 17:20


















up vote
1
down vote













Dates are numeric values. Use # instead of " when working with dates.



Selecting or activating ranges should be avoided watch Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset).



Private Sub CommandButton21_Click()
Application.ScreenUpdating = False
Dim r As Long

With ThisWorkbook.Worksheets("Sheet1")
For r = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
If .Cells(r, 7).Value > #10/31/2013# Then
.Rows(r).Copy Destination:=Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1)
End If
Next
End With

Application.CutCopyMode = False

End Sub





share|improve this answer




























    up vote
    0
    down vote













    The reason your > comparison is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this:



    If CDate(Worksheets("Sheet1").Cells(i, 7).Value) > #10/31/2013# Then


    Note that I've also ensured the value in your cell is converted to a Date datatype by using CDate



    Some additional comments, not related directly to your question:



    You use .select and .activate in your code, but it is a much better practice to interact with the objects directly. For example, that whole If Block should be more like this:



    If CDate(Worksheets("Sheet1").Cells(i, 7).Value) > #10/31/2013# Then
    Worksheets("Sheet1").Rows(i).Copy
    b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
    Worksheets("Sheet2").Cells(b + 1, 1).Paste
    End If





    share|improve this answer























    • I did convert to date column as date data type but after using your code i am getting no result in sheet2. I also added a data snapshot for the reference in my post.
      – DMani
      Nov 19 at 18:27











    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',
    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%2f53380307%2fpull-rows-using-vba-if-condition-met-my-code-working-for-condition-but-not-fo%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








    up vote
    1
    down vote



    accepted










    You are not properly qualifying your objects with a sheet which could very well be the issue. I adapted your code to properly qualify all objects & this will also be much faster since it will only copy/paste outside of the loop once.



    For instance, say you have 500 rows that meet your criteria ( Range > Date). This means you will have 500 instances of rows being copied and pasted over and over inside your loop. The below method will only have one instance of copy/paste and is independent of how many rows meet your criteria. The more rows to be copied, the more you will benefit from this solution.



    Another possible solution is to just simply filter by your criteria and copy/paste the visible cells only





    Updated for more criteria added in comments - tested and working fine on my end



    Option Explicit

    Private Sub CommandButton21_Click()

    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
    Dim i As Long, MyUnion As Range, LRow As Long

    For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    If ws.Range("G" & i) > #10/31/2013# Or ws.Range("AA" & i) = "Investigate" Or ws.Range("AA" & i) = "Leave Open" Then
    If Not MyUnion Is Nothing Then
    Set MyUnion = Union(MyUnion, ws.Range("G" & i))
    Else
    Set MyUnion = ws.Range("G" & i)
    End If
    End If
    Next i

    If Not MyUnion Is Nothing Then
    With ThisWorkbook.Sheets("Sheet2")
    LRow = .Range("A" & .Rows.Count).End(xlUp).Offset(1).Row
    MyUnion.EntireRow.Copy .Range("A" & LRow)
    End With
    End If

    End Sub




    Before & After



    enter image description here



    enter image description here






    share|improve this answer























    • I also want to insert few columns in the beginning in Sheet2 when Macro copy data from Sheet 1 based on criteria that you helped me last time as mentioned in the above code.
      – DMani
      Nov 30 at 17:18












    • That is off topic here. If questions were allowed to continuously evolve, this site would never have a final answer. You need to post a new question as this is not what the original question was
      – urdearboy
      Nov 30 at 17:20















    up vote
    1
    down vote



    accepted










    You are not properly qualifying your objects with a sheet which could very well be the issue. I adapted your code to properly qualify all objects & this will also be much faster since it will only copy/paste outside of the loop once.



    For instance, say you have 500 rows that meet your criteria ( Range > Date). This means you will have 500 instances of rows being copied and pasted over and over inside your loop. The below method will only have one instance of copy/paste and is independent of how many rows meet your criteria. The more rows to be copied, the more you will benefit from this solution.



    Another possible solution is to just simply filter by your criteria and copy/paste the visible cells only





    Updated for more criteria added in comments - tested and working fine on my end



    Option Explicit

    Private Sub CommandButton21_Click()

    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
    Dim i As Long, MyUnion As Range, LRow As Long

    For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    If ws.Range("G" & i) > #10/31/2013# Or ws.Range("AA" & i) = "Investigate" Or ws.Range("AA" & i) = "Leave Open" Then
    If Not MyUnion Is Nothing Then
    Set MyUnion = Union(MyUnion, ws.Range("G" & i))
    Else
    Set MyUnion = ws.Range("G" & i)
    End If
    End If
    Next i

    If Not MyUnion Is Nothing Then
    With ThisWorkbook.Sheets("Sheet2")
    LRow = .Range("A" & .Rows.Count).End(xlUp).Offset(1).Row
    MyUnion.EntireRow.Copy .Range("A" & LRow)
    End With
    End If

    End Sub




    Before & After



    enter image description here



    enter image description here






    share|improve this answer























    • I also want to insert few columns in the beginning in Sheet2 when Macro copy data from Sheet 1 based on criteria that you helped me last time as mentioned in the above code.
      – DMani
      Nov 30 at 17:18












    • That is off topic here. If questions were allowed to continuously evolve, this site would never have a final answer. You need to post a new question as this is not what the original question was
      – urdearboy
      Nov 30 at 17:20













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    You are not properly qualifying your objects with a sheet which could very well be the issue. I adapted your code to properly qualify all objects & this will also be much faster since it will only copy/paste outside of the loop once.



    For instance, say you have 500 rows that meet your criteria ( Range > Date). This means you will have 500 instances of rows being copied and pasted over and over inside your loop. The below method will only have one instance of copy/paste and is independent of how many rows meet your criteria. The more rows to be copied, the more you will benefit from this solution.



    Another possible solution is to just simply filter by your criteria and copy/paste the visible cells only





    Updated for more criteria added in comments - tested and working fine on my end



    Option Explicit

    Private Sub CommandButton21_Click()

    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
    Dim i As Long, MyUnion As Range, LRow As Long

    For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    If ws.Range("G" & i) > #10/31/2013# Or ws.Range("AA" & i) = "Investigate" Or ws.Range("AA" & i) = "Leave Open" Then
    If Not MyUnion Is Nothing Then
    Set MyUnion = Union(MyUnion, ws.Range("G" & i))
    Else
    Set MyUnion = ws.Range("G" & i)
    End If
    End If
    Next i

    If Not MyUnion Is Nothing Then
    With ThisWorkbook.Sheets("Sheet2")
    LRow = .Range("A" & .Rows.Count).End(xlUp).Offset(1).Row
    MyUnion.EntireRow.Copy .Range("A" & LRow)
    End With
    End If

    End Sub




    Before & After



    enter image description here



    enter image description here






    share|improve this answer














    You are not properly qualifying your objects with a sheet which could very well be the issue. I adapted your code to properly qualify all objects & this will also be much faster since it will only copy/paste outside of the loop once.



    For instance, say you have 500 rows that meet your criteria ( Range > Date). This means you will have 500 instances of rows being copied and pasted over and over inside your loop. The below method will only have one instance of copy/paste and is independent of how many rows meet your criteria. The more rows to be copied, the more you will benefit from this solution.



    Another possible solution is to just simply filter by your criteria and copy/paste the visible cells only





    Updated for more criteria added in comments - tested and working fine on my end



    Option Explicit

    Private Sub CommandButton21_Click()

    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
    Dim i As Long, MyUnion As Range, LRow As Long

    For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    If ws.Range("G" & i) > #10/31/2013# Or ws.Range("AA" & i) = "Investigate" Or ws.Range("AA" & i) = "Leave Open" Then
    If Not MyUnion Is Nothing Then
    Set MyUnion = Union(MyUnion, ws.Range("G" & i))
    Else
    Set MyUnion = ws.Range("G" & i)
    End If
    End If
    Next i

    If Not MyUnion Is Nothing Then
    With ThisWorkbook.Sheets("Sheet2")
    LRow = .Range("A" & .Rows.Count).End(xlUp).Offset(1).Row
    MyUnion.EntireRow.Copy .Range("A" & LRow)
    End With
    End If

    End Sub




    Before & After



    enter image description here



    enter image description here







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 at 20:30

























    answered Nov 19 at 18:29









    urdearboy

    5,7132625




    5,7132625












    • I also want to insert few columns in the beginning in Sheet2 when Macro copy data from Sheet 1 based on criteria that you helped me last time as mentioned in the above code.
      – DMani
      Nov 30 at 17:18












    • That is off topic here. If questions were allowed to continuously evolve, this site would never have a final answer. You need to post a new question as this is not what the original question was
      – urdearboy
      Nov 30 at 17:20


















    • I also want to insert few columns in the beginning in Sheet2 when Macro copy data from Sheet 1 based on criteria that you helped me last time as mentioned in the above code.
      – DMani
      Nov 30 at 17:18












    • That is off topic here. If questions were allowed to continuously evolve, this site would never have a final answer. You need to post a new question as this is not what the original question was
      – urdearboy
      Nov 30 at 17:20
















    I also want to insert few columns in the beginning in Sheet2 when Macro copy data from Sheet 1 based on criteria that you helped me last time as mentioned in the above code.
    – DMani
    Nov 30 at 17:18






    I also want to insert few columns in the beginning in Sheet2 when Macro copy data from Sheet 1 based on criteria that you helped me last time as mentioned in the above code.
    – DMani
    Nov 30 at 17:18














    That is off topic here. If questions were allowed to continuously evolve, this site would never have a final answer. You need to post a new question as this is not what the original question was
    – urdearboy
    Nov 30 at 17:20




    That is off topic here. If questions were allowed to continuously evolve, this site would never have a final answer. You need to post a new question as this is not what the original question was
    – urdearboy
    Nov 30 at 17:20












    up vote
    1
    down vote













    Dates are numeric values. Use # instead of " when working with dates.



    Selecting or activating ranges should be avoided watch Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset).



    Private Sub CommandButton21_Click()
    Application.ScreenUpdating = False
    Dim r As Long

    With ThisWorkbook.Worksheets("Sheet1")
    For r = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
    If .Cells(r, 7).Value > #10/31/2013# Then
    .Rows(r).Copy Destination:=Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1)
    End If
    Next
    End With

    Application.CutCopyMode = False

    End Sub





    share|improve this answer

























      up vote
      1
      down vote













      Dates are numeric values. Use # instead of " when working with dates.



      Selecting or activating ranges should be avoided watch Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset).



      Private Sub CommandButton21_Click()
      Application.ScreenUpdating = False
      Dim r As Long

      With ThisWorkbook.Worksheets("Sheet1")
      For r = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
      If .Cells(r, 7).Value > #10/31/2013# Then
      .Rows(r).Copy Destination:=Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1)
      End If
      Next
      End With

      Application.CutCopyMode = False

      End Sub





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Dates are numeric values. Use # instead of " when working with dates.



        Selecting or activating ranges should be avoided watch Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset).



        Private Sub CommandButton21_Click()
        Application.ScreenUpdating = False
        Dim r As Long

        With ThisWorkbook.Worksheets("Sheet1")
        For r = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
        If .Cells(r, 7).Value > #10/31/2013# Then
        .Rows(r).Copy Destination:=Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        End If
        Next
        End With

        Application.CutCopyMode = False

        End Sub





        share|improve this answer












        Dates are numeric values. Use # instead of " when working with dates.



        Selecting or activating ranges should be avoided watch Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset).



        Private Sub CommandButton21_Click()
        Application.ScreenUpdating = False
        Dim r As Long

        With ThisWorkbook.Worksheets("Sheet1")
        For r = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
        If .Cells(r, 7).Value > #10/31/2013# Then
        .Rows(r).Copy Destination:=Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        End If
        Next
        End With

        Application.CutCopyMode = False

        End Sub






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 18:24









        TinMan

        1,929211




        1,929211






















            up vote
            0
            down vote













            The reason your > comparison is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this:



            If CDate(Worksheets("Sheet1").Cells(i, 7).Value) > #10/31/2013# Then


            Note that I've also ensured the value in your cell is converted to a Date datatype by using CDate



            Some additional comments, not related directly to your question:



            You use .select and .activate in your code, but it is a much better practice to interact with the objects directly. For example, that whole If Block should be more like this:



            If CDate(Worksheets("Sheet1").Cells(i, 7).Value) > #10/31/2013# Then
            Worksheets("Sheet1").Rows(i).Copy
            b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
            Worksheets("Sheet2").Cells(b + 1, 1).Paste
            End If





            share|improve this answer























            • I did convert to date column as date data type but after using your code i am getting no result in sheet2. I also added a data snapshot for the reference in my post.
              – DMani
              Nov 19 at 18:27















            up vote
            0
            down vote













            The reason your > comparison is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this:



            If CDate(Worksheets("Sheet1").Cells(i, 7).Value) > #10/31/2013# Then


            Note that I've also ensured the value in your cell is converted to a Date datatype by using CDate



            Some additional comments, not related directly to your question:



            You use .select and .activate in your code, but it is a much better practice to interact with the objects directly. For example, that whole If Block should be more like this:



            If CDate(Worksheets("Sheet1").Cells(i, 7).Value) > #10/31/2013# Then
            Worksheets("Sheet1").Rows(i).Copy
            b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
            Worksheets("Sheet2").Cells(b + 1, 1).Paste
            End If





            share|improve this answer























            • I did convert to date column as date data type but after using your code i am getting no result in sheet2. I also added a data snapshot for the reference in my post.
              – DMani
              Nov 19 at 18:27













            up vote
            0
            down vote










            up vote
            0
            down vote









            The reason your > comparison is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this:



            If CDate(Worksheets("Sheet1").Cells(i, 7).Value) > #10/31/2013# Then


            Note that I've also ensured the value in your cell is converted to a Date datatype by using CDate



            Some additional comments, not related directly to your question:



            You use .select and .activate in your code, but it is a much better practice to interact with the objects directly. For example, that whole If Block should be more like this:



            If CDate(Worksheets("Sheet1").Cells(i, 7).Value) > #10/31/2013# Then
            Worksheets("Sheet1").Rows(i).Copy
            b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
            Worksheets("Sheet2").Cells(b + 1, 1).Paste
            End If





            share|improve this answer














            The reason your > comparison is not working as you would expect is because you have provided a string (wrapped in ") in your comparison. If you want to compare dates, provide a date wrapped in # like this:



            If CDate(Worksheets("Sheet1").Cells(i, 7).Value) > #10/31/2013# Then


            Note that I've also ensured the value in your cell is converted to a Date datatype by using CDate



            Some additional comments, not related directly to your question:



            You use .select and .activate in your code, but it is a much better practice to interact with the objects directly. For example, that whole If Block should be more like this:



            If CDate(Worksheets("Sheet1").Cells(i, 7).Value) > #10/31/2013# Then
            Worksheets("Sheet1").Rows(i).Copy
            b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
            Worksheets("Sheet2").Cells(b + 1, 1).Paste
            End If






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 at 18:25

























            answered Nov 19 at 18:15









            ArcherBird

            758219




            758219












            • I did convert to date column as date data type but after using your code i am getting no result in sheet2. I also added a data snapshot for the reference in my post.
              – DMani
              Nov 19 at 18:27


















            • I did convert to date column as date data type but after using your code i am getting no result in sheet2. I also added a data snapshot for the reference in my post.
              – DMani
              Nov 19 at 18:27
















            I did convert to date column as date data type but after using your code i am getting no result in sheet2. I also added a data snapshot for the reference in my post.
            – DMani
            Nov 19 at 18:27




            I did convert to date column as date data type but after using your code i am getting no result in sheet2. I also added a data snapshot for the reference in my post.
            – DMani
            Nov 19 at 18:27


















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53380307%2fpull-rows-using-vba-if-condition-met-my-code-working-for-condition-but-not-fo%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







            Popular posts from this blog

            Ottavio Pratesi

            Tricia Helfer

            15 giugno