![]() |
#1
|
|||
|
|||
![]()
Hi folks, I was looking for how to highlight the entire row and found in this forum the VBA code bellow. The problem with such code is that it messed (removed) the all styles in my spreadsheet. So, I've lost all columns styles I had set. I know there is the option to SHIF+SPACE to select the entire row, but does anybody know how to have a code that doesn't remove the colors from columns and so on, so do not mess the style of a spreadsheet.? Thanks.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Cells.Count > 1 Then Exit Sub Application.ScreenUpdating = False ' Clear the color of all the cells Target.Parent.Cells.Interior.ColorIndex = 0 With Target ' Highlight the entire row and column that contain the active cell .EntireRow.Interior.ColorIndex = 8 End With Application.ScreenUpdating = True End Sub |
#2
|
|||
|
|||
![]() Code:
Option Explicit Dim rng As Range, ray Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim Dn As Range, c As Long, col As Long col = 100 If Not rng Is Nothing Then c = 0 For Each Dn In rng c = c + 1 Dn.Interior.Color = ray(c, 1) Next Dn End If c = 0 Set rng = Cells(Target.Row, 1).Resize(, col) ReDim ray(1 To rng.Count, 1 To 2) For Each Dn In rng c = c + 1 ray(c, 1) = Dn.Interior.Color Next Dn Cells(Target.Row, 1).Resize(, col).Interior.ColorIndex = 15 End Sub |
#3
|
|||
|
|||
![]()
Hey Logit, thanks, almost there. Thanks for the code above, but it still mess the styles. It really doesn't clean the columns, but it still cleans the rows styles. As a test, create a blank spreadsheet insert a table and select the style in the design, and you will see when selecting a row it will clean the color and the gray grid (not borders). Test and see what I'm talking about. If I try to recreate the design style over the cells were selected previously, the style isn't applied.
Have a great weekend. |
#4
|
|||
|
|||
![]()
Please post a sample workbook so I know I'm working with the same thing as you have there.
|
#5
|
|||
|
|||
![]()
Hi Logit, here is the workbook. Just click anywhere to see what I mean.
Workbook: https://www.dropbox.com/s/xkytmsz9e2...test.xlsm?dl=0 Printscreen: https://www.dropbox.com/s/wumqpjpr5r...tscrn.JPG?dl=0 Thanks for a while. |
#6
|
|||
|
|||
![]()
.
This should work for you. The macro is pasted in ThisWorkbook module so it affects all sheets in the workbook. If you want it for only one sheet, you can move the macro into that sheets module: Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Dim RowShape As Shape ' ************************************************ ' Check if entire rows are selected ' If they are then hide the shapes ' ************************************************ If Target.Address = Selection.EntireRow.Address Then 'If error occurs because shape does not exist, ignore the error On Error Resume Next Sh.Shapes("SelectedRow").Visible = msoFalse 'Return error handling to Excel On Error GoTo 0 Exit Sub End If If Target.Address = Selection.EntireColumn.Address Then 'If error occurs because shape does not exist, ignore the error On Error Resume Next Sh.Shapes("SelectedCol").Visible = msoFalse Sh.Shapes("SelectedRow").Visible = msoFalse 'Return error handling to Excel On Error GoTo 0 Exit Sub End If ' Set RowShape to be the SelectedRow On Error Resume Next Set RowShape = Sh.Shapes("SelectedRow") On Error GoTo 0 'If RowShape doesn't exist, then create it If RowShape Is Nothing Then Sh.Shapes.AddShape(msoShapeRectangle, 1, 1, 1, 1).Select With Selection.ShapeRange .Fill.Visible = msoFalse ' Remove any fill color .Name = "SelectedRow" .Line.Weight = 2 ' Set line thickness e.g. 1, 1.5, 2 etc .Line.ForeColor.RGB = RGB(146, 208, 80) ' Light Green. 'Can use vbBlack, vbWhite, vbRed, vbGreen, vbBlue , vbYellow, vbMagenta, vbCyan 'DashStyle = msoLineDash ' Can use : msoLineSolid, msoLineSysDot, msoLineSysDash, msoLineDash, msoLineDashDot, msoLineLongDash, msoLineLongDashDot, msoLineLongDashDotDot ' Default is msoLineSolid and does not need to be specified End With End If ' ************************************************ ' Move the SelectedRow shape ' ************************************************ With Sh.Shapes("SelectedRow") .Visible = msoTrue 'Make sure it is visible, it may have been hidden by previous selection .Top = Target.Top .Left = ActiveWindow.VisibleRange.Left .Width = ActiveWindow.VisibleRange.Width .Height = Target.Height End With Target.Select End Sub |
#7
|
||||
|
||||
![]()
@lucato
Hi and welcome please post your sheets on the forum so that all members can access them ( click Go advanced - Manage attachments) Also, in the future please post in the correct forum. I will move it for you this time. Thank you
__________________
Using O365 v2503 - Did you know you can thank someone who helped you? Click on the tiny scale in the right upper hand corner of your helper's post |
#8
|
|||
|
|||
![]() Quote:
Just as a comment and if you want to burn your mind in programming a little more, just two issues found in this code: 1. See attachment, when freezing panes it mess the selection. Just creates the line on the visible area. It would be great if it was possible to make the row line in the whole line of the spreadsheet like when we do SHIFT+SPACE; 2. Not sure if it is possible, is to make the "Green" line to be on the back (behind) the selected cell, because the way it is now, it isn't possible to select/pick the fill handle (+) on the corner of the cell to do commands like repeat values by dragging or the increment values. Not big deal because there is a workaround by selecting the "green" line, moving it to the side and going back to the cell to click on the handle (+). But if it was possible to make the lines behind, the workaround wouldn't be necessary. Anyway, it works amazing. Thanks, thanks, thanks. Have a stunning Sunday. |
#9
|
|||
|
|||
![]() Quote:
Have a nice day. |
#10
|
|||
|
|||
![]()
.
I don't have an answer for FREEZE PANES. Sorry. Regarding the fill handle. Highlight a row by clicking in a cell. The row is now highlighted and you should see the fill handle in the cell you clicked on. Grab the fill handle and drag the green outline to another row. Now click on the cell you wish to use the fill handle by clicking once in that cell. You will find that the green highlight does not follow in this scenario. I can't take credit for the code as it is something I picked up somewhere but don't recall where. |
#11
|
|||
|
|||
![]()
I found it out, just to share just remove de .VisibleRange in the code at .left and .width also in the ".with =" replace the text to the value 100000 if you want to get a large size for the row selection instead of the size it shows with the code.
Quote:
Quote:
If I find something regarding the "green" line as background, I'll share here too. All the best to all. |
![]() |
Tags |
highligh, highlight row, vba code |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Joanne | Excel | 32 | 05-20-2017 07:30 AM |
![]() |
jamesnavoy | Word VBA | 2 | 01-29-2017 08:32 AM |
PDF Conversion Issue - Background Tiled Image Size Issue | dustpie | Word | 1 | 10-23-2016 07:33 PM |
![]() |
MSofficeBLUE | Excel | 2 | 10-15-2013 09:51 PM |
find - reading highlight - highlight all / highlight doesn't stick when saved | bobk544 | Word | 3 | 04-15-2009 03:31 PM |