Tennisguuy
Well-known Member
- Joined
- Oct 17, 2007
- Messages
- 564
- Office Version
- 2016
- Platform
- Windows
I don't know much about how to write VBA code but I had a situation I wanted a statement to appear if certain counties were in AR. I had the states in column W17:W130 and the counties X17:X130 on the spreadsheet.
On the sheet in cell A17 I entered this formula Arkansaswind(W17:W130)
Now I have a situation where I have multiple states and multiple counties and I tried to modify the above code but its not working. I think because I am not sure how to match the individual state with the exact counties. I entered the counties in the same position as the states.
For example if the state is AZ and the county is Gila I wanted the statement to list the state or states and counties with this text include HAS HIGH FLOOD FREQUENCIES. For example if AZ is listed with Gila I wanted it to say Gila, AR county has high flood frequencies. I modified the above VBA code with the below. Some states can have over 60 counties I just listed a few in example below but in the VBA code I show them all
Code:
Function ArkansasWind(myRange As Range) As String
Dim i As Long
Dim counties As Variant
Dim myCount As Long
Dim countyCount As Long
Dim myString As String
Application.Volatile
' Enter array of counties
counties = Array("SALINE", "PULASKI", "LONOKE", "DESHA", "WHITE", "CLEBURNE", "WASHINGTON", "INDEPENDENCE")
' Loop through counties
For i = LBound(counties) To UBound(counties)
' Count the number of times each entry appears in the range
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "AR")
' Add to string if count is greater than 0 and increase county count
If myCount > 0 Then
myString = myString & counties(i) & ", "
countyCount = countyCount + 1
End If
Next i
' See if any records returned
Select Case countyCount
Case 0
ArkansasWind = ""
Case 1
ArkansasWind = Left(myString, Len(myString) - 2) & " ARKANSAS COUNTY HAS HIGH WINDS."
Case Else
ArkansasWind = Left(myString, Len(myString) - 2) & " ARKANSAS COUNTIES HAVE HIGH WINDS."
End Select
End Function
On the sheet in cell A17 I entered this formula Arkansaswind(W17:W130)
Now I have a situation where I have multiple states and multiple counties and I tried to modify the above code but its not working. I think because I am not sure how to match the individual state with the exact counties. I entered the counties in the same position as the states.
For example if the state is AZ and the county is Gila I wanted the statement to list the state or states and counties with this text include HAS HIGH FLOOD FREQUENCIES. For example if AZ is listed with Gila I wanted it to say Gila, AR county has high flood frequencies. I modified the above VBA code with the below. Some states can have over 60 counties I just listed a few in example below but in the VBA code I show them all
Code:
Function Flood(myRange As Range) As String
Dim i As Long
Dim counties As Variant
Dim myCount As Long
Dim countyCount As Long
Dim myString As String
Application.Volatile
' Enter array of counties
counties = Array("APACHE", "COCONINO", "COCHISE", "GILA")
counties = Array("DEL NORTE", "SISKIYOU", "SONOMA", "MARIN")
counties = Array("WALTON", "HOLMES", "WASHINGTON", "JACKSON", "BAY", "CALHOUN")
counties = Array("PICKENS", "CHEROKEE", "ROCKDALE", "GILA")
counties = Array("LAKE", "DU PAGE", "COOK", "GILA", "WHTESIDE")
counties = Array("WARREN", "FOUNTAIN", "VERMILLION", "PARKE", "MONTGOMERY", "VIGO")
counties = Array("EMMET", "WINNEBAGO", "WORTH", "PALO ALTO")
counties = Array("CADDO", "BOSSIER", "WEBSTER", "IBERIA", "TERREBONNE")
counties = Array("ANDROSCOGGIN", "AROSTOOK", "CUMBERLAND", "FRANKLIN")
counties = Array("HINDS", "RANKIN", "COPIAH", "SIMPSON")
counties = Array("DAVIESS", "BUCHANNON", "CLINTON", "CALDWELL", "LIVINGSTON")
counties = Array("ALBANY", "SUFFOLK", "SULLIVAN")
counties = Array("ALFALFA", "GRANT", "CHEROKEE", "ADAIR")
counties = Array("STEWARD")
counties = Array("BRISCO", "WILLIAMSON")
counties = Array("CACHE", "RICH", "WEBER", "TOOELE", "UTAH", "JUAB", "SANPETE", "KANE")
' Loop through counties
For i = LBound(counties) To UBound(counties)
' Count the number of times each entry appears in the range
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "AZ")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "CA")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "FL")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "GA")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "IL")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "IN")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "IO")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "LA")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "ME")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "MS")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "MO")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "NY")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "OK")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "TN")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "TX")
myCount = Application.CountIfs(myRange, counties(i), myRange.Offset(0, -1), "UT")
' Add to string if count is greater than 0 and increase county count
If myCount > 0 Then
myString = myString & counties(i) & ", "
countyCount = countyCount + 1
End If
Next i
' See if any records returned
Select Case countyCount
Case 0
Flood = ""
Case 1
Flood = Left(myString, Len(myString) - 2) & " HAS HIGH FLOOD FREQUENCIES."
Case Else
Flood = Left(myString, Len(myString) - 2) & " HAS HIGH FLOOD FREQUENCIES."
End Select
End Function
Last edited by a moderator: