-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy_from_one_workbook_to_another.ahk
49 lines (35 loc) · 1.66 KB
/
Copy_from_one_workbook_to_another.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
; These are the two files that will be used.
Book1Path := A_ScriptDir "\Workbook1.xlsx"
Book2Path := A_ScriptDir "\Workbook2.xlsx" ; This workbook receives data when the Ctrl+1 hotkey is pressed.
; Create an instance of Excel.
xlApp := ComObjCreate("Excel.Application")
xlApp.Visible := true
; Open the two workbooks.
Book1 := xlApp.Workbooks.Open(Book1Path)
Book2 := xlApp.Workbooks.Open(Book2Path)
; Constants
xlUp := -4162
return ; End of Auto-execute section.
; Excel hotkeys
#IfWinActive, ahk_class XLMAIN
; Hotkey to copy the active cell to the next blank row in Book2. ActiveCell can be in either Book1 or Book2. (ie:
; 'ActiveCell' is a property of the Application object. Workbook and Worksheet objects do not have an 'ActiveCell'
; property.
^1:: ; Ctrl+1 hotkey.
; Find the next blank row in column A, sheet 1, of Book2. Start at the last cell in Column 'A' and look upwards for
; a non-blank cell. Then offset 1 down. (This essentially does the same thing as selecting the last cell in a column
; and then pressing the keys 'Ctrl+Up' followed by 'Down'.)
Cell := Book2.Sheets(1).Cells(xlApp.Rows.Count, 1).End(xlUp).Offset(1, 0)
Cell.Value := xlApp.ActiveCell.Value
return
; Copy data from cell 'B3', sheet 1, 'Book2' --> to --> cell 'A1', sheet 1, 'Book1'
^2:: ; Ctrl+2 hotkey
Book1.Sheets(1).Range("A1").Value := Book2.Sheets(1).Range("B3").Value
return
; Cut-and-paste cell 'C2', sheet 1, 'Book2' --> to --> cell 'A2', sheet 1, 'Book1'
^3:: ; Ctrl+3 hotkey
Book2.Sheets(1).Range("C2").Cut( Book1.Sheets(1).Range("A2") )
return
; Turn off context-sensitive hotkeys.
#If
Esc::ExitApp ; Press 'Escape' to exit this script.