473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Excel VBA help needed.

118 100+
Hi folks,

I am making a spreadsheet for a foreign exchange bureau, on the loading of the page it asks the operator to add the day's exchange rates.

I have the following code in Excel VBA

Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_Open()
  2.  
  3. Worksheets("Rates").Activate
  4.  
  5. 'GBP to Euro
  6.  
  7. Dim varInput As String
  8.  
  9. varInput = InputBox("Please enter today's rates from GBP to EUR", "Exchange Rates", "e.g. 0.765")
  10.  
  11. Range("C11").Select
  12.  
  13. If(varInput = "") Then
  14. Selection.Value = Range("C11").Value
  15.  
  16. Elseif(varInput = "e.g. 0.765") Then
  17. MsgBox "Please enter a proper value.", vbOkOnly, "System Message"
  18.  
  19. Else
  20.  
  21. Selection.Value = varInput
  22.  
  23. End If
  24.  
  25. 'GBP to American Dollars
  26.  
  27. Dim varInput As String
  28.  
  29. varInputa = InputBox("Please enter today's rates from GBP to USD", "Exchange Rates", "e.g. 0.765")
  30.  
  31. Range("C12").Select
  32.  
  33. If(varInputa = "") Then
  34. Selection.Value = Range("C12").Value
  35.  
  36. Elseif(varInputa = "e.g. 0.765") Then
  37. MsgBox "Please enter a proper value.", vbOkOnly, "System Message"
  38.  
  39. Else
  40.  
  41. Selection.Value = varInputa
  42. End If
  43.  
  44. 'GBP to Japanese Yen
  45.  
  46.  
  47. Dim varInputb As String
  48.  
  49. varInputb = InputBox("Please enter today's rates from GBP to YEN", "Exchange Rates", "e.g. 0.765")
  50.  
  51. Range("C13").Select
  52.  
  53. If(varInputb = "") Then
  54. Selection.Value = Range("C13").Value
  55.  
  56. Elseif(varInputb = "e.g. 0.765") Then
  57. MsgBox "Please enter a proper value.", vbOkOnly, "System Message"
  58.  
  59. Else
  60.  
  61. Selection.Value = varInputb
  62. End If
  63.  
  64. 'GBP to Canadian Dollars
  65.  
  66.  
  67.  
  68. Dim varInputc As String
  69.  
  70. varInputc = InputBox("Please enter today's rates from GBP to CAD", "Exchange Rates", "e.g. 0.765")
  71.  
  72. Range("C14").Select
  73.  
  74. If(varInputc = "") Then
  75. Selection.Value = Range("C14").Value
  76.  
  77. Elseif(varInputc = "e.g. 0.765") Then
  78. MsgBox "Please enter a proper value.", vbOkOnly, "System Message"
  79.  
  80. Else
  81.  
  82. Selection.Value = varInputc
  83. End If
  84.  
  85. 'GBP to Australian Dollars
  86.  
  87.  
  88.  
  89. Dim varInputd As String
  90.  
  91. varInputd = InputBox("Please enter today's rates from GBP to AUD", "Exchange Rates", "e.g. 0.765")
  92.  
  93. Range("C15").Select
  94.  
  95. If(varInputd = "") Then
  96. Selection.Value = Range("C15").Value
  97.  
  98. Elseif(varInputd = "e.g. 0.765") Then
  99. MsgBox "Please enter a proper value.", vbOkOnly, "System Message"
  100.  
  101. Else
  102.  
  103. Selection.Value = varInputd
  104. End If
  105. End Sub
  106.  
That works perfectly, but at the moment if the value is the default value of the InputBox ("e.g. 0.765") it just throws up the error message and then moves on to the next part.

How would I make it so that, without having to repeat the first bit of code (inputbox code) each time, if the elseif statement is executed it gives the error message (msgbox) and then prompts them again to enter a value for the same rate?

Hope that makes sense,
Sam
Mar 20 '08 #1
1 1217
Here is one way...

Private Sub Workbook_Open()

Dim varInput As String
Dim varInputa As String
Dim varInputb As String
Dim strDefault As String

Worksheets("Rates").Activate

'GBP to Euro
strDefault = "Please enter today's rates from GBP to EUR"
StartOver:
varInput = InputBox(strDefault, "Exchange Rates", "e.g. 0.765")

If (varInput = "") Then
Exit Sub ' user cancelled

ElseIf Val(varInput) = 0 Then
strDefault = "Please Try Again - Enter Today's GBP/EUR Rate "
GoTo StartOver

Else
Range("C11").Value = varInput
End If
Mar 21 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ian castleden | last post by:
From time to time I see requests for some sort of python MS Excel interface/software. I have written just such software 100% python (works with Jython too! at least it did a year ago). It was...
3
by: Prakash | last post by:
Hi, We face problems uploading excel (with macros) documents using HTML File Upload. The file contents are corrupted while viewing the same. However, we are able to upload excel (w/o. macros)...
6
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C#...
7
by: mattmerc | last post by:
Hi all, We have all seen lot of method for exporting datagrids to Excel. I have a slightly different need. I think it should be easy to accomplish but I am not sure how. I would like when a user...
15
by: John Machin | last post by:
I am pleased to announce a new general release (0.5.2) of xlrd, a Python package for extracting data from Microsoft Excel spreadsheets. CHANGES: * Book and sheet objects can now be pickled and...
7
by: rdemyan via AccessMonster.com | last post by:
I want to make sure that I'm closing an opened spreadsheet correctly. I've been having some locking up problems. The following code adds a dummy row to the spreadsheet to ensure that that the data...
2
by: madeleine | last post by:
I'm hoping the answer to this is that I'm just doing something silly, but I'm really scratching my head over this one. I'm importing data from multiple workbooks, each workbook has a sheet called...
3
by: dan_roman | last post by:
Hi, I developed a script with a nice interface in Tkinter that allows me to edit some formulas and to generate an Excel worksheet with VBA macros within it. The script runs perfectlly in Office...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.