私はこれを理解しようとしており、できません。Excel vbaコンパイルエラー - 引数はオプションではありません。
「コンパイルエラー - 引数はオプションではありません」というエラーが発生します。私は引数を提供しており、オプションとして設定されています!
文字列と配列を関数に渡し、渡された文字列内の配列文字列を数えようとしています。
コード行で実行を停止:行にVal
を強調: "引数オプションのないコンパイルエラー" メッセージ:
For Each Val In toCountARR
全コード:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nameR As Range
Dim colR As Range
Dim TKRcnt As Integer
Dim TKRarr() As Variant
TKRarr = Array("TKR", "THR", "Bipolar")
Dim ORIFcnt As Integer
Dim ORIFarr() As Variant
TKRarr = Array("ORIF", "Ilizarov", "PFN")
Set nameR = Range("P2:P9")
Set colR = Range("B2:B50,G2:G50,L2:L50")
For Each namecell In nameR
For Each entrycell In colR
If entrycell.text = namecell.text Then
TKRcnt = countTextInText(entrycell.Offset(0, 2).text, TKRarr)
ORIFcnt = countTextInText(entrycell.Offset(0, 2).text, TKRarr)
End If
Next entrycell
MsgBox (namecell.text & " TKR count: " & TKRcnt & " ORIF count: " & ORIFcnt)
Next namecell
End Sub
Public Function countTextInText(Optional text As String, Optional toCountARR As Variant) As Integer
Dim cnt As Integer
Dim inStrLoc As Integer
For Each Val In toCountARR
inStrLoc = InStr(1, text, Val)
While inStrLoc <> 0
inStrLoc = InStr(inStrLoc, text, Val)
cnt = cnt + 1
Wend
Next Val
Set countTextInText = cnt
End Function
と
Public Function countTextInText(Optional text As String, Optional toCountARR As Variant) As Integer
である必要があります。どの行が「引数はオプションではありません」というエラーを表示していますか? (b) 'SetTextInText = cnt'はできません。通常は' countTextInText = cnt'と書かれた 'Let countTextInText = cnt'でなければなりません。 – YowE3K
エラー行: 'Public関数countTextInText(文字列としてオプションの文字列、オプションのtoCountARRとして)Integer' –
私の以前の(a)コメントを無視して、' Val'でコンパイルエラーが発生します。 'Val'は必須の引数を渡す必要のあるExcel関数です。変数名として使用する場合は、変数として宣言する必要があります。しかし、より適切な変数名を選択する方が良い考えです。 (しかし、あなたの変数を宣言することはとにかくとても良いアイデアです!) – YowE3K