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

excel vba excel-vba
|
show 2 more comments
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

excel vba excel-vba
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 ExplicitOption ExplicitOption 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 workis 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
|
show 2 more comments
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

excel vba excel-vba
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

excel vba excel-vba
excel vba excel-vba
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 ExplicitOption ExplicitOption 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 workis 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
|
show 2 more comments
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 ExplicitOption ExplicitOption 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 workis 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
|
show 2 more comments
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


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
add a comment |
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
add a comment |
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
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
add a comment |
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


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
add a comment |
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


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
add a comment |
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


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


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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 19 at 18:24
TinMan
1,929211
1,929211
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 ExplicitOption ExplicitOption 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 workis 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