ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looking for a Month Name in a String (https://www.excelbanter.com/excel-programming/408163-looking-month-name-string.html)

wutzke

Looking for a Month Name in a String
 
Within my worksheet is a number of cells that MIGHT contain a name of
a Month

white mothistled January holdew February tracour December necters
exally slopers
tracing sentagen March samputs twistwased June unent chama puls
moperveyeberannochas micracadwatextilitiously
hung flamons cons bradritabol cashortimizes red

Is there a way programmically to loop thru each cell and search for
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", Dec"


NateBuckley

Looking for a Month Name in a String
 
This could work, I wasn't sure what order you have your data in your cells so
I'm presuming they're all just down on colum, but you can change to suit your
needs.

I'm sure there will be a better way, but this should work (not had chance to
test it)

Option Explicit

Public Sub loopAndFindMonths()
size = Sheets("SheetName").Cells(Rows.Count, "A").Row
Dim i As Long
For i = 1 To size
splitUpString Sheets("SheetName").Cells(i, 1).Value, i
Next i
End Sub
Private Sub splitUpString(str As String, rowAt As Long)
Dim splitString() As String
splitString = split(str, " ")
Dim i As Integer
For i = 0 To UBound(splitString)
If CheckIsMonth(Mid(splitString(i), 1, 3)) Then
Sheets("SheetName").Cells(rowAt, 2).Value = Mid(splitString(i),
1, 3)
End If
Next i
End Sub

Private Function CheckIsMonth(str As String) As Boolean
str = UCase(str)
Select Case str
Case "JAN"
CheckIsMonth = True
Case "FEB"
CheckIsMonth = True
Case "MAR"
CheckIsMonth = True
Case Else
CheckIsMonth = False
End Select
End Function



All times are GMT +1. The time now is 03:22 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com