Macro to apply alternate grey/white shading to selected paragraphs

Dear Experts:

I would like to do paragraph shading using grey shading alternately.

Example:

this is a sample text
This is another sample text
This is yet another sample text
This may be the last paragraph acquiring grey shading
This is the penultimate paragraph
This is the last paragraph

With these 6 paragraphs selected, I would like to run a macro which
applies grey shading alternately to the selected paragraphs, i.e. grey/
white (no shading) alternately.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
andreas
2/17/2010 3:23:03 PM
word.vba.general 1023 articles. 1 followers. Follow

4 Replies
1088 Views

Similar Articles

[PageSpeed] 21

Use

Dim oRng As Range
For i = 2 To ActiveDocument.Paragraphs.Count Step 2
    Set oRng = ActiveDocument.Paragraphs(i).Range
    oRng.Shading.BackgroundPatternColor = wdColorGray10
Next i

If you want the first row grey instead of the second, change to i = 1 etc.


-- 
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


"andreas" <andreas.hermle@gmx.de> wrote in message 
news:2db1a7ad-52c5-4252-a34f-4c6b14f03661@q16g2000yqq.googlegroups.com...
> Dear Experts:
>
> I would like to do paragraph shading using grey shading alternately.
>
> Example:
>
> this is a sample text
> This is another sample text
> This is yet another sample text
> This may be the last paragraph acquiring grey shading
> This is the penultimate paragraph
> This is the last paragraph
>
> With these 6 paragraphs selected, I would like to run a macro which
> applies grey shading alternately to the selected paragraphs, i.e. grey/
> white (no shading) alternately.
>
> Help is much appreciated. Thank you very much in advance.
>
> Regards, Andreas 


0
Graham
2/17/2010 3:34:24 PM
On 17 Feb., 16:34, "Graham Mayor" <gma...@REMOVETHISmvps.org> wrote:
> Use
>
> Dim oRng As Range
> For i =3D 2 To ActiveDocument.Paragraphs.Count Step 2
> =A0 =A0 Set oRng =3D ActiveDocument.Paragraphs(i).Range
> =A0 =A0 oRng.Shading.BackgroundPatternColor =3D wdColorGray10
> Next i
>
> If you want the first row grey instead of the second, change to i =3D 1 e=
tc.
>
> --
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Graham Mayor - =A0Word MVP
>
> My web sitewww.gmayor.com
> Word MVP web sitehttp://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
> "andreas" <andreas.her...@gmx.de> wrote in message
>
> news:2db1a7ad-52c5-4252-a34f-4c6b14f03661@q16g2000yqq.googlegroups.com...
>
>
>
> > Dear Experts:
>
> > I would like to do paragraph shading using grey shading alternately.
>
> > Example:
>
> > this is a sample text
> > This is another sample text
> > This is yet another sample text
> > This may be the last paragraph acquiring grey shading
> > This is the penultimate paragraph
> > This is the last paragraph
>
> > With these 6 paragraphs selected, I would like to run a macro which
> > applies grey shading alternately to the selected paragraphs, i.e. grey/
> > white (no shading) alternately.
>
> > Help is much appreciated. Thank you very much in advance.
>
> > Regards, Andreas- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Hi Graham,

thank you very much for your swift response. It works very well,
although I would like the macro to react slightly differently.

The whole paragraphs get shaded, i.e. all the way from the left page
margin to the right page margin.

