Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-11-2024, 11:02 PM
Aengus345 Aengus345 is offline 1. Apply to current row.   2. De-select row afterwards. Windows 10 1. Apply to current row.   2. De-select row afterwards. Office 97-2003
Novice
1. Apply to current row.   2. De-select row afterwards.
 
Join Date: Feb 2024
Posts: 2
Aengus345 is on a distinguished road
Question 1. Apply to current row. 2. De-select row afterwards.

Hello! New to the forum. Thank you!



I have been trying to finish the macro as seen below. However, the last 2 things I just can't seem to figure out.

Code:
Sub TblCellShadeOf_TEN_Percent()
'
' ------------------------------------
    Dim tbl As table
    Set tbl = ActiveDocument.Tables(1)
    tbl.Rows(6).Select
' ------------------------------------
    
    With Selection.Cells
        With .Shading
             .Texture = wdTextureNone
             .ForegroundPatternColor = wdColorAutomatic
             .BackgroundPatternColor = wdColorGray10
        End With
    End With
End Sub
1. I can't figure out how to change syntax of [tbl.Rows(6).Select] to just something that represents the CURRENT ROW (the one the cursor is on). This code, tbl.Rows(6).Select, does work on this particular table, but it won't work if the table row changes location. I'd rather just use something that represents the current row.

2. How to de-select the row after the shading is applied. Once the shading is done, the row _is_ shaded, but I'd rather the current row have the 10% shading applied without showing it still selected in the end. I'm sure it's represented by something in the code above, but I'm not very good at vba, so can't figure out what code does that (I know, I know ... dumb ...

Thank you very much in advance for any help in this regard!

Reply With Quote
  #2  
Old 02-12-2024, 02:05 AM
Italophile Italophile is offline 1. Apply to current row.   2. De-select row afterwards. Windows 11 1. Apply to current row.   2. De-select row afterwards. Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

In Word, whilst there is an open document there is always a selection, as the cursor must always be somewhere in the document.

If you select something in code and don't want that to be selected when your code has finished, you must move the selection somewhere else. A common way to do this is to record what was selected at the start and re-select it at the end. For example:
Code:
    Dim initSel As Range: Set initSel = Selection.Range
    'working code
    initSel.Select
However, a better approach is to avoid using Selection in your code and work with the objects directly. There are very few occasions where Selection has to be used. Mostly you would work with the Range object instead.

For the specific case in your question it is not necessary to select the entire row. To prevent errors, your code should first check that the selection is inside a table. Then you can access the row and its cells from the selection, as below.
Code:
Sub TblCellShadeOf_TEN_Percent()
    If Selection.Information(wdWithInTable) Then
        With Selection.Rows(1).Cells
            With .Shading
                .Texture = wdTextureNone
                .ForegroundPatternColor = wdColorAutomatic
                .BackgroundPatternColor = wdColorGray10
            End With
        End With
    End If
End Sub
Reply With Quote
  #3  
Old 02-12-2024, 05:37 AM
Aengus345 Aengus345 is offline 1. Apply to current row.   2. De-select row afterwards. Windows 10 1. Apply to current row.   2. De-select row afterwards. Office 97-2003
Novice
1. Apply to current row.   2. De-select row afterwards.
 
Join Date: Feb 2024
Posts: 2
Aengus345 is on a distinguished road
Question [RESOLVED] 1. Apply to current row. 2. De-select row afterwards.

Quote:
Originally Posted by Italophile View Post
In Word, whilst there is an open document there is always a selection, as the cursor must always be somewhere in the document.

If you select something in code and don't want that to be selected when your code has finished, you must move the selection somewhere else. A common way to do this is to record what was selected at the start and re-select it at the end. For example:
Code:
    Dim initSel As Range: Set initSel = Selection.Range
    'working code
    initSel.Select
However, a better approach is to avoid using Selection in your code and work with the objects directly. There are very few occasions where Selection has to be used. Mostly you would work with the Range object instead.

For the specific case in your question it is not necessary to select the entire row. To prevent errors, your code should first check that the selection is inside a table. Then you can access the row and its cells from the selection, as below.
Code:
Sub TblCellShadeOf_TEN_Percent()
    If Selection.Information(wdWithInTable) Then
        With Selection.Rows(1).Cells
            With .Shading
                .Texture = wdTextureNone
                .ForegroundPatternColor = wdColorAutomatic
                .BackgroundPatternColor = wdColorGray10
            End With
        End With
    End If
End Sub
Thank you! This code works like a charm!

And thank you for the lesson! We VBA-seemingly-forever-newbs get far recording our actions, but don't always know how to fix things so the code is nice and neat and efficient!

Thank you!
Reply With Quote
  #4  
Old 02-12-2024, 06:28 AM
Italophile Italophile is offline 1. Apply to current row.   2. De-select row afterwards. Windows 11 1. Apply to current row.   2. De-select row afterwards. Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

You're welcome.

Recording a macro can be a useful step in discovering the syntax for an action, but the code produced, if any, will be of inferior quality.

The VBA editor has some tools that can help you learn and which are only a keypress away.
  • F1, when the cursor is in a keyword, brings up the online documentation.
  • F2 displays the Object Browser which can help you to discover the objects, their methods, properties, and events, that you can work with. You can either browse the object tree or use the find button to discover potentially useful properties or methods. In the 30+ years that I have been writing VBA this has always been the most helpful tool for me.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to apply Heading1 to select rows bobk544 Word VBA 3 06-09-2018 05:24 AM
VBA Word Table - Select More than 1 Column at a time - Apply Formatting jc491 Word VBA 12 09-24-2015 06:03 AM
1. Apply to current row.   2. De-select row afterwards. How to select the first row of the current table Jennifer Murphy Word VBA 9 01-29-2012 06:50 PM
1. Apply to current row.   2. De-select row afterwards. How to call current PC date and/or current PC year KIM SOLIS Excel 2 11-04-2011 06:09 PM
1. Apply to current row.   2. De-select row afterwards. Auto insert current month's name and current year Styler001 Word 4 01-25-2010 06:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:46 AM.


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