Programming in VBA the selection of multiple rows in a Word 2014 table - vba

I want to change the font of all even rows in a large table in Microsoft Word (most versions, I use 2014) to red
I tried a simple loop :
For ii=2 to ActiveDocument.Tables(1).Rows.Count step 2
ActiveDocument.Tables(1).Rows(ii).Select
Selection.Font.ColorIndex = wdRed
Next
This sometimes hangs, sometimes it works, but takes hours (my table has 14000 rows...)
Then I had the idea : Manually, I can select a row by left-clicking on its left, then add additional rows to the selection by Ctrl-left-click on their left.
And I can then modify the font of all rows selected at once.
So let's see if doing the same programmatically is faster ! I tried something like
ActiveDocument.Tables(1).Rows(2).Select
For ii=4 to ActiveDocument.Tables(1).Rows.Count step 2
Selection.Add (ActiveDocument.Tables(1).Rows(ii))
Next
Selection.Font.ColorIndex = wdRed
but Add is not accepted as a valid Selection object member
Can someone help there ?

define a new style and apply it to the table ... no vba needed
this is a macro recording of an example style change ... if you wish to use vba
Selection.Tables(1).Style = "Grid Table 5 Dark - Accent 2"
also, record a macro of doing a new style definition .... lots of good stuff in it

Related

How to fit column width in macro?

I wrote a macro which copies table from excel to word. In excel all columns have different width. In Word I want to fit my table to one page - margins in Word are 1.5 cm from left and right. Number of rows in my table changes, number of columns is stable (this is 14). How can I set column width to be equal? I wonder if it's possible to set the same column width regardless of the amount of text in headlines. I create swdth variable which I then divide by 14 (all my columns) and I have my table on one page...
This code doesn't work properly. I have all rows in one page, but columns have different width.
Table.Rows.SetHeight RowHeight:=InchesToPoints(0.17), HeightRule:=wdRowHeightExactly
Table.Rows(1).SetHeight RowHeight:=InchesToPoints(0.59), HeightRule:=wdRowHeightExactly
sWdth = InchesToPoints(6.22)
WordTable.PreferredWidthType = wdPreferredWidthPoints
WordTable.PreferredWidth = sWdth
sWdth = sWdth / 14
Giving all columns the same with is as simple as:
Table.Columns.DistributeWidth
but it's not apparent what this has to do with keeping the table on one page.
I've seen similar questions recently. As always...
#1) Turn on the Macro Recorder
#2) Click through the steps you need to perform
#3) Turn off the Macro Recorder
Hit Alt+F11 and you should see all the code you need to do whatever you want to do. Remember, the Macro Recorder is your friend!

Excel copy all values from one main sheet to various other sheets if they are a certain colour

Would really appreciate a solution to the below:
I am looking to have 8 sheets.
Main sheet that has all jobs, these are all currently sorted into the following colours :
Red - live
Green - invoiced/complete
Blue - quoted
Black - enquiry
Grey - dead/ lost
Purple - work in progress
Yellow - Retention
what i would like to do is keep the main sheet and have a sheet for each of the above. when the text becomes red for example i would like it to be transfered to the live sheet and vica versa for the rest. this should be in a macro
can anyone help?
Many thanks,
You say "when the text becomes red " so possibly this is a conditional format?
In any case, what you need to do is to attach code to the main sheet's Calculate event . This code should do the following
look at the activecell's color element that you mean (font, background, conditional formating, etc)
Based on that color, copy the entire row to the appropriate sheet
(Can you assume the sheets already exist?)
You will probably need a Select Case Statement. I would declare a worksheet variable and then SET it to the appropriate sheet in the select
and then
Activecell.entirerow.copy ws.cells(ws.rows.count,1).end(xlup).offset(1,0)
will copy the row to the desired sheet at the bottom of any existing rows.
Have a try and come back with code if you get stuck
EDIT: Sorry I missed the "click a button" part. You can ignore the bit about the sheets calculate event - just attach your code to the button. The only thing to worry about then is that you will need to run through all the used rows of the sheet, since there might be more than one coloured row when you click on the button.

Make filter dropdown menus move into heading - VBA Excel 2016

When scrolling down, row 1 should replace the letters in the heading, but with my code it just disappears like a normal row.
How do I have to change my code in order to get this result?:
(Default Excel 2016 CSV import)
Existing Code:
With ActiveSheet
.Range("A1:" & Range("A1").SpecialCells(xlCellTypeLastCell).Address).AutoFilter
End With
you want to hide headings
it is in the ribbon ... view ... show ... headings
vba code
ActiveWindow.DisplayHeadings = False
then split and freeze top row
record a macro of the action if you need code
The solution to this problem is creating a table (Insert -> Tables -> Table) and select "My Table has headers"
Guide on support.office.com

Prevent vba generated PowerPoint table from auto-sizing rows

