Do While loops

elliott10

New Member
Joined
Jan 16, 2020
Messages
23
Office Version
  1. 365
Platform
  1. MacOS
Hey guys! Thanks so much for answering ALL my questions all the time. I am working on a macro here to do something specific. Say you are working with a process over long periods of time and you want to know how many minutes there are when the process is turned on and off. The macro will first ask the user to input hours on, hours off, minutes on, then minutes off. Data might look like this: 7 hours (on) 33 minutes (on) and 9 hours (off) 22 minutes (off). The macro should then calculate the difference between 9 hrs 22 mins and 7 hrs 33 mins. So far I have this:

Option Explicit
Sub problem2()
Dim totalmin As Double
Dim Deltamin As Double
Dim HON As Double
Dim MON As Double
Dim HOFF As Double
Dim MOFF As Double
Dim answer As String

HON = InputBox("Input hours on")
HOFF = InputBox("Input hours off")
MON = InputBox("Input minutes on")
MOFF = InputBox("Input minutes off")

totalmin = 0
Deltamin = 0

Do While answer = "yes"
Deltamin = (HOFF - HON) * 60 + (MOFF - MON)
totalmin = totalmin + Deltamin
Loop


answer = InputBox("More data? yes or no")
If answer = "no" Then
MsgBox "Total is " & totalmin
Wend

answer



End Sub

Can someone help me fix this? I know it needs to be a Do While there is more data to input loop. Answer should pop up in a message box. Thank you.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
VBA Code:
Dim Answer as Long

'...

Deltamin = 0
Answer = vbYes
Do While answer = vbYes
    Deltamin = (HOFF - HON) * 60 + (MOFF - MON)
    totalmin = totalmin + Deltamin


    Answer = MsgBox("More Data", vbYesNo)
Loop


' delete  answer = InputBox("More data? yes or no")
' delete   If answer = "no" Then
MsgBox "Total is " & totalmin
' delete  Wend

' delete
answer
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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
Back
Top