Function Call

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
Many thanks in advance

Using a function to check a column "E" to the last row if the date format is "DD/MM/YYYY" then it needs to copy the data to the next column "F"
Says DateFormatCheck "Argument not optional" when I try to add function to the command button

Command button code below

VBA Code:
Private Sub cmdUpdate_Click()

Dim res As Integer
Dim ws As Worksheet


  Set ws = ThisWorkbook.Worksheets("TGS JOB RECORD")

With ws

  If DateFormatCheck = True Then .Range("E2:E").Copy Range("F2:F")
    End If
    End With
    
ThisWorkbook.RefreshAll

      End Sub

Then Function Code below

VBA Code:
Function DateFormatCheck(cells As Range) As Boolean

Dim ws As Worksheet
Dim Rng As Range
Dim LRow As Long

Set ws = thisworkbookworksheets("TGS JOB RECORD")

Set LRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set Rng = ws.Range("E2:E" & LRow)

    DateFormatCheck = False
    If IsDate(Rng.Cells.Value) Then
        If Rng.Cells.NumberFormat = "dd/mm/yyyy;@" Then
            DateFormatCheck = True
        ElseIf CInt(Mid(Rng.Cells.Value, 1, 2)) <= 12 Then
            DateFormatCheck = True
        End If
    End If
End Function
 
In that case, I'd try something like:

Code:
With ws
   for each cell in .Range("E2:E" & LastRow).Cells
       If IsDate(cell) then cell.offset(, 1).Value = cdate(cell.value)
    next cell
    End With
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
It now says the Method range of object worksheet failed

For Each Cell In .Range("E2:E" & LastRow).cells
 
Upvote 0
Are you still calculating LastRow as you were before? You seem to like changing the code without telling us... ;)
 
Upvote 0
Solution

Forum statistics

Threads
1,224,837
Messages
6,181,255
Members
453,028
Latest member
letswriteafairytale

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