Keyboard and Mouse VBA Multiple Excel Sendkeys and Clicks
You can download the workbook below. I'll explain the code more eventually. column AH pulls the data selected into excel
Explanation of the Above Program:
The above program brings up a Userform that lets you select a series of programmed inputs such as left click; double left click; left click and hold down the left click and select text between another left click and copy and paste to the location of a third left click; left click and hold down the left click and select text between another left click and delete the text in between (change PDF pages); input whatever text string you input at that point of time in the code; control + C (copy); and control + V (paste). You can technically save each workbook as different recorded actions (which can then be put in their own referenceable listbox for quick selection).
Obvious uses:
1 – Pull data from a formatted PDF with text into Excel.
2 – Input large amounts of data into systems quickly
3 – Pull large amounts of data from systems quickly
4 – This program eliminates the problem of different monitor sizes since users select their own inputs.
5 – In short, anything you do on the PC I can automate or even another person can automate since this program also plays back what they do. I'll add functionality to select ranges or customize loop amounts at a given point, but overall I accomplished what I set out to do.
Please note that the recorded actions get recorded on the workbook itself, so you can copy and paste a recorded section such as copying all the PDF pages down and change the "Text Input" pages to pull data from however many pages..
My favorite part of the code:
Code in workbook Module 1:
Private Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwflags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwextrainfo As LongPtr)
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Private Const VK_LBUTTON = &H1
Private Const VK_F9 = &H78
Private Const keyeventf_keyup = 2
Public Declare PtrSafe Sub keybd_event Lib "user32" (ByVal BYK As Byte, ByVal bscan As Byte, dwflags As Long, ByVal dwextrainfo As Long)
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public numberholder As Double
Public locations As Double
Private Type POINTAPI
X As Long
Y As Long
End Type
Sub Form_KeyDown()
Dim Point As POINTAPI
Dim skip As Double
Dim plop As Double
Dim plop2 As Double
Dim columnacounter As Double
Dim DataObj As MSForms.DataObject
Dim Text As String
Dim X As Long, Y As Long
Dim caseselector As Double
Dim keyholder As String
locations = Range("L1").Value ' blank if running first
If locations = 1 Then
Range("E1").Value = Range("E1").Value + 1
numberholder = Range("E1").Value
GoTo line13:
End If
If locations = 2 Then
numberholder = Range("E1").Value
If numberholder = 1 Then
GoTo line17:
End If
GoTo line11:
End If
line13:
numberholder = Range("E1").Value ' E1 should be equal to 1
Sleep 100
AppActivate Application.Caption
DoEvents
Userform1.Show
line11:
If Range("AA1").Value = 1 Then
GoTo line33:
End If
numberholder = 1
'column Z is an index for actions
numberholder2 = Range("e1").Value
Do Until numberholder > numberholder2
caseselector = Range("Z" & numberholder).Value
Select Case caseselector
Case 1
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 200
Case 2
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 200
Case 3
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Application.SendKeys ("~")
Application.SendKeys ("{NUMLOCK}")
Sleep 200
Case 4
Application.SendKeys ("%D")
Application.SendKeys ("{NUMLOCK}")
Sleep 200
Case 5
Application.SendKeys ("{F5}")
Application.SendKeys ("{NUMLOCK}")
Sleep 200
Case 6
keyholder = Range("U" & numberholder).Value
Application.SendKeys ("^c")
Application.SendKeys ("{NUMLOCK}")
Sleep 200
Case 7
keyholder = Range("U" & numberholder).Value
Application.SendKeys& keyholder
Application.SendKeys ("{NUMLOCK}")
Sleep 200
Case 8
Application.SendKeys ("~")
Application.SendKeys ("{NUMLOCK}")
Sleep 200
Case 9
keyholder = Range("U" & numberholder).Value
Application.SendKeys& keyholder
Application.SendKeys ("{NUMLOCK}")
Sleep 200
Case 10
Range("J" & numberholder).Value = Range("h" & numberholder).Value - Range("f" & numberholder).Value
Range("K" & numberholder).Value = Range("i" & numberholder).Value - Range("g" & numberholder).Value
plop = Range("J" & numberholder).Value
plop2 = Range("K" & numberholder).Value
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_MOVE, plop, plop2, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Application.SendKeys ("^c")
Sleep 200
Application.SendKeys ("{NUMLOCK}")
X = Range("AC" & numberholder).Value
Y = Range("AD" & numberholder).Value
SetCursorPos X, Y
Sleep 200
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 200
Application.SendKeys ("^v")
Sleep 200
Application.SendKeys ("{NUMLOCK}")
Sleep 200
Case 11
Range("J" & numberholder).Value = Range("h" & numberholder).Value - Range("f" & numberholder).Value
Range("K" & numberholder).Value = Range("i" & numberholder).Value - Range("g" & numberholder).Value
plop = Range("J" & numberholder).Value
plop2 = Range("K" & numberholder).Value
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_MOVE, plop, plop2, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 200
Application.SendKeys ("^c")
Sleep 200
Application.SendKeys ("{NUMLOCK}")
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
strpaste = DataObj.GetText(1)
Range("AH" & numberholder).Value = strpaste
Case 12
Range("J" & numberholder).Value = Range("h" & numberholder).Value - Range("f" & numberholder).Value
Range("K" & numberholder).Value = Range("i" & numberholder).Value - Range("g" & numberholder).Value
plop = Range("J" & numberholder).Value
plop2 = Range("K" & numberholder).Value
Range("U" & numberholder).Select
strpaste = Range("U" & numberholder).Value
Selection.Copy
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
DataObj.GetText (1)
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 200
Application.SendKeys ("^v")
Sleep 200
Case 13
columnacounter = 1
erow = Range("A" & Rows.Count).End(xlUp).Row
Do Until columnacounter > erow
Range("a" & columnacounter).Select
Selection.Copy
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
DataObj.GetText (1)
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 200
Application.SendKeys ("^v")
Sleep 300
Application.SendKeys ("{NUMLOCK}")
columnacounter = columnacounter + 1
Loop
Case 14
Application.SendKeys ("{DELETE}")
Application.SendKeys ("{NUMLOCK}")
Case 15
Range("J" & numberholder).Value = Range("h" & numberholder).Value - Range("f" & numberholder).Value
Range("K" & numberholder).Value = Range("i" & numberholder).Value - Range("g" & numberholder).Value
plop = Range("J" & numberholder).Value
plop2 = Range("K" & numberholder).Value
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_MOVE, plop, plop2, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 200
Application.SendKeys ("{DELETE}")
Sleep 200
Application.SendKeys ("{NUMLOCK}")
End Select
numberholder = numberholder + 1
Loop
line17:
line33:
skip = 1
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
Code for the Userform please note that each section is attached to the respectively named ActiveX object. I'll explain how to do that after the below Userform code.
Public locations As Double
Private Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwflags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwextrainfo As LongPtr)
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Private Const VK_LBUTTON = &H1
Private Const VK_F9 = &H78
Private Const keyeventf_keyup = 2
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Public numberholder As Double
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Sub CommandButton1_Click()
numberholder = Range("E1").Value
Dim Point As POINTAPI
Sleep 1000
line48:
line55:
caseselector2 = Range("x" & numberholder).Value
Select Case caseselector2
Case 7
AppActivate Application.Caption
DoEvents
answer = MsgBox("Press Yes when you are ready to do your first left click.", vbYesNo)
If answer = 6 Then
GoTo Line23:
Else
GoTo line20:
End If
Case 6
AppActivate Application.Caption
DoEvents
answer = MsgBox("Press Yes when you are ready to do your second left click.", vbYesNo)
If answer = 6 Then
GoTo line22:
Else
GoTo line21:
End If
Case 5
AppActivate Application.Caption
DoEvents
answer = MsgBox("Press Yes when you are ready to do select the place to paste the mouse move text.", vbYesNo)
If answer = 6 Then
GoTo line29:
Else
GoTo line28:
End If
End Select
AppActivate Application.Caption
DoEvents
answer = MsgBox("Press Yes when you are ready to do a left click.", vbYesNo)
If answer = 6 Then
Else
GoTo line15:
End If
line45:
line22:
line29:
Line23:
Do Until GetAsyncKeyState(VK_F9)
'exit when F9 key is pressed
DoEvents
If (GetAsyncKeyState(VK_LBUTTON) < 0) Then
If Range("x" & numberholder).Value = 5 Then
GetCursorPos Point
X = Point.X
Y = Point.Y
Range("AC" & numberholder).Value = X
Range("AD" & numberholder).Value = Y
If Range("Az1").Value = 1 Then
GoTo line91:
End If
Range("Z" & numberholder).Value = 10
line91:
keybd_event VK_LBUTTON, 1, keyeventf_keyup, 0
GoTo line59:
End If
If Range("x" & numberholder).Value = 6 Then
GetCursorPos Point
X = Point.X
Y = Point.Y
Range("H" & numberholder).Value = X
Range("I" & numberholder).Value = Y
If Range("Az1").Value = 1 Then
GoTo line92:
End If
Range("Z" & numberholder).Value = 10
line92:
keybd_event VK_LBUTTON, 1, keyeventf_keyup, 0
Range("x" & numberholder).Value = Range("x" & numberholder).Value - 1
If Range("Az1").Value = 1 Then
GoTo line72:
End If
GoTo line55:
End If
If Range("x" & numberholder).Value = 7 Then
GetCursorPos Point
X = Point.X
Y = Point.Y
Range("F" & numberholder).Value = X
Range("G" & numberholder).Value = Y
If Range("Az1").Value = 1 Then
GoTo line93:
End If
Range("Z" & numberholder).Value = 10
line93:
keybd_event VK_LBUTTON, 1, keyeventf_keyup, 0
Range("x" & numberholder).Value = Range("x" & numberholder).Value - 1
GoTo line48:
End If
GetCursorPos Point
X = Point.X
Y = Point.Y
Range("F" & numberholder).Value = X
Range("G" & numberholder).Value = Y
Range("Z" & numberholder).Value = 1
keybd_event VK_LBUTTON, 1, keyeventf_keyup, 0
GoTo line9:
End If
line50:
Loop
line9:
If Range("U" & numberholder).Value = 5 Then
GoTo line45:
End If
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
line15:
line20:
line21:
line28:
line30:
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
Range("AA1").Value = 1
line59:
line72:
End Sub
Private Sub CommandButton2_Click()
numberholder = Range("E1").Value
Dim Point As POINTAPI
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Press Yes when you are ready to do a left click.", vbYesNo)
If answer = 6 Then
Else
GoTo line15:
End If
Do Until GetAsyncKeyState(VK_F9)
'exit when F9 key is pressed
DoEvents
If (GetAsyncKeyState(VK_LBUTTON) < 0) Then
GetCursorPos Point
X = Point.X
Y = Point.Y
Range("F" & numberholder).Value = X
Range("G" & numberholder).Value = Y
Range("Z" & numberholder).Value = 2
keybd_event VK_LBUTTON, 1, keyeventf_keyup, 0
GoTo line9:
End If
Loop
line9:
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
line15:
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
Range("AA1").Value = 1
End Sub
Private Sub CommandButton3_Click()
Unload Me
Exit Sub
End Sub
Private Sub CommandButton4_Click()
numberholder = Range("E1").Value
Dim Point As POINTAPI
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Press Yes when you are ready to do a left click.", vbYesNo)
If answer = 6 Then
Else
GoTo line15:
End If
Do Until GetAsyncKeyState(VK_F9)
'exit when F9 key is pressed
DoEvents
If (GetAsyncKeyState(VK_LBUTTON) < 0) Then
GetCursorPos Point
X = Point.X
Y = Point.Y
Range("F" & numberholder).Value = X
Range("G" & numberholder).Value = Y
Range("Z" & numberholder).Value = 3
keybd_event VK_LBUTTON, 1, keyeventf_keyup, 0
GoTo line9:
End If
Loop
line9:
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
line15:
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
Range("AA1").Value = 1
End Sub
Private Sub CommandButton5_Click()
MsgBox ("The text you entered in the textbox was saved in column U in excel.")
numberholder = Range("E1").Value
Dim Point As POINTAPI
Range("U" & numberholder).Value = TextBox1.Value
answer = MsgBox("Press yes when ready to Left click where you want the text to be input from the textbox", vbYesNo)
Do Until GetAsyncKeyState(VK_F9)
'exit when F9 key is pressed
DoEvents
If (GetAsyncKeyState(VK_LBUTTON) < 0) Then
GetCursorPos Point
X = Point.X
Y = Point.Y
Range("F" & numberholder).Value = X
Range("G" & numberholder).Value = Y
Range("Z" & numberholder).Value = 12
keybd_event VK_LBUTTON, 1, keyeventf_keyup, 0
GoTo line9:
End If
Loop
line9:
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
line15:
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
Range("AA1").Value = 1
End Sub
Private Sub ListBox1_Click()
numberholder = Range("E1").Value
If ListBox1.Value = "Select Browser Search Address Bar" Then
Range("U" & numberholder).Value = "(""%D"")"
Range("Z" & numberholder).Value = 4
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
End If
If ListBox1.Value = "Refresh Browser F5" Then
Range("U" & numberholder).Value = "({F5})"
Range("Z" & numberholder).Value = 5
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
End If
If ListBox1.Value = "Copy" Then
Range("U" & numberholder).Value = "(""^c"")"
Range("Z" & numberholder).Value = 6
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
End If
If ListBox1.Value = "Paste" Then
Range("U" & numberholder).Value = "(""^v"")"
Range("Z" & numberholder).Value = 7
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
End If
If ListBox1.Value = "Copy Text between 2 clicks" Then
Range("z" & numberholder).Value = 10
Range("X" & numberholder).Value = 7
Call CommandButton1_Click
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
line15:
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
Range("x" & numberholder).Value = ""
End If
If ListBox1.Value = "Delete Already Selected Text" Then
Range("Z" & numberholder).Value = 14
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
End If
If ListBox1.Value = "Fill list of information from Column A into a place that you click" Then
Dim Point As POINTAPI
Range("Z" & numberholder).Value = 13
answer = MsgBox("Press yes when ready to Left click where you want column A to be input from Excel", vbYesNo)
Do Until GetAsyncKeyState(VK_F9)
'exit when F9 key is pressed
DoEvents
If (GetAsyncKeyState(VK_LBUTTON) < 0) Then
GetCursorPos Point
X = Point.X
Y = Point.Y
Range("F" & numberholder).Value = X
Range("G" & numberholder).Value = Y
Range("Z" & numberholder).Value = 13
keybd_event VK_LBUTTON, 1, keyeventf_keyup, 0
GoTo line9:
End If
Loop
line9:
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
End If
If ListBox1.Value = "Delete text between 2 clicks (like PDF Page)" Then
Range("z" & numberholder).Value = 15
Range("X" & numberholder).Value = 7
Range("Az1").Value = 1
Call CommandButton1_Click
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
Range("x" & numberholder).Value = ""
End If
If ListBox1.Value = "Copy Text between 2 clicks into Excel" Then
Range("z" & numberholder).Value = 11
Range("X" & numberholder).Value = 7
Range("Az1").Value = 1
Call CommandButton1_Click
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
Range("x" & numberholder).Value = ""
End If
If ListBox1.Value = "Enter" Then
Range("U" & numberholder).Value = "(""~"")"
Range("Z" & numberholder).Value = 8
Sleep 1000
AppActivate Application.Caption
DoEvents
answer = MsgBox("Do you want to do another action?", vbYesNo)
If answer = 6 Then
Range("L1").Value = 1
numberholder = numberholder + 1
Unload Me
Call Module1.Form_KeyDown
Else
Range("L1").Value = 2
numberholder = numberholder + 1
Unload Me
Exit Sub
End If
End If
Range("AA1").Value = 1
End Sub
Code for Module 2:
Private Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwflags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwextrainfo As LongPtr)
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Private Const VK_LBUTTON = &H1
Private Const VK_F9 = &H78
Private Const keyeventf_keyup = 2
Public Declare PtrSafe Sub keybd_event Lib "user32" (ByVal BYK As Byte, ByVal bscan As Byte, dwflags As Long, ByVal dwextrainfo As Long)
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public numberholder As Double
Public locations As Double
Private Type POINTAPI
X As Long
Y As Long
End Type
Sub mm()
numberholder = 1
'column Z is an index for actions
numberholder2 = Range("e1").Value
Do Until numberholder > numberholder2
caseselector = Range("Z" & numberholder).Value
Select Case caseselector
Case 1
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 1000
Case 2
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 1000
Case 3
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Application.SendKeys ("~")
Application.SendKeys ("{NUMLOCK}")
Sleep 1000
Case 4
Application.SendKeys ("%D")
Application.SendKeys ("{NUMLOCK}")
Sleep 1000
Case 5
Application.SendKeys ("{F5}")
Application.SendKeys ("{NUMLOCK}")
Sleep 1000
Case 6
keyholder = Range("U" & numberholder).Value
Application.SendKeys ("^c")
Application.SendKeys ("{NUMLOCK}")
Sleep 1000
Case 7
keyholder = Range("U" & numberholder).Value
Application.SendKeys& keyholder
Application.SendKeys ("{NUMLOCK}")
Sleep 1000
Case 8
keyholder = Range("U" & numberholder).Value
Application.SendKeys& keyholder
Application.SendKeys ("{NUMLOCK}")
Sleep 1000
Case 9
Application.SendKeys ("~")
Application.SendKeys ("{NUMLOCK}")
Sleep 1000
Case 10
Range("J" & numberholder).Value = Range("h" & numberholder).Value - Range("f" & numberholder).Value
Range("K" & numberholder).Value = Range("i" & numberholder).Value - Range("g" & numberholder).Value
plop = Range("J" & numberholder).Value
plop2 = Range("K" & numberholder).Value
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_MOVE, plop, plop2, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Application.SendKeys ("^c")
Sleep 1000
Application.SendKeys ("{NUMLOCK}")
X = Range("AC" & numberholder).Value
Y = Range("AD" & numberholder).Value
SetCursorPos X, Y
Sleep 1000
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 1000
Application.SendKeys ("^v")
Sleep 1000
Application.SendKeys ("{NUMLOCK}")
Sleep 1000
Case 11
Range("J" & numberholder).Value = Range("h" & numberholder).Value - Range("f" & numberholder).Value
Range("K" & numberholder).Value = Range("i" & numberholder).Value - Range("g" & numberholder).Value
plop = Range("J" & numberholder).Value
plop2 = Range("K" & numberholder).Value
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_MOVE, plop, plop2, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 1000
Application.SendKeys ("^c")
Sleep 1000
Application.SendKeys ("{NUMLOCK}")
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
strpaste = DataObj.GetText(1)
Range("AH" & numberholder).Value = strpaste
Case 12
Range("J" & numberholder).Value = Range("h" & numberholder).Value - Range("f" & numberholder).Value
Range("K" & numberholder).Value = Range("i" & numberholder).Value - Range("g" & numberholder).Value
plop = Range("J" & numberholder).Value
plop2 = Range("K" & numberholder).Value
Range("U" & numberholder).Select
Selection.Copy
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
DataObj.GetText (1)
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 1000
Application.SendKeys ("^v")
Sleep 1000
Application.SendKeys ("{NUMLOCK}")
Case 13
columnacounter = 1
erow = Range("A" & Rows.Count).End(xlUp).Row
Do Until columnacounter > erow
Range("a" & columnacounter).Select
Selection.Copy
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
DataObj.GetText (1)
X = Range("F" & numberholder).Value
Y = Range("G" & numberholder).Value
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sleep 1000
Application.SendKeys ("^v")
Sleep 1000
Application.SendKeys ("{NUMLOCK}")
columnacounter = columnacounter + 1
Loop
Case 14
Application.SendKeys ("{DELETE}")
Application.SendKeys ("{NUMLOCK}")
End Select
numberholder = numberholder + 1
Loop
line17:
line33:
skip = 1
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
In the Visual Basic Editor, which I teach how to access in this article: https://vbatutorialcode.com/visual-basic-editor/ you can click the below Insert button to insert a Module and to insert a Userform, which are both highlighted yellow in the Project Explorer, which can be accessed by pressing View – Project Explorer. You can change the name of the module and the Userform by pressing View – Properties Window and changing the (Name). In my module code, I reference Userform1.show, so if you change the name of the Userform, then my code won't work anymore.
If you double-left click on the Userform1 you'll see the below screen. You can drag the bottom right to make the Userform bigger. I highlighted yellow in the properties where you can change your Userform Name. Don't change it. You can also change the color of it.
If you click View –> Toolbox after clicking on the Userform1 in the Project Explorer:
Then you'll see the below toolbox. This has a series of ActiveX objects that you can attach to the Userform. If you hover your mouse over them, you can find out which each is called. My favorites are command buttons (to perform an action), labels (text that is used to describe areas without a function), listbox (to store multiple values to be selected by the user), checkbox which is a Boolean True or False if checked an action can be done a different way, images, multiple pages, and comboboxes. For the purpose of this quick article, I'll currently only explain a few.
Command Button: The below are two examples of command buttons that were added to a userform.
After drawing them onto the Userform from the Toolbox, you can double left click them to enter the code for the ActiveX object that you click on.
The above is the screen that happens when you double left click on the "Click here for Left Click" ActiveX object on my Userform. From the Properties Window if you single left click on the "Click Here for Left Click" you can see the name of the ActiveX button that you single left-clicked on. You can also see that same name in the image above near the arrow to the left. The arrow to the right "Click" is called an Event which happens when that selected dropdown box action occurs to the ActiveX object. So when you click on the CommandButton1 ActiveX object, it executes all the code under Private Sub CommandButton1_Click( ). Obviously, the name of the subprocedure changes based on the event that you select. You can do this at a workbook level in excel too to execute code when a workbook is opened or changed, but that's another topic entirely.
I basically pass the code from Module 1 to the Userform back to back to record all the coded actions and then execute the recorded actions back to back when you say that you're done. All VBA code runs from top to bottom.
Listbox
In order to add values to a listbox, you click the listbox that you drew on the userform once. In the properties window, you type out a specific word as highlighted yellow below:
Now to make this above RowSource work, go to the workbook, highlight an unused column, and type text as shown below and press Enter:
Now when you click this dropdown, it lets you select the range that you had highlighted when you named it by selecting it from here:
This above name is the name that I typed next to RowSource. Any values that I type in this highlighted range will be included in the Listbox.
Another sidenote on naming highlighted cells is that you can click Formulas on the Excel workbook Ribbon and click Name Manager:
You can then click edit to adjust the range for this name. I like to combine this with another lesson below. Click a random cell that you want to have a single value that can be selected from a list of values. Press Data – Data validation – data validation:
select list
Click this arrow to highlight a range and fill this list with selectable values for the cell:
Now when you press ok and click on the down arrow next to the cell, you can select a value from the list dropdown that gets put in the value.