I have a large macro that generates and populates a table in powerpoint based on excel values. I manually resize the rows based on specific parameters, but I've run into the very annoying issue that I cannot seem to prevent the rows from auto-resizing if the text would overflow from that particular cell. I've tried using the textframe and textframe2 "autosize" property but this gives an error on the first call saying that the specified value is out of range. The error number is -2147024809 (80070057), although I doubt that will be of any use. Is there a way to prevent this autosizing beyond writing code to manually shorten the text when it will overflow?
RGA,
the answer to your question is yes; you can do this. This topic is discussed in the following thread: Understanding format of tables in PowerPoint (VBA 2010) (resize text to cell)
However, I don't know if this technique still 'works' for ppt 2016. I had such code implemented, and then I 'upgraded' to office 2016; now it doesn't work.
With that being said, this was my code (resized the text until it 'fit'):
...
Do Until (table.rows(1).height + table.rows(2).height < TABLE_HEIGHT) or (table.Cell(2, 2).Shape.TextFrame.TextRange.Font.size = 1)
If table.Cell(1, 1).Shape.TextFrame.TextRange.Font.size = 1 Then
table.Cell(1, 1).Shape.TextFrame.TextRange.Font.size = 27
table.Cell(2, 2).Shape.TextFrame.TextRange.Font.size = table.Cell(2, 2).Shape.TextFrame.TextRange.Font.size - 1
table.Cell(2, 3).Shape.TextFrame.TextRange.Font.size = table.Cell(2, 2).Shape.TextFrame.TextRange.Font.size
Else
table.Cell(1, 1).Shape.TextFrame.TextRange.Font.size = table.Cell(1, 1).Shape.TextFrame.TextRange.Font.size - 1
End If
Loop
In order to restore some functionality in ppt 2016, I decided to rewrite my code to limit the number of lines shown to prevent the 'resize' table call:
...
table.Cell(1, 1).Shape.TextFrame.TextRange = table.Cell(1, 1).Shape.TextFrame.TextRange.lines(1,2)
table.Cell(2, 2).Shape.TextFrame.TextRange = table.Cell(2,2).Shape.TextFrame.TextRange.lines(1,1)
table.Cell(2, 3).Shape.TextFrame.TextRange = table.Cell(2, 3).Shape.TextFrame.TextRange.lines(1,1)
In theory, you can use the .height; .Textrange; and the height of your font to figure the size of font you need inorder to 'shrink' the text to fit.
What do you want to happen when there is too much text to fit into a cell? There is no UI concept in PowerPoint to prevent row auto resizing based on cell overflow as there is nowhere for the additional text to go as demonstrated by typing in a cell within PowerPoint. Therefore there is no API to do the same. I would record the row before and after inserting the text and truncate word by word as you say until the row height returns to the original value.

Create new column in Excel based on previous column data

Just a disclaimer: I have limited experience with Excel and sql... I'm basically a noob, so bear with me.
I have a big Excel spreadsheet that is sent to me daily that I would like to manipulate.
I would like to add a couple columns that create values based on their respective rows.
ID Color Brand Indicator
1 Green Vizio TRUE
2 Yellow Samsung FALSE
3 Blue Samsung TRUE
4 Red Sony FALSE
5 Orange Vizio TRUE
In the example above, the Indicator column is the one that I'd like to be created based on the values in the previous columns. The Indicator should be true if Brand has the word Vizio in it, OR if the color is Blue. I mention it has to have the word vizio in it because there are cases where it won't be simply "vizio", but maybe "vizio tv".
I would like to automate this process as much as possible, so do you think it would be best to use an Excel VBA macro or SQL for this?
Any help would be much appreciated, thank you.
You can use the following Excel macro.
Public Sub AddIndicators()
Const INDICATOR_1_COLUMN = 4
Const INDICATOR_1_FORMULA = "=OR(IFERROR(SEARCH(""vizio"",C2),0),(B2=""blue""))"
[a2:index(a:a,counta(a:a))].Offset(, INDICATOR_1_COLUMN - 1) = INDICATOR_1_FORMULA
End Sub
This can easily be expanded for additional indicator columns.
To have this macro available to run whenever you receive your daily worksbooks (and without the need to add the macro to the incoming workbook) simply add this macro to your Personal Workbook.
After that is done, just make sure your daily workbook is open and on the sheet where you want the indicators before executing the macro.
If perchance you do not wish for the formulas to remain in the indicator column, you can use this version of the macro instead:
Public Sub AddIndicators()
Const INDICATOR_1_COLUMN = 4
Const INDICATOR_1_FORMULA = "=OR(IFERROR(SEARCH(""vizio"",C2),0),(B2=""blue""))"
With [a2:index(a:a,counta(a:a))].Offset(, INDICATOR_1_COLUMN - 1)
.Formula = INDICATOR_1_FORMULA
.Value = .Value
End With
End Sub
Your requirement is very simple. You don't have to go for VBscript, macros etc. It can be simply done with excel functions.
=OR(ISNUMBER(SEARCH("blue",B2)),(ISNUMBER(SEARCH("vizio",C2))))
Assumption: Color is in column B and Brand is in column c and data starts from row 2. First row is for headers.