Last active
September 10, 2021 11:05
-
-
Save JoeGlines/82928c0c5a2c47ca384fa72a75b12506 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;******************************************************* | |
; Want a clear path for learning AutoHotkey; Take a look at our AutoHotkey Udemy courses. They're structured in a way to make learning AHK EASY | |
; Right now you can get a coupon code here: https://the-Automator.com/Learn | |
;******************************************************* | |
XL:=XL_Handle(1) ;Get pointer to Excel | |
;***********call function******************* | |
loc:=XL_Find_Headers_in_Cols(XL,["email","country","Age"]) ;pass search terms as an array | |
MsgBox % "email: " loc["email"] . "`nCountry: " loc["country"] . "`nAge: " loc["Age"] | |
;********************search***Find columns based on header********************************. | |
XL_Find_Headers_in_Cols(PXL,Values){ | |
Headers:={} ;need to create the object for storing Key-value pairs of search term and Location | |
for k, Search_Term in Values{ | |
Loop, % XL_Last_Col_Nmb(PXL){ ;loop over all used columns | |
if (PXL.Application.ActiveSheet.cells(1,A_Index).Value=Search_Term) ;if cell in row 1, column A_index = search term | |
Headers[Search_Term]:=XL_Col_To_Char(A_Index) . "1" ;set column to value in Hearders object | |
}} return Headers ;return the key-value pairs Object | |
} | |
;******Get Handle to Excel************************ | |
XL_Handle(Sel){ | |
ControlGet, hwnd, hwnd, , Excel71, ahk_class XLMAIN ;identify the hwnd for Excel | |
Obj:=ObjectFromWindow(hwnd,-16) | |
return (Sel=1?Obj.Application:Sel=2?Obj.Parent:Sel=3?Obj.ActiveSheet:"") | |
} | |
;***borrowd & tweaked from Acc.ahk Standard Library*** by Sean Updated by jethrow***************** | |
ObjectFromWindow(hWnd, idObject = -4){ | |
if(h:=DllCall("LoadLibrary","Str","oleacc","Ptr")) | |
If DllCall("oleacc\AccessibleObjectFromWindow","Ptr",hWnd,"UInt",idObject&=0xFFFFFFFF,"Ptr",-VarSetCapacity(IID,16)+NumPut(idObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81,NumPut(idObject==0xFFFFFFF0?0x0000000000020400:0x11CF3C3D618736E0,IID,"Int64"),"Int64"), "Ptr*", pacc)=0 | |
Return ComObjEnwrap(9,pacc,1) | |
} | |
;***********Find last column******************* | |
XL_Last_Col_Nmb(PXL){ | |
Return, PXL.Application.ActiveSheet.UsedRange.Columns(PXL.Application.ActiveSheet.UsedRange.Columns.Count).Column | |
} | |
;***********************Numeric Column to string********************************. | |
;~ XL_Col_To_Char(26) | |
XL_Col_To_Char(index){ ;Converting Columns to Numeric for Excel | |
IfLessOrEqual,index,26, Return, (Chr(64+index)) | |
Else IfGreater,index,26, return, Chr((index-1)/26+64) . Chr(mod((index - 1),26)+65) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment