Excel

Diabetics Log

Ease of Use

Easy

Version tested with

2000 

Submitted by:

johnske

Description:

On opening the workbook, a userform is displayed. This userform is used for entries. 

Discussion:

The most common task for diabetics is to test/monitor and keep a regular log of their blood-glucose levels. This program goes a long way to automating and simplifying such log entries. In addition, a daily average of blood-glucose levels is given that can assist in noticing any up or downward trends that may occur over a period of days (Note the only really 'novel' coding in this entry is making the application invisible so that the workbook simulates a non-Office 'stand-alone' application, and, clicking the userform X actuates a procedure) 

Code:

instructions for use

			

'************************************************ '<< Code for ThisWorkbook module >> Option Explicit Private Sub Workbook_Open() 'hide the workbook Application.Visible = False 'display message MsgBox "On the form that appears next..." & vbNewLine & _ vbNewLine & _ "1) Scroll down and select your reading" & vbNewLine & _ "2) Select an appropriate time" & vbNewLine & _ "3) Write any notes" & vbNewLine & _ "4) Click ENTER", , "Entering Blood Glucose Levels" 'show userform UserForm1.Show False End Sub '************************************************ '************************************************ '<< Code for Module1 >> Option Explicit Sub ShowForm() Application.Visible = False UserForm1.Show False End Sub Private Sub RemoveFromMenu() On Error Resume Next '< error = no control '//remove right-click control With Application.CommandBars("Cell") .Controls("Hide Book").Delete End With End Sub '************************************************ '************************************************ '<< Code for Userform1 module >> Option Explicit Private Sub UserForm_Initialize() 'size & position all the controls With UserForm1 .Caption = "Select your reading and time then click ENTER" .Height = 338 .Width = 232 .Top = 60 .Left = 180 End With With ListBox1 .Height = 304 .Width = 40 .Top = 7 .Left = 7 End With With ListBox2 .Height = 133 .Width = 166 .Top = 7 .Left = 54 End With With Label1 .Height = 20 .Width = 166 .Top = 177 .Left = 54 End With With TextBox1 .Height = 35 .Width = 166 .Top = 200 .Left = 54 End With With CommandButton1 .Height = 30 .Width = 82 .Top = 245 .Left = 55 End With With CommandButton2 .Height = 30 .Width = 82 .Top = 245 .Left = 138 End With With CommandButton3 .Height = 34 .Width = 166 .Top = 275 .Left = 54 End With DoEvents End Sub Private Sub UserForm_Activate() Dim MyList(250), N& With ListBox1 .ControlTipText = "Blood glucose reading in mmol/L" .Font.Size = 8 .ColumnWidths = 10 'insert a list of possible glucose levels (1.0 to 26.0) For N = 0 To 250 MyList(N) = (N + 10) / 10 Next .List = MyList DoEvents End With 'insert a list of broad times rel. to meals With ListBox2 .ControlTipText = "NOTE: It is recommended that readings be taken 2 hours after meals, " & _ "if your ''after'' time differs from that, use ''notes''" .AddItem "On waking / fasting" .AddItem "Before breakfast" .AddItem "After breakfast" .AddItem "Before morning snack" .AddItem "After morning snack" .AddItem "Before midday meal" .AddItem "After midday meal" .AddItem "Before afternoon snack" .AddItem "After afternoon snack" .AddItem "Before evening meal" .AddItem "After evening meal" .AddItem "Before late-night snack" .AddItem "After late-night snack" DoEvents End With 'put headings on sheet [A1] = "Reading (mmol/L)" [B1] = "Time entered" [C1] = "Date" [D1] = "Reading relative to meal" [E1] = "Notes" [F1] = "Days Average" ActiveWindow.FreezePanes = True End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'save and quit if X button clicked If CloseMode = 0 Then CommandButton3_Click End Sub Private Sub CommandButton1_Click() Dim N&, ViewIt As Range 'need listboxes 1 and 2 to have readings If ListBox1 = Empty Or ListBox2 = Empty Then Exit Sub End If 'select row for entry With [A65536].End(xlUp) If .Offset(0, 2) = Date Then 'same-date entry, next row N = 1 Else 'not the same-date, leave an empty row N = 2 End If .Offset(N, 0) = ListBox1 '< reading If .Offset(N, 0) = Empty Then Exit Sub '<ENTER clicked twice .Offset(N, 1) = Format(Time, "hh:mm ampm") .Offset(N, 2) = Format(Date, "dd mmm yy") .Offset(N, 3) = ListBox2 '< rel. to meals .Offset(N, 4) = TextBox1 '< Notes 'enter the average reading in column F If .Offset(N - 1, 0) = Empty Then .Offset(N, 5) = .Offset(N, 0) Else .Offset(N, 5) = Format(WorksheetFunction.Average(.CurrentRegion.Columns(1)), "##.0") 'to avoid confusion clear the previous average .Offset(N - 1, 5).ClearContents End If 'scroll up to only show last 10 rows of entries 'avoid out-of-range errors when < 10 rows On Error Resume Next Application.Goto .Offset(-6, 0).End(xlUp), Scroll:=True End With 'tart up sheet a bit Cells.Columns.AutoFit 'clear all selections & text ListBox1 = Empty ListBox2 = Empty TextBox1 = Empty End Sub Private Sub CommandButton2_Click() 'edit or read past entries 'remove any previous right-click control Run "RemoveFromMenu" '//now add new right-click control With Application.CommandBars("Cell") .Controls.Add(Type:=msoControlButton). _ Caption = "Hide Book" '//assign procedure to this control .Controls("Hide Book"). _ OnAction = "ShowForm" End With Application.Visible = True Unload Me End Sub Private Sub CommandButton3_Click() 'readings have been entered, save and quit Run "RemoveFromMenu" ActiveWorkbook.Save Unload Me Application.Quit End Sub '************************************************

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select View/Project Explorer
  4. Select the 'ThisWorkbook' Module, copy and paste the code for it from above
  5. Now select Insert/Module (this will create Module1)
  6. Copy and paste the code for Module1 (above) into this module
  7. Now select Insert/Userform (this will create Userform1)
  8. In the Toolbox shown, click 'listbox', now put the mouse pointer near the top left of the Userform and move the pointer down and to the right to create ListBox1
  9. Repeat to create ListBox2
  10. In the Toolbox, click 'command button', now put the mouse pointer near the top left of the Userform and move the pointer down and to the right to create CommandButton1
  11. Repeat twice to create CommandButtons 2 and 3
  12. In the Toolbox shown, click 'textbox', now put the mouse pointer near the top left of the Userform and move the pointer down and to the right to create TextBox1
  13. In the Toolbox shown, click 'label', now put the mouse pointer near the top left of the Userform and move the pointer down and to the right to create Label1
  14. (don't worry, the code will size and position all these controls)
  15. Double-click either the listbox or userform and a code pane will appear
  16. Delete any code in this code pane and copy and paste the code for Userform1 from above
  17. Now select File/Close and Return To Microsoft Excel
  18. Save your work
 

Test the code:

  1. Close and re-open the workbook
  2. Alternatively, download and extract the ready-to-use workbook 'BloodGlucoseLevels'
  3. (When viewing or editing entries in the workbook, right-click and select "Hide Book" to show the form again).
 

Sample File:

BloodGlucoseLevels.zip 15.54KB 

Approved by mdmackillop


This entry has been viewed 235 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express