Can you explain this CDate code please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,251
Office Version
  1. 2007
Platform
  1. Windows
This is the code in use.
Can you explain please the line of code CDate shown in red.
It currently shows 2021 but why ?
If i change it to 2022 or 2023 etc then i see error messages so trying to understand how it works



Rich (BB code):
Private Sub SUMMARYTRANSFER()
    Dim rFndCell As Range
    Dim strData As String
    Dim stFnd As String
    Dim fRow As Long
    Dim sh As Worksheet
    Dim ws As Worksheet
    Dim strDate As String

    Set ws = Sheets("G INCOME")
    Set sh = Sheets("G SUMMARY")
    stFnd = ws.Range("A3").Value
    strDate = ws.Range("A5").Value
    With sh

        Set rFndCell = .Range("C6:C16").Find(stFnd, LookIn:=xlValues)
        If Not rFndCell Is Nothing Then
            fRow = rFndCell.Row
            If CDate(strDate) > CDate("05/04/2021") Then
                sh.Cells(fRow, 4).Resize(, 1).Value = ws.Range("D31").Value
                sh.Cells(fRow, 5).Resize(, 1).Value = ws.Range("E31").Value
            Else:

            End If
            MsgBox "TRANSFER TO SUMMARY SHEET ALSO COMPLETED", vbInformation + vbOKOnly, "SUMMARY TO TRANSFER SHEET COMPLETED MESSAGE"
        Else
            MsgBox "DOES NOT EXIST", vbCritical + vbOKOnly, "SUMMARY TO TRANSFER SHEET FAILED MESSAGE"
            Range("A5").Select
        End If
        Range("A3:B3").ClearContents
        Range("E3").ClearContents
        Range("C3").ClearContents
        Range("A5:B30").ClearContents
        Range("A5:A30").NumberFormat = "@"
        Range("A5").Select
        ActiveWorkbook.Save
        
    
    End With
    
End Sub
 
Doing some more testing this morning i have found an issue.
This is the code in use.

Just a recap sheet G INCOME in cell A3 is a value.
On sheet G SUMMARY is a list of values in column C where the value from A3 is looked for & then values entered if found.

As a test on G SUMMARY i changed 04 APRIL to 04 APRILLLL
So what should happen is when the code looks at the value in G INCOME A3 thus being 04 APRIL it looks on sheet G SUMMARY for a match BUT as i changed it to 04 APRILLL it should show me the Msgbox "04 APRIL WAS NOT FOUND IN THE RANGE" BUt it continues to paste the values.

Do you see what this is happening.

Thanks


Rich (BB code):
Private Sub SUMMARYTRANSFER()
    Dim rFndCell As Range
    Dim stFnd As String
    Dim fRow As Long
    Dim sh As Worksheet
    Dim ws As Worksheet

    Set ws = Sheets("G INCOME")
    Set sh = Sheets("G SUMMARY")
    stFnd = ws.Range("A3").Value
    
    With sh

    Set rFndCell = .Range("C5:C17").Find(stFnd, LookIn:=xlValues)
    If Not rFndCell Is Nothing Then
    
        fRow = rFndCell.Row
        sh.Cells(fRow, 4).Resize(, 1).Value = ws.Range("D31").Value
        sh.Cells(fRow, 5).Resize(, 1).Value = ws.Range("E31").Value
    Else
          MsgBox Range("A3") & vbNewLine & "WAS NOT FOUND IN THE RANGE", vbCritical + vbOKOnly, "NO MONTH IN SUMMARY SHEET RANGE"
          Worksheets("G SUMMARY").Activate
          Worksheets("G SUMMARY").Range("C5").Select
          Exit Sub
    End If
        MsgBox "TRANSFER TO SUMMARY SHEET ALSO COMPLETED", vbInformation + vbOKOnly, "SUMMARY TO TRANSFER SHEET COMPLETED MESSAGE"
        Sheets("G INCOME").Range("A5:B30").ClearContents
        Range("A5").Select
    End With
    
End Sub
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Just an update.
On G SUMMARY sheet if i have in the cell any of the below the MsgBox is then shown
04
04 A
04 AP
04 APR
04 APRI

BUT as soon as i type 04 APRIL followed by anything at all the values are entered
So 04 APRILLLLLLLLLLLLL still allows the values to be entered.
Looks like as long as 04 APRIL is seen in the value then thats good enough but is then then a partial match where i need an exact match ?
 
Upvote 0
See if this fixes the issue:
Rich (BB code):
    Set rFndCell = .Range("C5:C17").Find(stFnd, LookIn:=xlValues, LookAt:=xlWhole)
This only looks for complete matches, and not partial ones.
If you wanted partial mataches, you would use "xlPart".
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,535
Members
449,316
Latest member
sravya

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