0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【VBA】UI構築の一例【自分用備忘録】

Last updated at Posted at 2025-03-13

急遽必要になったので(第二弾)

1. 数値データ, 画像, 警告の表示

' 数値を動的に更新する例
Label_BP.Caption = "血圧: " & bloodPressure & " mmHg"
Label_HR.Caption = "心拍数: " & heartRate & " bpm"

' 画像をUserForm上のImageコントロールに表示する
Image1.Picture = LoadPicture("C:\Data\ECG_Image.jpg")

' 入力フォームから患者情報を取得
Dim patientID As String
patientID = TextBox_ID.Text
MsgBox "患者ID: " & patientID

If bloodPressure > 180 Then
    Label_BP.ForeColor = vbRed
    MsgBox "Warn!", vbCritical
End If

2. レポート作成・データエクスポート

Excelにデータを書き出す

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Data")
ws.Cells(2, 1).Value = bloodPressure  ' B1セルに血圧を書き込む

3. 心拍数 & 血圧を管理するシンプルなUserForm

機能
✅ TextBoxで数値入力
✅ 異常値ならラベルが赤く変化
✅ ボタンを押すとCSVに保存

Private Sub CommandButton1_Click()
    Dim bloodPressure As Integer
    Dim heartRate As Integer

    ' テキストボックスの値を取得
    bloodPressure = CInt(TextBox_BP.Text)
    heartRate = CInt(TextBox_HR.Text)

    ' 異常値チェック(血圧 180 以上で警告)
    If bloodPressure >= 180 Then
        Label_BP.ForeColor = vbRed
        MsgBox "Warn!", vbCritical
    Else
        Label_BP.ForeColor = vbBlack
    End If

    ' CSVに保存
    Dim filePath As String
    filePath = "C:\Data\MedicalData.csv"
    
    Dim fileNo As Integer
    fileNo = FreeFile

    Open filePath For Append As #fileNo
    Print #fileNo, Date & "," & bloodPressure & "," & heartRate
    Close #fileNo

    MsgBox "データを保存しました。", vbInformation
End Sub
  • 実装手順
  1. UserFormを作成
  2. TextBox(TextBox_BP , TextBox_HR)を配置
  3. ラベル(Label_BP)とボタン(CommandButton1)を配置
  4. 上記のコードをボタンのClickイベントに設定
  5. 実行すると、異常値でアラートが出る & CSVにデータ保存
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?