Application.OnKey快捷键失效:仅在Data工作表生效的代码放置咨询
快捷键无法触发宏的问题(仅需在指定工作表生效)
- 宏手动点击「运行」可正常工作,但Ctrl+Shift+I快捷键无法触发,此前尝试过同类问题的解决方案无效
- 宏是从其他工作簿复制到当前工作簿的,当前工作簿包含11个工作表(含ThisWorkbook共12个)及12个模块
- 需求:仅让该宏及其快捷键在名为Data的工作表中生效,不清楚代码应放置在何处
当前快捷键设置代码
Sub MacroShortcut() Applicaton.OnKey Key:="+^{I}", Procedure:=ThisWorkbook.Name & "CopyOnlyVisibleRowsInColumnsG_I_T" End Sub
需触发的宏代码
Sub CopyOnlyVisibleRowsInColumnsG_I_T() ' ' CopyOnlyVisibleRowsInColumnsG_I_T Macro ' Copy only visible rows in columns G, I, T into a new file and add no. 5 into last column. ' ' Shortcut key: Ctrl+Shift+I ' Dim startWs As Worksheet Dim endFile As Workbook Dim endWs As Worksheet Dim lastRowColB As Long Dim lastRowColG As Long Set startWs = ThisWorkbook.Worksheets("Data") Set endFile = Workbooks.Open("C:\Users\user_name\OneDrive\import.txt") Set endWs = Worksheets("import") 'clear all content in target workbook Cells.ClearContents 'set last row in column G lastRowColG = startWs.Cells(Rows.Count, "G").End(xlUp).Row startWs.Range("G1:G" & lastRowColG).SpecialCells(xlCellTypeVisible).Copy Destination:=endWs.Range("B:B") startWs.Range("I1:I" & lastRowColG).SpecialCells(xlCellTypeVisible).Copy Destination:=endWs.Range("A:A") startWs.Range("T1:T" & lastRowColG).SpecialCells(xlCellTypeVisible).Copy Destination:=endWs.Range("C:C") startWs.Range("T1:T" & lastRowColG).SpecialCells(xlCellTypeVisible).Copy Destination:=endWs.Range("D:D") 'convert text format to date format for columns C and D endWs.Range("C:C, D:D").NumberFormat = "dd.mm.yyyy" 'delete header from new worksheet endWs.Range("A1:E1").Select Selection.EntireRow.Delete 'adds 5 to column E according to the number of rows in column B lastRowColB = Cells(Rows.Count, "B").End(xlUp).Row Range("E1:E" & lastRowColB).Value = 5 'save new file endFile.Save End Sub
内容的提问来源于stack exchange,提问作者Cz_Libu
相关产品推荐
相关产品推荐

