Can you explain this CDate code please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
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
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Changing the year in there from 2021 to any other year should not cause an error.
 
Upvote 0
The "CDATE" function is simply used to convert a Text entry into a Date entry.
See: MS Excel: How to use the CDATE Function (VBA)

You can find these things yourself with a simple Google search, i.e. search on "Excel VBA CDATE function".

If you are getting an error on that line, it almost certainly has to do with the value of the "strDate" variable.
 
Upvote 0
In my first post if I were to delete the following.
strDate = ws.Range("A5").Value
What part of the code below would I also need to delete.

Basically G INCOME cell A3 would be a month.
Example 06 JUNE

When the code runs the value in cell A3 on G INCOME is looked at & then on G SUMMARY range C6:C16 06 JUNE is found & the values from D31 & E31 are pasted accordingly.

So I don’t believe the CDate part is now in use.
 
Upvote 0
Morning,
You can download my test file here
MY TEST FILE

I need to remove the redundant code in Private Sub SUMMARYTRANSFER

On worksheet G INCOME the code will look at the date in cell A3
It then looks to find this date on worksheet G SUMMARY in the range C5:C17
The code then copies the values on worksheet G INCOME in cells D31 & E31
& pastes them next to the date it just found in the column D & E
Thats all


So there is no need for the Dim strDate As String & If CDate(strDate) > CDate("05/04/2021") Then
But im then not sure what i need to do to tidy up the code with RTE

Basically its just look at the date one the sheet ,find the date on next sheet & then copy 2 values from one sheet to the other.
Thanks.
Im then finished once this is done.
 
Upvote 0
I have this working now but need some help if i may.

If the month isnt found in the range C5:C17 i need the code to stop & show me a MsgBox


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:
            
    End If
        MsgBox "TRANSFER TO SUMMARY SHEET ALSO COMPLETED", vbInformation + vbOKOnly, "SUMMARY TO TRANSFER SHEET COMPLETED MESSAGE"
    
        Range("A5").Select
    End With
    
End Sub
 
Upvote 0
Change this part:
VBA Code:
    Else:
            
    End If
to this:
VBA Code:
    Else
          MsgBox "Month not found in the range"  
          Exit Sub
    End If
 
Upvote 0
Solution
Thanks,nearly done & your advice worked.

Can you advise what ive done wrong here please.
The Range("A3") would be say 03 DECEMBER, where there is no such thing. so my MsgBox wouls read 03 DECEMBER WAS NOT FOUND IN THE RANGE

Rich (BB code):
MsgBox "Range("A3") & "WAS NOT FOUND IN THE RANGE",vbCritical + vbOKOnly, "NO MONTH IN SUMMARY SHEET RANGE"
 
Upvote 0
Ive done it now thanks.

Rich (BB code):
 MsgBox Range("A3") & vbNewLine & "WAS NOT FOUND IN THE RANGE", vbCritical + vbOKOnly, "NO MONTH IN SUMMARY SHEET RANGE"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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