HomeVedantSri Varanasi ArticleExcel VBA Typing Master Project for beginners | Excel VBA Project by...

Excel VBA Typing Master Project for beginners | Excel VBA Project by Pradip VedantSri

5/5 - (2 votes)

Excel VBA Typing Master Project for beginners | Excel VBA Project by Pradip VedantSri

Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub

Option Explicit

Dim sentenceList As Variant ‘ Store all sentences
Dim currentSentence As String ‘ Current sentence being typed
Dim words() As String ‘ Words in the current sentence
Dim currentWordIndex As Integer ‘ Index of the current word
Dim totalWords As Long ‘ Total words typed
Dim correctWords As Long ‘ Correct words typed
Dim startTime As Double ‘ Typing test start time

‘yah lblsentences ka code hai jisme automatic excel ke sentence aayenge
Private Sub UserForm_Initialize()
‘Load sentences from the “Sentences” sheet
sentenceList = ThisWorkbook.Sheets(“Sentences”).Range(“A1:A” & ThisWorkbook.Sheets(“Sentences”).Cells(Rows.Count, 1).End(xlUp).Row).Value
currentWordIndex = 0
totalWords = 0
correctWords = 0
startTime = Timer

'Display current date in lbldate textbox
lbldate.Caption = Format(Now, "dd-mm-yyyy hh:mm:ss")

LoadNextSentence

End Sub

Private Sub txtbox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
‘ Handle spacebar or enter key press
If KeyCode = vbKeySpace Then
HandleWordCompletion
ElseIf KeyCode = vbKeyReturn Then
If currentWordIndex >= UBound(words) + 1 Then
LoadNextSentence
End If
End If

' Ensure cursor remains in the input textbox
txtbox.SetFocus

End Sub

Private Sub HandleWordCompletion()
‘ Remove trailing space from input
Dim userInput As String
userInput = Trim(txtbox.Text)

' Check if the word is correct
If userInput = words(currentWordIndex) Then
    correctWords = correctWords + 1
End If

totalWords = totalWords + 1
currentWordIndex = currentWordIndex + 1

' Clear the input box
txtbox.Text = ""

' Check if the sentence is completed
If currentWordIndex >= UBound(words) + 1 Then
    lblhighlight.Caption = "Press Enter for next sentence..."
    lblhighlight.BackColor = RGB(255, 165, 0) ' Highlight in orange
Else
    ' Highlight the next word
    lblhighlight.Caption = words(currentWordIndex)
    lblhighlight.BackColor = RGB(255, 255, 0) ' Highlight in yellow
End If

' Update stats
UpdateStats

' Ensure cursor remains in the input textbox
txtbox.SetFocus

End Sub

Private Sub LoadNextSentence()
Static sentenceIndex As Integer

' Check if all sentences are completed
If sentenceIndex >= UBound(sentenceList, 1) Then
    MsgBox "Test Completed!", vbInformation
    Me.Hide
    Exit Sub
End If

' Load the next sentence
currentSentence = sentenceList(sentenceIndex + 1, 1)
words = Split(currentSentence, " ") ' Split sentence into words
currentWordIndex = 0 ' Reset word index

' Display the sentence and first word
lblsentence.Caption = currentSentence
lblhighlight.Caption = words(currentWordIndex)
lblhighlight.BackColor = RGB(255, 255, 0) ' Highlight the first word

' Increment sentence index
sentenceIndex = sentenceIndex + 1

' Ensure cursor remains in the input textbox
txtbox.SetFocus

End Sub

Private Sub UpdateStats()
Dim elapsedTime As Double
elapsedTime = Timer – startTime ‘ Calculate total elapsed time in seconds

' Update WPM, Accuracy, and Word Count
lblwpm.Caption = "WPM: " & IIf(totalWords > 0, Int((totalWords / elapsedTime) * 60), 0)
lblwordcount.Caption = "Words Typed: " & totalWords

' Prevent division by zero for accuracy
If totalWords > 0 Then
    Dim accuracy As Double
    accuracy = (correctWords / totalWords) * 100
    lblaccuracy.Caption = "Accuracy: " & Format(accuracy, "0.00") & "%"
Else
    lblaccuracy.Caption = "Accuracy: 0.00%" ' Default accuracy when no words typed
End If

' Update Duration
Dim minutes As Long, seconds As Long
minutes = Int(elapsedTime / 60)
seconds = Int(elapsedTime Mod 60)
lblduration.Caption = "Duration: " & Format(minutes, "00") & ":" & Format(seconds, "00")

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim ws As Worksheet
Dim newRow As Long
Dim elapsedTime As Double
Dim wpm As Integer
Dim accuracy As Double

elapsedTime = Timer - startTime ' Total time in seconds

' Handle division by zero for WPM
wpm = IIf(elapsedTime > 0, Int((totalWords / elapsedTime) * 60), 0)

' Handle division by zero for accuracy
If totalWords > 0 Then
    accuracy = (correctWords / totalWords) * 100
Else
    accuracy = 0
End If

' Save results to the "Result" sheet
Set ws = ThisWorkbook.Sheets("Result")
newRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
ws.Cells(newRow, 1).Value = lbldate.Caption
ws.Cells(newRow, 2).Value = Format(elapsedTime / 60, "0.00") & " min"
ws.Cells(newRow, 3).Value = wpm
ws.Cells(newRow, 4).Value = Format(accuracy, "0.00") & "%"
ws.Cells(newRow, 5).Value = totalWords

End Sub

‘yah Exit Button ka Code hai
Private Sub lblexit_Click()
ThisWorkbook.Save
Unload Me
Application.Quit
End Sub

‘yah Excel on Button ka Code hai
Private Sub lblon_Click()
Application.Visible = True
Unload Me
End Sub

VedantSri Sessional Reward Ceremony Toppers Students Image
VedantSri Sessional Reward Ceremony Toppers Students
3,600FansLike
12,900FollowersFollow
20FollowersFollow
456FollowersFollow
97,000SubscribersSubscribe
Call Now Button