![]() |
#7
|
|||
|
|||
![]()
Ok Cool dude. Thanks again for your help, hope you have a good evening! I've worked on it some today and this is what it looks like right now. Its working and outputting the answers properly. Good progress!!!
![]() Code: Option Explicit Private Const sName As String = "Billy|Melanie|John|Susan|Ted|Christine|Bobby" Private Const sPlace As String = "Cliff|Tower|Bridge|Skyscraper" Private Const sItem As String = "Stone|Rock|Wrench|Box|Crate|Suitcase" Private vName As Variant, vPlace As Variant, vItem As Variant Private iPlace As Integer, iName As Integer, iItem As Integer Private iHeight As Double, iMass As Double Private sText As String Private Function RandomNumber(Lowest As Long, Highest As Long, _ Optional Decimals As Integer) As Integer If IsMissing(Decimals) Or Decimals = 0 Then Randomize RandomNumber = Int((Highest + 1 - Lowest) * Rnd + Lowest) Else Randomize RandomNumber = Round((Highest - Lowest) * Rnd + Lowest, Decimals) End If End Function Function FileExists(FName As String) As Boolean ' Returns True if the file FName exists, else False Dim fs Set fs = CreateObject("Scripting.FileSystemObject") FileExists = fs.FileExists(FName) Set fs = Nothing End Function Sub anticopy() Dim oxlApp As Object ' Used for the Excel App Dim oxlWbk As Object ' Used for the Workbook Dim FN As String Dim AnswerA As Single ' Part A solution Dim AnswerB As Single ' Part B solution Dim AnswerC As Single ' Part C solution FN = "C:\users\proctor.david\documents\vba\testanswers. xlsx" ' Spreadsheet Name vName = Split(sName, "|") vPlace = Split(sPlace, "|") vItem = Split(sItem, "|") iHeight = RandomNumber(20, 80, 1) iMass = RandomNumber(2, 25, 0) iPlace = RandomNumber(0, UBound(vPlace)) iName = RandomNumber(0, UBound(vName)) iItem = RandomNumber(0, UBound(vItem)) AnswerA = iMass * 9.8 AnswerB = ((2 * iHeight) / 9.8) ^ 0.5 AnswerC = 9.8 * AnswerB sText = vName(iName) & " dropped a " & vItem(iItem) & " off a " & iHeight & " meter-high " & _ vPlace(iPlace) & ". The " & vItem(iItem) & " has a mass of " & iMass & " kilograms." & _ Chr(11) & Chr(9) & "A. Calculate the force of gravity on the " & vItem(iItem) & "." & Chr(11) & _ Chr(9) & "B. Calculate the time it will take to hit the ground. Ignore air resistance." & _ Chr(11) & Chr(9) & "C. How fast will the " & vItem(iItem) & " be falling when it hits the ground?" & _ Chr(11) Selection.TypeText sText Set oxlApp = CreateObject("Excel.Application") If FileExists(FN) Then ' Check to see if the spreadsheet exists. Set oxlWbk = oxlApp.Workbooks.Open(FileName:=FN) ' Open the Workbook oxlWbk.ActiveSheet.Cells(1, 1).Value = AnswerA oxlWbk.ActiveSheet.Cells(1, 2).Value = AnswerB oxlWbk.ActiveSheet.Cells(1, 3).Value = AnswerC oxlWbk.Close SaveChanges:=True ' Close the spreadsheet, saving the changes. Set oxlWbk = Nothing Else ' It does not exist so tell the user. MsgBox "Excel File " & FN & " not found" End If ' Close the Excel App cleanly oxlApp.Quit Set oxlApp = Nothing End Sub |
Tags |
word 2016 |
|