If you find any post useful then, please do share with others. Thanks!

Popular Posts

Contact

Email me

Need help and that also free? I like to learn this way, in case of any question or for a small task, please feel free to email me with details and example data, if required.

Friday, March 17, 2023

Number Guessing Game, Using Excel VBA

In this game, the player has to guess a randomly generated secret number between 1 and 100. The player has 10 guesses to correctly guess the number. After each guess, the player is told whether the secret number is higher or lower than their guess. If the player correctly guesses the secret number, they win the game. If the player uses up all their guesses without correctly guessing the secret number, they lose the game.


Note: This game uses the Rnd function to generate a random number. However, the Rnd function is based on the system clock, so if you want to generate truly random numbers, you may need to use a more advanced random number generator.


Sub NumberGuessingGame()

    Dim SecretNumber As Integer

    Dim Guess As Integer

    Dim GuessesRemaining As Integer

    

    SecretNumber = Int((100 - 1 + 1) * Rnd + 1) 'generate a random number between 1 and 100

    GuessesRemaining = 10 'set the number of guesses

    

    Do While GuessesRemaining > 0

        Guess = InputBox("Guess the secret number between 1 and 100." & vbNewLine & "You have " & GuessesRemaining & " guesses remaining.", "Number Guessing Game")

        If Guess = SecretNumber Then

            MsgBox "Congratulations! You guessed the secret number in " & (10 - GuessesRemaining + 1) & " guesses."

            Exit Sub

        ElseIf Guess < SecretNumber Then

            MsgBox "The secret number is higher than your guess."

        ElseIf Guess > SecretNumber Then

            MsgBox "The secret number is lower than your guess."

        End If

        GuessesRemaining = GuessesRemaining - 1

    Loop

    

    MsgBox "Game over. The secret number was " & SecretNumber & "."

End Sub


Analysis ToolPak; Excel Add In for Statistical Analysis (Descriptive Statistics)

The Analysis ToolPak is an add-in for Microsoft Excel that provides additional functionality for data analysis. One of the features of the Analysis ToolPak is the Descriptive Statistics tool, which allows users to quickly calculate basic statistical measures for a data set. If you are using the Office 365 version, in your MS Excel file, go to Developer Tab (If Developer Tab not available, then right click on the top ribbon and click on customize ribbon and then check the Developer box) and click on Excel Add Ins and select the "Analysis ToolPak". 


To use the Descriptive Statistics tool in the Analysis ToolPak, follow these steps:


Click on the "Data" tab in the Excel ribbon.


Click on "Data Analysis" in the "Analysis" section.


If the "Data Analysis" option is not visible, you may need to install the Analysis ToolPak add-in first.


Select "Descriptive Statistics" from the list of tools, then click "OK."


In the "Descriptive Statistics" dialog box, select the range of data you want to analyze.


Choose the output options you want to include, such as the mean, standard deviation, and quartiles.


Click "OK" to generate the results.


The output of the Descriptive Statistics tool will include a summary table with basic statistical measures for the selected data set, such as the mean, standard deviation, minimum and maximum values, and quartiles. It can also include a histogram or frequency distribution chart to help visualize the distribution of the data.


The Descriptive Statistics tool is useful for quickly analyzing the basic characteristics of a data set. It can be used in a variety of applications, such as market research, scientific analysis, and financial forecasting.


In summary, the Descriptive Statistics tool in the Analysis ToolPak add-in for Microsoft Excel provides a quick and easy way to calculate basic statistical measures for a data set. It is a useful tool for data analysis and can be used in a variety of applications.