English 中文(简体)
宏可以在 i 复制后更改单元格的颜色( CTRL+C)
原标题:macro to change the color of a cell after i copy it (CTRL+C)
  • 时间:2024-07-25 01:29:12
  •  标签:
  • excel
  • macros
i 做很多测试, 并使用Excel 中生成的条码列表 。 我复制条码 i 使用, 然后更改单元格的颜色, 以便知道哪些单元格被使用 。 当使用 Ctrl + C 复制时, 宏可以自动更改选定单元格/ 的颜色 。 我试图将宏和密钥绑到 Ctrl + C 上, 但是它会附带一个 Stack 空间错误 。 Sub 宏2() 宏2 宏键板快捷键 : Ctrl+c 范围 ("E3") 。 选择应用程序 。 Run “ LPN 数. xlsde! Macro2 ” 与选中范围 。 内部 pater = xSollid. partern Color Index = xautomatical. Theme cloor A. tand Shade = levelople lection it to ex ex forn.
问题回答
What you need I guess is to record the keys you pressed and to execute the cell fill if the pressed keys were Ctrl+c. To receive this, you can write a macro that watches out if you press this keys and if you do, to execute the fill. For recognizing the pressed keys, you can use the Windows API function GetAsyncKeyState. This function checks if a specific key is currently pressed or has been pressed since the last call to this function - so exactly what we need. The function returns a number different to zero if a key was pressed. To find out if the keys you look for were pressed you can just check, if the function returns zero or not. You can combine this via an And in an If statement, to check wether both conditions (Ctrl and C) are met. To especially check for Ctrl and C you need to specify this keys, using their Key Code (for Key Codes in general take a look at the Microsoft pages: Keycode constants). You can pack this into a nice litte loop, so that it will not stop after you pressed Ctrl+C once. Therefore you have to add DoEvents into the loop, what prevents Excel from being stuck in the loop and let s you perform other stuff meanwhile (user interaction in general but also other macros for example). If you don t put it in the loop, you won t even be able to click anywhere within the workbook. To exit the loop, you can again make use of GetAsyncKeyState and create the loop to stop if you hit the Escape key. Therefore you can use a Do loop, so that it will not stop, until you hit ESC. So put an additional If statement into the loop to check, if Escape was pressed and if so, exit the sub. The code could look like this: 1. At the top of a Module add the following to make use of the Windows API function (you may have to add a "PtrSafe" after "Declare", but your system will tell you): Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer 2. Into the Module write: Sub WatchOutForControlC() Do DoEvents If GetAsyncKeyState(vbKeyC) <> 0 And GetAsyncKeyState(vbKeyControl) <> 0 Then Selection.Interior.ThemeColor = xlThemeColorAccent4 If GetAsyncKeyState(vbKeyEscape) <> 0 Then Exit Sub Loop End Sub Please pay attention to not press Ctrl+C too quickly and to really push the keys down to the ground, to make sure the macro gets when you are copying! You could connect this macro to a button or shape on the worksheet, so that you can start it comfortably from the worksheet by clicking on the button or shape without having to open the VBE. For example: Add a shape to the workbook, right click on it, select "Assign Macro" and select the just created macro from the list, click Ok and afterwards you can call this macro by clicking on the shape. Let me know if this works.




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签