Excel-VBA InStrRevは要注意

Excelのみでは意識の必要のない小数点の扱いですが、VBAの場合は変数宣言に
注意が必要です。
LongもDoubleも整数型、Singeで単小数点が扱えます。
セル内の値が、
0+12,7.5+12, 11.5+8, 6+8などがあります。
+の前が開始時刻、+の後ろが勤務時間です。
これを数値として取り出さねばなりません。

「+」の位置を検出し、LEFTとRIGHT関数で切り出すのですが、今回慣れない
InStrRevを使ってハマりました。
この関数、探索はは文字列の後ろからですが、返す値は前方からの「位置」、これを
後方からの位置とかって思い込んでの失敗です。

どうにか複雑なケースにも対応出来るように書きました・・・・
これで、Select Caseで処理が出来ます。

以下、Source



Dim Y_n As Long, X_n As Long
Dim i As Long, j As Long, k As Long
Dim Nor_n As Long, Nit_n As Long, Hol_n As Long, Mon_n As Long, Sus_n As Long
Dim Zer_n As Long
Dim W_cF_n As Single, W_cB_n As Single
Dim PoPF_n As Long, PoPB_n As Long


Dim W_c As String, W_c2 As String



For i = 3 To 34

For j = 7 To 57

W_c = Cells(j, i)

'+の前後の数値を取り出す

If W_c = "0" Or W_c = "*" Or W_c = "Y" Or W_c = "=" Then

Else

PoPF_n = InStr(W_c, "+") '+位置を検出前方
PoPB_n = InStrRev(W_c, "+") '+位置を検出後方
PoPB_n = Len(W_c) - PoPB_n + 1

If PoPF_n = 0 And PoPB_n = 1 Then

Else

W_cF_n = Val(Left(W_c, PoPF_n))
W_cB_n = Val(Right(W_c, PoPB_n))


' Debug.Print PoPF_n & ":" & PoPB_n & "-" & Len(W_c)

' Debug.Print W_cF_n & ":" & W_cB_n

End If


End If


Select Case W_c '勤務状態で色を決める



わかお かずまさ
VegaSystems

📷📷📷📷📷📷
#LAN_PRO
#Bloguru
#kuma

People Who Wowed This Post

×
  • If you are a bloguru member, please login.
    Login
  • If you are not a bloguru member, you may request a free account here:
    Request Account
This user only allows bloguru members to make comments.
If you are a bloguru member, please login.
Login
If you are not a bloguru member, you may request a free account here:
Request Account