After executing some deleting rows in VBA, a shape size (group) changes height, but is there a way to do this in VBA rather than in the sheet itself. I've assigned it a Macro (Group1), but I am uncertain what to do next. Any suggestions?
Set shape properties to Don't move or size with cells
Shapes("YourShapename").Placement = xlFreeFloating
Related
I'm really a beginner at vba, but I've been puzzling around how to make it work. I'm creating a powerpoint presentation with Excel vba, based on data in an excel sheet a table is created on a slide in a newly created powerpoint presentation. I've got the table formatting done, which is a real hassle, by going over each individual cell and formatting them (looping, but still)....
The only thing missing is the individual column width. Based on the input data some columns might change in width,
Because of this reason I would like to have an autofit on the columns only. The rows should remain 1 line in height. But the column width should adapt to whatever would be necessary to keep the row height on 1 line.
Dim PPTtable1 As PowerPoint.Shape
With PPTtable1
.Left = 350
.Top = 150
.Width = 758
.table.Columns(1).Width = Auto
.table.Columns(2).Width = Auto
.table.Columns(3).Width = Auto
End With
This would turn the columns in the smallest possible width by stretching the height of the cells. Basically what I need is the function of double clicking on the side border of a column, and it will automatically fit to the correct width.
EDIT:
Maybe this would be interesting and important to know the order of the code:
Table is created
Format of the individual cells (bold, italic, size, font, etc...)
Data is filled in from an excel sheet
Trying to autosize the column
I have made a large diagram of made by shapes with numbers stored inside as text and need to change the "old" numbers to a "new" range of numbers. On each of the 20 sheets there are circles, Rectangles, Left Arrows and Right Arrows. The old range of numbers are stored on a separate spreadsheet in the Column "A" and need to be changed to the numbers listed in Column "B" (as in A1 to B1).
What would a VBA method to change the old text inside the Shapes to the new correct text based off of a new range of numbers? Is it possible to write a script that changes the values in the entire WorkBook?
My incorrect way of thinking is to:
1. Search inside the diagram to find the various shapes.
2. Get the text inside each shape.
3. Compare the text with the spreadsheet with the old numbers.
4. Insert the new number.
5. Move onto the next shape.
Put a value in A1 and say we already have a Shape:
Running this:
Sub LinkCellToShape()
Dim sh As Shape
Set sh = ActiveSheet.Shapes(1)
sh.DrawingObject.Formula = "=A1"
End Sub
will link the text in the shape to the value in the cell. You could also use:
sh.OLEFormat.Object.formula = "=A1"
I am using Excel 2007. I have a worksheet that contains many shapes. Each shape is linked to a cell on another spreadsheet (i.e., '=Data!$F$5') in order to dispay some text in each shape.
Now, I would like to use the formula in the shapes as a starting point to reference other cells from the worksheet. The idea behind this is to run a macro when the user clicks on the shape to give them additional information in a message box.
I've tried making a string from "ActiveShape.DrawingObject.Formula", but haven't had any success. Does anyone know how to do this or suggest another way of accomplishing this?
Any help is greatly appreciated.
Within the macro that you assigned to the shape(s). You can use
ActiveSheet.Shapes(Application.Caller).name to get the name of the shape
and then ActiveSheet.Shapes.Range(Array(name)) to get the shape reference. Then you can select it and use Selection.formula to get the formula
Copy this and paste it into your macro for the shape and you can see how it works:
Dim name As String
Dim formula As String
name = ActiveSheet.Shapes(Application.Caller).name
ActiveSheet.Shapes.Range(Array(name)).Select
formula = Selection.formula
MsgBox formula
Note: I did try and just do formula = ActiveSheet.Shapes.Range(Array(name)).formula but vba was unhappy with me. So I had to stick to the .Select and Selection.formula
I'm automatically generating a powerpoint slide through VBA, User Forms, and Excel. You run the VBA script in excel, fill out the data, the data goes into cells in excel, then the VBA script pulls the data and puts it into textbox shapes in the slide.
My problem is I want to use different font sizes at different times, so for example 28 pt font for one part and 14 pt for the rest. The problem is that any property changes I make to the textbox applies to all of the text within the shape.
My current workaround is sloppy and is to just generate another textbox over the original and insert spacing in the original so it looks like the larger text is "in" the textbox while it's actually just sitting over a few empty lines set aside.
You can format specific substrings within a string, but it's very cumbersome, for example assuming shp is an object variable representing your textbox:
Sub foo()
Dim shp As Shape
Set shp = ActivePresentation.Slides(1).Shapes("TextBox 3")
shp.TextFrame.TextRange.Text = "Hello, world!"
shp.TextFrame.TextRange.Characters.Font.Size = 14 'applies uniform font to entire shape
shp.TextFrame.TextRange.Characters(1, 5).Characters.Font.Size = 28
End Sub
Example output:
The difficulty of course is working with mixed formats, and I do not think there is any easy solution. It will be up to you to determine what formats you need to "capture", and what subsequently implement the appropriate conditional logic to transfer those formats to the PowerPoint shapes.
One possible alternative -- and this is the route that I would go if I were you -- would be to copy the cell from Excel, and use this method to paste in to PowerPoint. I believe this will create a table consisting of a single cell, in the PowerPoint slide. You will have to make adjustments for size/position, but this should be an order of magnitude easier than trying to capture every possible variation of font formatting:
Sub foo2()
Dim shp As Shape
Dim xl As Object
'Get Excel and copy a specific cell
Set xl = GetObject(, "Excel.Application")
xl.Workbooks("Book35").Sheets("Sheet2").Range("B4").Copy
'Paste that cell in to PowerPoint as a table, preserving formats:
ActivePresentation.Slides(1).Select
Application.CommandBars.ExecuteMso "PasteSourceFormatting"
End Sub
Example output, as copied from the Excel cell:
No need to change the font in excel to reflect in Word. You can do it directly. Just paste the below mentinoed line in Word VBA : -
Activedocument.Shapes("sam").TextFrame.TextRange.Words(1).Font.Size = 28
I have VBA code to copy shapes matching a certain criteria from one Powerpoint presentation to another below. However, when it pastes the shape, it is offset (down and to the right) from the original position. How can I copy a shape while maintaining the original coordinates?
sourceShape.Copy
Presentations(2).Windows(1).Activate
ActivePresentation.Slides(x).Shapes.Paste (1)
Perhaps simply:
Shape s=ActivePresentation.Slides(x).Shapes.Paste (1)
s.Left=sourceShape.Left
s.Top=sourceShape.Top