my code doesn't work after add message box

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
I have this code brings data from sheet to another between two dates , but some times I writing wrong date in l2,m2 may be from date > to date and may not be the date in l2 and m2 so I add message to show the wrong because it gives me error in code whether I write the right date or wrong it shows always the message
this is my code I disabled the message in the code to understand what I want
VBA Code:
Sub CopyDataUsingDateRange()

    Application.ScreenUpdating = False

    Dim wsData As Worksheet, wsDate As Worksheet, 
    Dim dSDate As Variant, dEDate As Variant
    Dim lRowStart As Long, lRowEnd As Long
    Dim aData() As Variant
    Dim i As Long


    'set the worksheet objects
    Set wsData = ThisWorkbook.Sheets("process")
    Set wsDate = ThisWorkbook.Sheets("data")
 
    'required variables
    dSDate = wsDate.Range("l2").Value
    dEDate = wsDate.Range("m2").Value

    'set the array - you can make this dynamic!
    aData = wsData.Range("A1:h1000").Value

    'for loop to find start
    For i = 2 To 1000
     'If dSDate > dEDate Or dSDate <> Format(Date, "mm/dd/yyyy") Or dEDate <> Format(Date, "mm/dd/yyyy") Then
    ' MsgBox "the first date can't be more than end date", vbInformation
     ' Exit Sub
    ' End If
    
    
        If aData(i, 1) = dSDate Then
            lRowStart = i
            Debug.Print "Start row = " & lRowStart
            Exit For
        End If
    Next i

    'now loop backwards to find end date
    For i = 1000 To 2 Step -1
        If aData(i, 1) = dEDate Then
            lRowEnd = i
            Debug.Print "End row = " & lRowEnd
            Exit For
        End If
    Next i

    'now we have start and end dates
    'going to use copy/ paste for simplicity
    wsData.Range("A" & lRowStart, "h" & lRowEnd).Copy
    'paste in date sheet
    wsDate.Range("A2").PasteSpecial Paste:=xlPasteValues
    'clear clipboard
    Application.CutCopyMode = False

    Application.ScreenUpdating = True

End Sub
If any body has idea please provide me
thanks
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Format(Date, "mm/dd/yyyy")
This Date means today.
This line says dsDate and dEDate must be today.
dSDate <> Format(Date, "mm/dd/yyyy") Or dEDate <> Format(Date, "mm/dd/yyyy")
 
Upvote 0
thanks for your notice can you help me about mod the code I mean format the date in l2,m2 not today
I modified but the problem still continue
VBA Code:
If dSDate > dEDate Or dSDate <> Format(dSDate, "mm/dd/yyyy") Or dEDate <> Format(dEDate, "mm/dd/yyyy") Then
     MsgBox "the first date can't be more than end date", vbInformation
      Exit Sub
     End If
 
Upvote 0
Do you mean you want to check if dSDate & dEDate are date?
Try:
VBA Code:
  If dSDate > dEDate Or Not IsDate(dSDate) Or Not IsDate(dEDate) Then
     MsgBox "the first date can't be more than end date", vbInformation
     Exit Sub
  End If
 
Upvote 0
Solution
yes that's great ! thank you ,but I have something may you help me ,please? I added this line
VBA Code:
wsDate.Range("a2").CurrentRegion.ClearContents
but it clears the headers I added after this line
Code:
 Set wsDate = ThisWorkbook.Sheets("data")
 
Upvote 0
Try:
VBA Code:
wsDate.Range("a1").CurrentRegion.Offset(1).ClearContents
 
Upvote 0
@Akuini thanks for your assistance I would add last thing in n2 the name based on column 2
I defined the new variable and add some lines but doesn't work
VBA Code:
dname = wsDate.Range("n2").Value
   For i = 2 To 1000 Step -1
    If aData(i, 1) = dSDate And aData(i, 1) = dEDate And aData(i, 2) = dname Then
    lRowStart = i
    Debug.Print "End row = " & lRowEnd
    Exit For
    
End If
Next i
short word I would add conditions if l2,m2 = dates and n2 = name based on column 2 then bring data
and if l2,m2,n2 are empty show all data
thanks again
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,869
Members
449,054
Latest member
juliecooper255

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