Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-28-2018, 07:00 AM
lucato lucato is offline VBA highlight the entire row issue Windows 7 64bit VBA highlight the entire row issue Office 2010 64bit
Novice
VBA highlight the entire row issue
 
Join Date: Apr 2018
Location: Brazil
Posts: 6
lucato is on a distinguished road
Default VBA highlight the entire row issue

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

Reply With Quote
  #2  
Old 04-28-2018, 08:43 AM
Logit Logit is offline VBA highlight the entire row issue Windows 10 VBA highlight the entire row issue Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

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
Reply With Quote
  #3  
Old 04-28-2018, 01:40 PM
lucato lucato is offline VBA highlight the entire row issue Windows 7 64bit VBA highlight the entire row issue Office 2010 64bit
Novice
VBA highlight the entire row issue
 
Join Date: Apr 2018
Location: Brazil
Posts: 6
lucato is on a distinguished road
Default

Quote:
Originally Posted by Logit View Post
Code:
Option Explicit ...
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.
Reply With Quote
  #4  
Old 04-28-2018, 02:36 PM
Logit Logit is offline VBA highlight the entire row issue Windows 10 VBA highlight the entire row issue Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

Please post a sample workbook so I know I'm working with the same thing as you have there.
Reply With Quote
  #5  
Old 04-28-2018, 03:02 PM
lucato lucato is offline VBA highlight the entire row issue Windows 7 64bit VBA highlight the entire row issue Office 2010 64bit
Novice
VBA highlight the entire row issue
 
Join Date: Apr 2018
Location: Brazil
Posts: 6
lucato is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 04-28-2018, 04:28 PM
Logit Logit is offline VBA highlight the entire row issue Windows 10 VBA highlight the entire row issue Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

.
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
Attached Files
File Type: xlsm worbook-highlight-test.xlsm (18.7 KB, 13 views)
Reply With Quote
  #7  
Old 04-29-2018, 12:18 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is offline VBA highlight the entire row issue Windows 7 64bit VBA highlight the entire row issue Office 2010 64bit
Expert
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,920
Pecoflyer has a reputation beyond reputePecoflyer has a reputation beyond reputePecoflyer has a reputation beyond reputePecoflyer has a reputation beyond reputePecoflyer has a reputation beyond reputePecoflyer has a reputation beyond reputePecoflyer has a reputation beyond reputePecoflyer has a reputation beyond reputePecoflyer has a reputation beyond reputePecoflyer has a reputation beyond reputePecoflyer has a reputation beyond repute
Default

@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
Reply With Quote
  #8  
Old 04-29-2018, 03:12 AM
lucato lucato is offline VBA highlight the entire row issue Windows 7 64bit VBA highlight the entire row issue Office 2010 64bit
Novice
VBA highlight the entire row issue
 
Join Date: Apr 2018
Location: Brazil
Posts: 6
lucato is on a distinguished road
Default

Quote:
Originally Posted by Logit View Post
.
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:
Hey Logit, thanks a lot. That is the BCEFI (best code ever found in Internet) :0) It really worked for me and will work for the world. God bless you. I really appreciated that. This one doesn't mess any format (border, fill, fonts, anything) and better, it marks lines when you select a range. AMAZING! I have no words to say thank you for you help.

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.
Attached Files
File Type: xlsm worbook-highlight-test.xlsm (19.1 KB, 11 views)
Reply With Quote
  #9  
Old 04-29-2018, 03:15 AM
lucato lucato is offline VBA highlight the entire row issue Windows 7 64bit VBA highlight the entire row issue Office 2010 64bit
Novice
VBA highlight the entire row issue
 
Join Date: Apr 2018
Location: Brazil
Posts: 6
lucato is on a distinguished road
Default

Quote:
Originally Posted by Pecoflyer View Post
@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
Hi Pecoflyer, nice to meet you. Thanks for the tip and I'm sorry for the little mess. As a newbie I didn't find how to attach and didn't find the correct forum. Thanks for moving it. Next time I'll pay more attention.
Have a nice day.
Reply With Quote
  #10  
Old 04-29-2018, 09:23 AM
Logit Logit is offline VBA highlight the entire row issue Windows 10 VBA highlight the entire row issue Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

.
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.
Reply With Quote
  #11  
Old 04-29-2018, 02:47 PM
lucato lucato is offline VBA highlight the entire row issue Windows 7 64bit VBA highlight the entire row issue Office 2010 64bit
Novice
VBA highlight the entire row issue
 
Join Date: Apr 2018
Location: Brazil
Posts: 6
lucato is on a distinguished road
Default

Quote:
Originally Posted by Logit View Post
.
I don't have an answer for FREEZE PANES. Sorry.
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:
Originally Posted by Logit View Post
.
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.
- Yes, thanks, that was what I said previously with "... 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 (+). "

Quote:
Originally Posted by Logit View Post
I can't take credit for the code as it is something I picked up somewhere but don't recall where.
- I see, thanks anyway for sharing what you found. I think I found it later and it was here by Philip.

If I find something regarding the "green" line as background, I'll share here too.

All the best to all.
Reply With Quote
Reply

Tags
highligh, highlight row, vba code



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA highlight the entire row issue highlight the entire row of current cell Joanne Excel 32 05-20-2017 07:30 AM
VBA highlight the entire row issue issue with highlight macro word 2010 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
VBA highlight the entire row issue Excel 2007, highlight entire row based on data found in one cell 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:48 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft