I'm trying to create a database based on lots of spreadsheets. I aim to go through a lot of spreadsheets pulling out the information needed into a nicely formatted table. I have just started so I'm writing some functions to pull out what I need. I'll store them in variables and then put them into a table.
The function I have written searches a range for a string and pulls out the information in the cell next to it. It seems to work fine for the strings.
Can anyone explain what is happening with the dates? I want it to output dates in UK format dd/mm/yyyy. Currently the date next to expiry 01/06/2018 is in UK format. If I debug it and put in a print it comes out in UK format. If I check the format of the cell it's stored in and the cell it is being returned to both are UK format. Yet it comes out as 06/01/2018.
If possible could an explanation of what is happening be included in the answer rather than a "Just change the code to this" solution so I can learn how it is actually interpreting it?
Any help would be appreciated.
Here's the code
Private Sub Main()
'Turn off screen updating
Application.ScreenUpdating = False
'Define the variables
Dim Coverholder As String
Dim SearchRange As Range
'set the range to search
Set SearchRange = Sheets("2017 Property").Range("A1:AZ100")
'testing
Range("A1").Value = GetNextTo(SearchRange, "*Expiry*")
Debug.Print GetNextTo(SearchRange, "*Expiry*")
'Reset the position of the selection
Range("A1").Select
'Turn on the screen updating
Application.ScreenUpdating = True
End Sub
'Function to return the value one cell to the right of a search value
Private Function GetNextTo(SearchRange As Range, FindString As String) As String
'Define variables
Dim Rng As Range
'Clean the string
If Trim(FindString) <> "" Then
'use the search range
With SearchRange
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
'If value found
If Not Rng Is Nothing Then
Application.Goto Rng.Offset(0, 1)
'If value not found
Else
MsgBox "Nothing found"
End If
End With
End If
GetNextTo = ActiveCell.Value
End Function