AutoHotKey

EXCEL快速移动行列

2018-07-30  本文已影响4人  sixtyone

;功能:Excel快速移动行、列

;QQ:576642385(1799)

;时间:2018-7-30

;Capslock+鼠标左键:移动行

;Capslock+鼠标右键:移动列

$CapsLock::

  KeyWait, CapsLock

  If (A_PriorKey="CapsLock")

      SetCapsLockState, % GetKeyState("CapsLock","T") ? "Off":"On"

return

#If GetKeyState("CapsLock", "P") and WinActive("ahk_exe excel.exe")

;移动行Capslock+鼠标左键

LButton::

  excel :=Excel_Get() ;获取EXCEL对象

  addr :=excel.selection.address ;获取选择区域的地址:$A$1:$F$8  $1:$7

  If InStr(addr,",") ;是否有ctrl多选,如果有 则提示不执行

    {

      MsgBox,请选择连续区域

      return

    }

  ar1 :=excel.selection.row ;获取选区的行号

  arc :=excel.selection.rows.count ;获取选区的总行数

  Send,{Click} ;点击目标行

  Sleep,50

  ar2 :=excel.selection.row ;获取目标区域的行号

  If (ar2>=ar1) and (ar2<=ar1+arc-1) ;如果目标区域行号在源数据行中,则不执行

      return

  addr1 :=RegExReplace(addr,"[A-Z$]","") ;替换掉地址的字母及$符号,得到1、1:3、2:2

  try  ; 尝试执行的代码.

{

    excel.ActiveSheet.Rows(addr1).Cut ;剪切源地址所在行的数据

}

catch e  ; 处理由上面区块产生的首个错误/异常.

{

    MsgBox, 出错!`n已知会出错的原因:选区有合并单元格,或其他未知原因。

    Exit

}

  ;excel.ActiveSheet.Rows(addr1).Cut

  excel.ActiveSheet.Rows(ar2).Insert ;在目的行插入剪切的数据

  If ar2<%ar1% ;完成后offset到最终的选择区

      offsets :=ar2-ar1

  Else

      offsets :=ar2-ar1-arc

  excel.ActiveSheet.Range(addr).offset(offsets).Select ;完成后offset到最终的选择区

SetTimer,ToolTip1,-1

return

ToolTip1:

ToolTip,移动行

Sleep,400

ToolTip

return

;移动列Capslock+鼠标右键

RButton::

  excel :=Excel_Get() ;获取EXCEL对象

  addr :=excel.selection.address ;获取选择区域的地址:$A$1:$F$8  $A:$C

  If InStr(addr,",") ;是否有ctrl多选,如果有 则提示不执行

    {

      MsgBox,请选择连续区域

      return

    }

  ac1 :=excel.selection.column ;获取选区的列号

  acc :=excel.selection.columns.count ;获取选区的总列数

  Send,{Click} ;点击目标列

  Sleep,50

  ac2 :=excel.selection.column ;获取目标区域的列号

  If (ac2>=ac1) and (ac2<=ac1+acc-1) ;如果目标区域列号在源数据列中,则不执行

      return

  addr1 :=RegExReplace(addr,"[\d$]","") ;替换掉地址的字母及$符号,得到A, A:B

  try  ; 尝试执行的代码.

{

    excel.ActiveSheet.columns(addr1).Cut ;剪切源地址所在列的数据

}

catch e  ; 处理由上面区块产生的首个错误/异常.

{

    MsgBox, 出错!`n已知会出错的原因:选区有合并单元格,或其他未知原因。

    Exit

}

  ;excel.ActiveSheet.Rows(addr1).Cut

  excel.ActiveSheet.columns(ac2).Insert ;在目的列插入剪切的数据

  If ac2<%ac1% ;完成后offset到最终的选择区

      offsets :=ac2-ac1

  Else

      offsets :=ac2-ac1-acc

  excel.ActiveSheet.Range(addr).offset(0,offsets).Select ;完成后offset到最终的选择区

  SetTimer,ToolTip2,-1

return

ToolTip2:

ToolTip,移动列

Sleep,400

ToolTip

return

; Excel_Get by jethrow (modified)

; Forum:    https://autohotkey.com/boards/viewtopic.php?f=6&t=31840

; Github:  https://github.com/ahkon/MS-Office-COM-Basics/blob/master/Examples/Excel/Excel_Get.ahk

Excel_Get(WinTitle:="ahk_class XLMAIN", Excel7#:=1) {

    static h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")

    WinGetClass, WinClass, %WinTitle%

    if !(WinClass == "XLMAIN")

        return "Window class mismatch."

    ControlGet, hwnd, hwnd,, Excel7%Excel7#%, %WinTitle%

    if (ErrorLevel)

        return "Error accessing the control hWnd."

    VarSetCapacity(IID_IDispatch, 16)

    NumPut(0x46000000000000C0, NumPut(0x0000000000020400, IID_IDispatch, "Int64"), "Int64")

    if DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", -16, "Ptr", &IID_IDispatch, "Ptr*", pacc) != 0

        return "Error calling AccessibleObjectFromWindow."

    window := ComObject(9, pacc, 1)

    if ComObjType(window) != 9

        return "Error wrapping the window object."

    Loop

        try return window.Application

        catch e

            if SubStr(e.message, 1, 10) = "0x80010001"

                ControlSend, Excel7%Excel7#%, {Esc}, %WinTitle%

            else

                return "Error accessing the application object."

}

上一篇 下一篇

猜你喜欢

热点阅读