But I would like the shading to span only from the first to the last
character of the text. And this text may be much shorter than the
printable area of the page (page width minus right page margin minus
left page margin.

I hope I could make myself clear. Thank you very much in advance for
your help.

Regards, Andreas
0
andreas
2/17/2010 5:11:22 PM
Try:

Sub ShadeEm()
Dim oPara As Paragraph
Dim j As Long
j = 1
For Each oPara In Selection.Paragraphs
   If j Mod 2 = 0 Then
      oPara.Range.HighlightColorIndex = wdGray25
   End If
   j = j + 1
Next
End Sub

As you only want the paragraphs selected, I used Selection.Paragraphs.  The
above shades (25%) every SECOND paragraph - using Mod 2 iterating through the
paragraph count of the Selection.  If you want it the other way (the first
paragraph gets shaded and then alternates after), change to:

   If j Mod 2 <> 0 Then


Gerry

andreas wrote:
>> Use
>>
>[quoted text clipped - 36 lines]
>>
>> - Zitierten Text anzeigen -
>
>Hi Graham,
>
>thank you very much for your swift response. It works very well,
>although I would like the macro to react slightly differently.
>
>The whole paragraphs get shaded, i.e. all the way from the left page
>margin to the right page margin.
>
>But I would like the shading to span only from the first to the last
>character of the text. And this text may be much shorter than the
>printable area of the page (page width minus right page margin minus
>left page margin.
>
>I hope I could make myself clear. Thank you very much in advance for
>your help.
>
>Regards, Andreas

-- 
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201002/1

0
Fumei2
2/17/2010 7:31:36 PM
On 17 Feb., 20:31, "Fumei2 via OfficeKB.com" <u53619@uwe> wrote:
> Try:
>
> Sub ShadeEm()
> Dim oPara As Paragraph
> Dim j As Long
> j =3D 1
> For Each oPara In Selection.Paragraphs
> =A0 =A0If j Mod 2 =3D 0 Then
> =A0 =A0 =A0 oPara.Range.HighlightColorIndex =3D wdGray25
> =A0 =A0End If
> =A0 =A0j =3D j + 1
> Next
> End Sub
>
> As you only want the paragraphs selected, I used Selection.Paragraphs. =
=A0The
> above shades (25%) every SECOND paragraph - using Mod 2 iterating through=
 the
> paragraph count of the Selection. =A0If you want it the other way (the fi=
rst
> paragraph gets shaded and then alternates after), change to:
>
> =A0 =A0If j Mod 2 <> 0 Then
>
> Gerry
>
>
>
>
>
> andreas wrote:
> >> Use
>
> >[quoted text clipped - 36 lines]
>
> >> - Zitierten Text anzeigen -
>
> >Hi Graham,
>
> >thank you very much for your swift response. It works very well,
> >although I would like the macro to react slightly differently.
>
> >The whole paragraphs get shaded, i.e. all the way from the left page
> >margin to the right page margin.
>
> >But I would like the shading to span only from the first to the last
> >character of the text. And this text may be much shorter than the
> >printable area of the page (page width minus right page margin minus
> >left page margin.
>
> >I hope I could make myself clear. Thank you very much in advance for
> >your help.
>
> >Regards, Andreas
>
> --
> Message posted via OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/wo=
rd-programming/201002/1- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Hi Fumei,

great, that did the trick. Thank you very much for your terrific
help.

Regards, Andreas
0
andreas
2/18/2010 7:13:50 AM
Reply:

Similar Artilces:

Trouble applying borders in Excel
Running Excel in Office 2003 on Windows XP Home I have a column with about 20 rows in it. I would like to place a border (black line) at the bottom of each cell. If I drag my mouse over all 20 cells, they highlight. I then go to format>cells>border and select the bottom line option. It applies it to only one of the 20 cells even though all were highlighted. If I click the first cell then Shift-Click the last cell, they all highlight but again using the format>>>> only one cell gets the bottom border. If I click on the first cell, then CTRL-click on each of the other 19...

spell check does not visually cycle through cells when run from macro?
I'm trying to recreate the same behavior in a macro that spellcheck has when the spellcheck button is pressed. The spellcheck does not cycle through the cells visually when run from a macro, so when there is a misspelled word, you cannot see the cell it is a part of. If anyone knows how I can get this behavior to work, it would be GREATLY appreciated. I did a record new macro, and got code similar to the following: Sub testSpelling() Range("F2:F500").Select Selection.CheckSpelling SpellLang:=1033 End Sub thanks, dan Dan, Add the fiollowing to your code as shown: ...

Setting directory for file save in macro
Hi Thanks to a generous poster I now have a great bit of code to use in a macro for Excel which saves a text file (I'm no VBA programmer so this was really helpful), i.e.: Open "Test.txt" For Output As #1 (rest of code) My problem now is this: The macro saves test.txt to Excel's current active folder, rather than the folder that the current workbook is contained in. For example, if I last saved an Excel workbook to c:\workbooks\, and the workbook that I have open exists in c:\workbooks\workbook1\, when I run the macro it saves the text file in c:\workbooks\. If I ...

Change Backcolor for the selected line
I would like to be able to highlight or change the color of a single line on a subform formated as a continuous form. The user selects one line from many possible lines on a continuous form to respond to. When he selects the line another pop-up form is display which the user must complete. I would like to maintain some kind of highlight (backcolor?) so that the user can clearly see which line he is reponding to. Setting the backcolor after selecting a line sets the back color for the entire form, which really defeats the purpose. Any help? -- Message posted via AccessMons...

Can you record a macro in Publisher like you can in Excel?
In Excel I use the feature to Record a Macro. Excel records the keystrokes and converts to VB. Does Publisher have that option? In my version, Publisher 2003, I can Create a Macro, and it takes me into VB. However, I don't know how to code VB. Darlene wrote: > In Excel I use the feature to Record a Macro. Excel records the keystrokes > and converts to VB. Does Publisher have that option? In my version, > Publisher 2003, I can Create a Macro, and it takes me into VB. However, I > don't know how to code VB. Publisher does not have a Macro Recorder, sorry. --...

Macros/Signatures Question
I have an Excel Workbook that I created for a PC Order Form. I set it up to only make certain rows visible based on the Machine Model that they choose. I digital signed the Worksheets and exported the Digital Signature Certificate and installed it on the User’s PCs. The code works great. The user opens the Workbook and it runs the Macros with no problem. The problem I have is when the user goes to save the Workbook. They get an error that “Excel can not sign VBA macros when saving to this file format. Do you want to remove the digital signature and continue saving this workbook?”...

Macro Copy and Paste
I am currently trying to copy from one workbook that has 7 differen tabs and paste into one worksheet on a separate master workbook. M problem is that when I set up the macro to copy from one of the tabs i the workbook and paste to the other work book, I only want the the row to come in where there is data. Currently I am having to run a macr that takes every 100 lines, but ideally I only want the informatio copy and pasted where there is only data. In addition, once I have copy and pasted everything from the first tab my next step is to copy and paste information from the second tab t th...

Referring to a FileName in Macro
I have a Macro with the following code Application.Run "TestQry!Macro2" Application.Run "TestQry!Macro1" TestQry is the name of the file. Is there anyway that this macro cod can refer to the activeWorkbook or filename so that if the file i saved to a new name this macro will still ru -- Message posted from http://www.ExcelForum.com How about just: macro2 or even call macro2 "DoctorV <" wrote: > > I have a Macro with the following code > > Application.Run "TestQry!Macro2" > Application.Run "TestQry!Macro1" > &g...

Macro Question #10
Hi, I am working with a text file that I imported into Excel. It is a transactional report of sales by customer. The data will have the customer number and name spread out over three cells. What I want to do is to create a macro that will contatenate all three cell and then perform a paste special and paste them into another cell in another column. Since the number of transactions by customer can vary the customer name and number can appear anywhere on the page. I created a macro to do this using the first customer which appears in cell B5 through D5. The next customer appears...

Multi-Select Listbox VBA code causing overflow in loop
Private Sub Form_Current() Dim oItem1 As Variant Dim oItem2 As Variant Dim bFound1 As Boolean Dim bFound2 As Boolean Dim sTemp1 As String Dim sTemp2 As String Dim sValue1 As String Dim sValue2 As String Dim sChar1 As String Dim sChar2 As String Dim iCount1 As Integer Dim iCount2 As Integer Dim iListItemsCount1 As Integer Dim iListItemsCount2 As Integer sTemp1 = Nz(Me!SignsList.Value, " ") sTemp2 = Nz(Me!SymptomsList.Value, " ") iListItemsCount1 = 0 iListItemsCount2 = 0 bFound1 = False bFound2 ...

Filter two columns with criterion applying to one or the other?
Hi, I am looking for a solution to the following filtering problem: I have two adjacent columns, so using a filter for both of them is no problem. But what I want to do and don't know how to do is this: I want to filter for values greater than x (a certain number, in my case 5000) in any of the two columns. I can filter both columns for x greater than 5000 but that filters out more than I want because there may be some cells with a value greater than 5000 in only one of the two columns. Is there a solution to this problem (using Excel alone or an add-on)? Peter Hi Peter you can use th...

Spreadsheet macro stopped working!
Hi, I recently made an Excel spreadsheet for keeping track of my golf score. The spreadsheet is quite simple. It consists of 4 sheets: Scorecard, statistics, database and equations (for calculating some specific). I insert informations about the golf course I'm playing and how my score was etc. I then hit a button "Save scorecard" that runs a macro. The macro inserts specifik values from the scorecard into the database sheet (which works like a charm). The macro also updates the "Statistics" sheet with information about stroke number and points for ...

Excel 2007 macro compatability
I have created a macro in Excel 2007 that includes importing a text file. It works fine on my PC, but fails on a co-workers PC running Excel 2000. The failure points to the "TextFile Platform" command in the macro. I have tried several (437, 1252, 20127), but get the same results. Any solution? Thanks you. Have you try xlWindows -- Regards Ron de Bruin http://www.rondebruin.nl/tips.htm "Al @ Frontier" <Al @ Frontier@discussions.microsoft.com> wrote in message news:3F601F80-F099-42C8-AA18-6D31A85946F9@microsoft.com... >I have cr...

Select more than 1 object in the lookup field
I would like to have the ability to select more than one object in a lookup field. Is there a way to accomplish this? Thanks No. The only exception to this is the party list (like the to, cc and bcc fields in an email), but this is an internal thing and cannot be used in customizations. -- Michael H�hne, Microsoft Dynamics CRM MVP Web: http://www.stunnware.com/crm2 Feed: http://www.stunnware.com/crm2/atom.aspx Custom Lookup Dialog: http://www.stunnware.com/crm2/?area=customLookup ---------------------------------------------------------- "Onetreeup" <Onetreeup@discussions...

Value shading map issue
I am creating a map in Excel using MS Map to show a US map. When using the value shading function, I can't figure out how to keep the states that have no values blank. What happens is the key shows a range of values (0- 10, 11-20). I need the states that have NO value to have their own color (or lack of color) and I am not sure how to do this... Any suggestions? Chris In your data, you could calculate the state name in a new column, and use the calculated name in the map. For example, if state names are in column D, and values are in column E: =IF(E2=0,"",D2) Th...

Excel E-mail Macros
I need to know how to create a macro that will send an excel file as an attachment to one individual, and then send one of the file's worksheets to 3 e-mail addresses using the mail recipient method so that the worksheet appears in the body of the e-mail. The e-mail program I am using is Outlook. Any help with this will be GREATLY appreciatd. Hi John, I'd tell you, but Ron de Bruin already did all the work. It'd be silly for me to do it again...Try Ron's site: http://www.rondebruin.nl/sendmail.htm tim "John" <jbegley@dcas.nyc.gov> wrote in message ne...

format macro
Last week, Art was trying to help me with this, but the solution doesn't work for me. I'm looking for a macro that would be contained within my personal.xls, that I could use as needed. Lets say I have A1:C5 selected, which represent headdings and data for jan, feb and march. [On a different spreadsheet, it may be different columns, or a different number of columns. The point is that I will select the range before I run the macro.] I want to do an auto fit for each column, so that the column widths are big enough to fit the numbers. No problem, format-->column-->autof...

granting users Send as permissions causes errors unless they select the FROM address from the GAL
I've got some users who have two mailboxes, each with a different address. They need to be able to select which address they send from, on occasion. I've given them "send on behalf" and "send as" permissions the 2nd accounts. But, unless they select the FROM address from the GAL, they get the following error: You do not have permission to send to this recipient. If they manually type in the address, they receive this error. This is only a problem because I would prefer to hide the 2nd account from the GAL, to avoid confusion. Does anyone know a way around this?...

Combining two types of functions. Select Query and DLookup
I would like to write a code that performs the following action: 1. If the value in a comboBox Project Number is 19912 2. Perform a select query on comboBox Task Number and comboBox National Site ID 3. If the value in a comboBox Project Number is not 19912 4. Perform a select query on comboBox Task Number and a DLookup () on comboBox National Site ID I tried the code below but it's not working. Any ideas on how to do this? If Me.cmbProjectNumber.Value="19912" then cmbNationalSiteID.RowSource = "SELECT DISTINCT [National Site ID] FROM [InScope Table] WHERE [Proje...

macro problems!
i have a check box whose control source is: =[Total In Stock]-[reorderlevel]<=0 how can i set the control to only be visible if this check box is true? tried macro and vba, but can't quite work it out. thanks in advance Paul ...

Unable to find FrxClient.cfg file, when applying FRx service pack.
When I try to apply a services apck for my FRx client i got this error " Unable to find FrxClient.cfg file. Setup cannot continue if this file is corrupted or not found. Setup will abort now." Please help! ...

Macro #17
Does anyone have a Macro to edit a cell to add "=" sign at the beginning of the data Thanks, Alexandra can you be more pacific ActiveCell.FormulaR1C1 = "=" Range(" ").Select Alexandra wrote: > > Does anyone have a Macro to edit a cell to add "=" sign at the beginning of the data? > > Thanks, Alexandra ...

Alternate grey/red shading with subtitles getting shaded differently (Word table)
Dear Experts: I got a telephone list/table with the following make-up: - Each name and corresponding phone number has got its own row. - The letters A-Z also have their own rows and are formatted bold - First name, last name is entered in one table cell - Phone numbers are entered in the table cells next to the "name" cell With any number rows of the table SELECTED would like the table to acquire the following FORMATTING: Rows A, B,C to Z (formatted bold) should have a grey shaded fill (RGB value 133, 133, 133) Rows immediately following these subtitles (A-Z) should...

Macros applied to all workbooks
Does anybody know how to set up macros that apply to all workbooks open in the future? I believe it has something to do with saving the macro on a worksheet that automatically opens up when you open Excel, however I am not sure. Any help would be greatly appreciated.\ Nick Hi Nick! Save or record the macro in your Personal.xls file. To see if you already have one, use Windows > Unhide > Take a look > Cancel If you see it there, you have it already If you haven't got one, use: Tools > Macro > Record new macro Select place for it to be recorded as Personal macro workbo...

shorter macro
is there a way to shorten this macro to make it run faster thanks JD Range("C:C,D:D,E:E,F:F,G:G,H:H,I:I").Select Range("I1").Activate ActiveWindow.SmallScroll ToRight:=6 Range("C:C,D:D,E:E,F:F,G:G,H:H,I:I,J:P").Select Range("J1").Activate ActiveWindow.SmallScroll ToRight:=4 Range("C:C,D:D,E:E,F:F,G:G,H:H,I:I,J:P,Q:Q,R:R,S:S,T:T,U:U").Select Range("U1").Activate ActiveWindow.SmallScroll ToRight:=7 Range("C:C,D:D,E:E,F:F,G:G,H:H,I:I,J:P,Q:Q,R:R,S:S,T:T,U:U,V:V,W:W,X:X").Select Range(&q...