Data Input on Workbook Open

default_name

Board Regular
Joined
May 16, 2018
Messages
151
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
Hey guys,

I am trying to implement an input box that appears when the user first opens the workbook.
Here is how I would like it to function:
  1. User opens the workbook
  2. A prompt appears, which asks the user to enter a balance (which is stored as a variable called 'balance')
    • If the user just hits enter or Cancel (without inputting a new balance), then the box disappears, and the value in cell B1 remains unchanged (it remains at whatever value it was already set to prior to opening the workbook)
    • If the user enters a value and hits OK or enter, the input box disappears, and the value of cell B1 is updated to reflect the new 'balance'
I tried to get it started using some input box code I found online, but I am having issues with even the initial setup (not to mention the Cancel/no data inputted function).
Here is what I currently have started.

VBA Code:
Private Sub Workbook_Open()
Dim balance As Variant
Do
    balance = InputBox("What is your current balance?")
    If IsNumeric(balance) Then Exit Do
    Range("B1").Value = account
Loop
End Sub

Thanks in advance
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,794
Office Version
  1. 365
Platform
  1. Windows
What is the name of the sheet that holds the balance?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,794
Office Version
  1. 365
Platform
  1. Windows
Ok, how about
VBA Code:
Private Sub Workbook_Open()
   Dim balance As Variant
   balance = InputBox("What is your current balance?")
   If balance = "" Then Exit Sub
   Sheets("2020").Range("B1").Value = balance
End Sub
 
Solution

default_name

Board Regular
Joined
May 16, 2018
Messages
151
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

Ok, how about
VBA Code:
Private Sub Workbook_Open()
   Dim balance As Variant
   balance = InputBox("What is your current balance?")
   If balance = "" Then Exit Sub
   Sheets("2020").Range("B1").Value = balance
End Sub
Works perfectly! Thanks for all your help Fluff!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,794
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

default_name

Board Regular
Joined
May 16, 2018
Messages
151
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

Real quick follow-up question to the provided solution.
Hopefully this is ok to ask...if not I can just make a brand new thread.

I am trying to incorporate a Protect Sheet function with the provided code, in the event that "" (no new data) is inputted.
I incorporated a Protect Sheet function, that seems to work just fine, in the event that any new data is inputted, but adding the same Protections (even in the event of a "") seems to be a challenge.

VBA Code:
Private Sub Workbook_Open()
Dim balance As Variant
balance = InputBox("What is your current balance?")
If balance = "" Then
                Sheets("2020").Select
                ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _False
           Exit Sub
Sheets("2020").Range("B1").Value = balance
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _False
Sheets("2020").Select
End Sub
 

EXCEL MAX

Active Member
Joined
Nov 11, 2020
Messages
392
Office Version
  1. 2016
Platform
  1. Windows
Hello Default Name,
you got some syntax error. Try with some changes:
VBA Code:
Private Sub Workbook_Open()
Dim balance As Variant
balance = InputBox("What is your current balance?")
If balance = "" Then
                Sheets("2020").Select
                ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False
                
Exit Sub
End If
Sheets("2020").Range("B1").Value = balance
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False
Sheets("2020").Select
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,128,127
Messages
5,628,860
Members
416,345
Latest member
sayad

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top