add a line of code using zn if statement or between

daniels012

Well-known Member
Joined
Jan 13, 2005
Messages
5,219
How can I write code to copy data from one sheet to another if the dates in a certain cell are between the begining date (cell F2) and Ending date (cell F3)

Thank you,
Michael
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
How's this for a start:

<font face=tahoma><SPAN style="color:#00007F">Sub</SPAN> DateRange()
    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Date</SPAN> >= [F2] And <SPAN style="color:#00007F">Date</SPAN> <= [F3] <SPAN style="color:#00007F">Then</SPAN>
        MsgBox <SPAN style="color:#00007F">Date</SPAN> & " is between your acceptable date range"
    Else: MsgBox Date & " is not between your acceptable date range"
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

Just replace "Date" with your qualifier.

Hope that helps,

Smitty
 
Upvote 0
Here is what I have:

Dim c As Range, ws As Worksheet

Set ws = Worksheets("Running Total")

For Each c In ws.Range("K:K")
If c.Value >= [G2] And c.Value <= [G3] Then
c.EntireRow.Copy
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Select
End If

I need to copy the data from a defined name "propdata" in the Running Total worksheet and paste it into the new sheet.
Does my code above do this? It seems like it is just going to copy wherever it is at the moment???

Michael
 
Upvote 0
You are asking us if the code works?

What happens when you run it?

Code:
Dim c As Range, ws As Worksheet
Dim x as Date
Set ws = Worksheets("Running Total")

For Each c In ws.Range("K:K")
x= Format(c.Value,"mm/dd/yyyy")

If x >= [G2] And x <= [G3] Then
c.EntireRow.Copy
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Select
End If

I would format the comparison to the same comparison as your G2 and G3 cells, and declare the variable passed as a DATE.

But you have not indicated if your code works or not, only if you want our opinion as to it's working or not.
 
Upvote 0
I see you are struggling with two separate threads on the same topic.

I have PM'd you with my email address...you can send the file if you like.
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,416
Members
448,960
Latest member
AKSMITH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top