Excel VBA highlight current line of code

Source: Internet
Author: User

Code : Copy code The Code is as follows: private sub worksheet_selectionchange (byval target as range)
Thisworkbook. Names. Add "XM", Target
End sub

these lines of code define the cell (or region) as the name "XM" when selecting a new cell ", you can reference this name in the condition format settings.
paste the Code in the following attachment, return to the worksheet, select A4: i15, click format-condition format in the menu, and set:
Formula 1: = (A4 <> "") * (A4 = XM)
(if A4 is not empty and A4 is equal to XM)
formula 2: = row () = row (XM)
(if the current row number is the same as the XM row number)
use the code to define the name. We need to update it in real time, the name "XM" is updated.
for example, when you click the F7 cell, XM is equal to F7, row (XM) = 7. In the condition format area () if the value is 7 (7th rows), the background color is displayed.
You can also obtain the same effect by manually defining it. You can try it. Then compare the benefits of using code.
when cells B6 are selected, they may be different because two condition formats are set in the region. When the first condition is met, the first format is displayed, the second format is displayed if the second condition is met.
click format-condition format to view details.
is there a problem for beginners? Can you make the highlighted cell into an auto-starting macro so that the user can click a cell in the worksheet and the corresponding row and column will display one or two background colors!
Use VBA to set the background color as follows:
right-click the worksheet label, select "view code", and paste the following code at the cursor:
code: copy Code the code is as follows: private sub worksheet_selectionchange (byval target as range)
dim RNG as range
set RNG = target. range ("A1")
cells. interior. colorindex = 0' clear all background colors
RNG. entirecolumn. interior. colorindex = 40' set the color of the current column
RNG. entirerow. interior. colorindex = 36 'set the color of the current row
end sub

where 40 and 36 are color index numbers, different values represent different colors. The corresponding indexes are as follows:
if each worksheet in the workbook has the same effect, what should I do?
press Alt + F11 to open the VBE editor. In the Project Manager, double-click the module "thisworkbook" and paste the following code at the cursor:
code: copy Code the code is as follows: private sub workbook_sheetselectionchange (byval sh as object, byval target as range)
dim RNG as range
set RNG = target. range ("A1")
cells. interior. colorindex = 0' clear all background colors
RNG. entirecolumn. interior. colorindex = 40' set the color of the current column
RNG. entirerow. interior. colorindex = 36 'set the color of the current row
end sub

after this code is used, the "copy" and "copy" functions in the table are disabled. Is there any solution?
you can insert a line of code in the second line of the Code (before the color is cleared):
If application. cutcopymode then exit sub 'exit the Program
code: copy Code the code is as follows: private sub workbook_sheetselectionchange (byval sh as object, byval target as range)
If application. cutcopymode then exit sub 'exit the program if it is selected
dim RNG as range
set RNG = target. range ("A1")
cells. interior. colorindex = 0' clear all background colors
RNG. entirecolumn. interior. colorindex = 40' set the color of the current column
RNG. entirerow. interior. colorindex = 36 'set the color of the current row
end sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.