View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Helmut Weber[_2_] Helmut Weber[_2_] is offline
external usenet poster
 
Posts: 163
Default Macro converting Wind Direction from text to degrees

Hi Norm,

forget about the "MVP" in my signature.
It is for Word, not Excel, which I am only playing with.

I created a testing environment,
an first array which holds the wind direction as text in letters.
("N,NNE,NE,ENE,E,ESE,SE,SSE,S,SSW,SW,WSW,W,WNW,NW, NNW", ",")

A second array which holds the wind direction as text in digits.
("0,23,45,68,90,113,135,158,180,203,225,248,270,29 3,315,338", ",")

A third array which holds the wind direction as number,
so that e.g. N would correspont to 0,
NNE would correspont to 23 ...

I filled in column 1 the cells from row 17 to 26000
with random text values from:
"N,NNE,NE,ENE,E,ESE,SE,SSE,S,SSW,SW,WSW,W,WNW,NW,N NW"

Then I loop through all cells from 17 to 26000,
check what index in the first array the value of the cell has,
and write the corresponding value from the third array (numbers)
in the cell right next to it.

Needs 15 seconds, here and now.

Yes, a challenge, IMHO, for someone saying
My macro skills are not great


There are a million other ways and many details,
which I can't explain in a posting.

' -------------------------------------------------------------------
Sub Test447()
Dim lTime As Single
lTime = Time
Dim sDrc() As String ' direction
Dim sTmp() As String
Dim lDrc(0 To 15) As Long
Dim lRow As Long
Dim x As Long
sDrc = Split("N,NNE,NE,ENE,E,ESE,SE,SSE,S,SSW,SW,WSW,W,WN W,NW,NNW",
",")
sTmp =
Split("0,23,45,68,90,113,135,158,180,203,225,248,2 70,293,315,338",
",")
For x = 0 To 15
lDrc(x) = CLng(sTmp(x))
Next
Randomize
For lRow = 17 To 26000
Cells(lRow, 1).Value = sDrc(Int(16 * Rnd))
Next

' cells in row 1 are filled with random values from sDrc
For lRow = 17 To 26000
For x = 0 To 15
If Cells(lRow, 1).Value = sDrc(x) Then
Cells(lRow, 2).Value = lDrc(x)
End If
Next
Next

MsgBox Time - lTime
End Sub
' -------------------------------------------------------------


HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"