In my tool I am trying to create a shape, rename it, have it move with the shifting widths of columns, and hyperlink it to a summary sheet. This is what I have so far, thanks in advance.
For s = 7 To Sheets.Count
With Sheets(s)
Dim GoToSummary As Shape
Set GoToSummary = .Shapes.AddShape(msoShapeRoundedRectangle, 400, 153 + 12.75 * 2, 300, 50)
.Shapes(GoToSummary).TextFrame.Characters.Text = "Go Back To Summary"
End With
Next s
I know this is not correct, that is why I am reaching out, because I couldn't find anything similar to my situation.
You were pretty close!
Sub test()
Dim GoToSummary As Shape
For s = 7 To Sheets.Count
Set GoToSummary = Sheets(s).Shapes.AddShape(msoShapeRoundedRectangle, 400, 153 + 12.75 * 2, 300, 50)
GoToSummary.TextFrame.Characters.Text = "Go Back To Summary"
Sheets(s).Hyperlinks.Add Anchor:=GoToSummary, Address:="", SubAddress:="Summary!A1"
Next s
End Sub
Dim GoToSummary outside of the loop
Once you've defined GoToSummary with Set, you can just refer to it directly, i.e. as GoToSummary instead of .Shapes(GoToSummary)
Added the hyperlink as well
Related
I'm attempting to get the trend line equation from the first series in my chart to a shape text box placed elsewhere on the worksheet - however, I can only get the textbox to populate correctly when I'm stepping through the code line by line - during run-time it has no effect:
For Each chtObj In ActiveSheet.ChartObjects
Set cht = chtObj.Chart
For Each srs In chtObj.Chart.SeriesCollection
srs.Trendlines(1).DisplayEquation = True 'Display the labels to get the value
ThisWorkbook.Worksheets("MyDataSheet").Shapes(slopetextboxes(k)).TextFrame.Characters.Text = srs.Trendlines(1).DataLabel.Text
srs.Trendlines(1).DisplayEquation = False 'Turn it back off
Exit For
Next srs
k = k + 1 ' for the slope textboxes
Next chtObj
Note that slopetextboxes is an array containing the names of ~6 shape text boxes.
As far as I know there's no way to get the trend line data label without stopping to display it. I've tried storing it in a string first, DoEvents, and turning Application.ScreenUpdating back on, all to no avail. I'm stumped here.
EDIT: It appears that by placing DoEvents after .DisplayEquation = True I'm able to have some of my shapes populate correctly, but not all. Still appears to be some kind of run-time issue.
BOUNTY EDIT: I've moved ahead to grab the slopes with a formula ran into the data itself, but I still don't understand why I can't grab the chart's .DataLabel.Text during run-time. I can grab it when stepping through, not during run-time. It appears to just take the PREVIOUS series slope and place it in the shape (or a cell, it doesn't even matter where the destination is). DoEvents placed in different spots yields different outcomes, so something must be going on.
Updated with better understanding of the bug. This works for me in excel 2016 with multiple changes to the source data (and therefore the slope)
I tried myChart.refresh - didnt work. I tried deleting and then re-adding the entire trendline, also didnt work.
This works for everything but the first case. First case needs to be hit twice. Same as for .select
If you try and delete trendline even after assigning its text to textbox, this wont work
Option Explicit
Sub main()
Dim ws As Worksheet
Dim txtbox As OLEObject
Dim chartObject As chartObject
Dim myChart As chart
Dim myChartSeriesCol As SeriesCollection
Dim myChartSeries As Series
Dim myChartTrendLines As Trendlines
Dim myTrendLine As Trendline
Set ws = Sheets("MyDataSheet")
Set txtbox = ws.OLEObjects("TextBox1")
For Each chartObject In ws.ChartObjects
Set myChart = chartObject.chart
Set myChartSeriesCol = myChart.SeriesCollection
Set myChartSeries = myChartSeriesCol(1)
Set myChartTrendLines = myChartSeries.Trendlines
With myChartTrendLines
If .Count = 0 Then
.Add
End If
End With
Set myTrendLine = myChartTrendLines.Item(1)
With myTrendLine
.DisplayEquation = True
txtbox.Object.Text = .DataLabel.Text
End With
Next chartObject
End Sub
Here's my code that seems to definitely work when just pressing F5:
Basically, I store the text in a collection, then iterate through all of the textboxes to add the text to the textboxes. If this wasn't precisely what you were asking for, then I hope this helps in any way.
Sub getEqus()
Dim ws As Worksheet
Dim cht As Chart
Dim srs As Variant
Dim k As Long
Dim i As Long
Dim equs As New Collection
Dim shp As Shape
Dim slopetextboxes As New Collection
Set ws = Excel.Application.ThisWorkbook.Worksheets(1)
'part of the problem seemed to be how you were defining your shape objects
slopetextboxes.Add ws.Shapes.Range("TextBox 4")
slopetextboxes.Add ws.Shapes.Range("TextBox 5")
For Each chtObj In ActiveSheet.ChartObjects
Set cht = chtObj.Chart
For Each srs In chtObj.Chart.SeriesCollection
srs.Trendlines(1).DisplayEquation = True 'Display the labels to get the value
equs.Add srs.Trendlines(1).DataLabel.Text
srs.Trendlines(1).DisplayEquation = False 'Turn it back off
Next srs
Next chtObj
For i = 1 To slopetextboxes.Count
'test output i was trying
ws.Cells(i + 1, 7).Value = equs(i)
slopetextboxes(i).TextFrame.Characters.Text = equs(i)
Next
End Sub
Pictures of what the output looks like when i just press the button
Good luck!
This worked for me - I loop through multiple charts on Sheet1, toggling DisplayEquation and then writing the equation to a textbox/shape on the different worksheet. I used TextFrame2.TextRange but TextFrame worked as well, if you prefer that. I wrote to both a regular text box, as well as a shape, which was probably overkill as the syntax is the same for both.
This gets the trendline equation from the first Series - it sounded like you didn't want to loop through all the Series in the SeriesCollection.
Sub ExtractEquations()
Dim chtObj As ChartObject
Dim slopeTextBoxes() As Variant
Dim slopeShapes() As Variant
Dim i As Integer
slopeTextBoxes = Array("TextBox 1", "TextBox 2", "TextBox 3")
slopeShapes = Array("Rectangle 6", "Rectangle 7", "Rectangle 8")
For Each chtObj In ThisWorkbook.Sheets("Sheet1").ChartObjects
With chtObj.Chart.SeriesCollection(1).Trendlines(1)
.DisplayEquation = True
ThisWorkbook.Sheets("MyDataSheet").Shapes(slopeTextBoxes(i)).TextFrame2.TextRange.Characters.Text = .DataLabel.Text
ThisWorkbook.Sheets("MyDataSheet").Shapes(slopeShapes(i)).TextFrame2.TextRange.Characters.Text = .DataLabel.Text
.DisplayEquation = False
i = i + 1
End With
Next chtObj
End Sub
I've written this off as a bug - The only workaround was discovered by BrakNicku which is to Select the DataLabel before reading its Text property:
srs.Trendlines(1).DataLabel.Select
Not a sufficient solution (since this can cause some issues during run-time), but the only thing that works.
I had a similar issue running the code below and my solution was to run Application.ScreenUpdating = True between setting the trendline and querying the DataLabel. Note that screen updating was already enabled.
'Set trendline to the formal y = Ae^Bx
NewTrendline.Type = xlExponential
'Display the equation on the chart
NewTrendline.DisplayEquation = True
'Add the R^2 value to the chart
NewTrendline.DisplayRSquared = True
'Increse number of decimal places
NewTrendline.DataLabel.NumberFormat = "#,##0.000000000000000"
'Enable screen updating for the change in format to take effect otherwise FittedEquation = ""
Application.ScreenUpdating = True
'Get the text of the displated equation
FittedEquation = NewTrendline.DataLabel.Text
If it works when you step through, but not when it runs then it's an issue with timing and what Excel is doing in between steps. When you step through, it has time to figure things out and update the screen.
FYI, Application.Screenupdating = False doesn't work when stepping
through code. It gets set back to True wherever the code pauses.
When did you give it a chance to actually do the math and calculate the equation? The answer is that, you didn't; hence why you get the previous formula.
If you add a simple Application.Calculate (in the right spot) I think you'll find that it works just fine.
In addition, why should Excel waste time and update text to an object that isn't visible? The answer is, it shouldn't, and doesn't.
In the interest of minimizing the amount of times you want Excel to calculate, I'd suggest creating two loops.
The first one, to go through each chart and display the equations
Then force Excel to calculate the values
Followed by another loop to get the values and hide the equations again.
' Display the labels on all the Charts
For Each chtObj In ActiveSheet.ChartObjects
Set cht = chtObj.Chart
For Each srs In chtObj.Chart.SeriesCollection
srs.Trendlines(1).DisplayEquation = True 'Display the labels to get the value
' I take issue with the next line
' Why are you creating a loop, just for the first series?
' I hope this is just left over from a real If condition that wan't included for simplicity
Exit For
Next srs
Next chtObj
Application.ScreenUpdating = True
Application.Calculate
Application.ScreenUpdating = False
' Get the Equation and hide the equations on the chart
For Each chtObj In ActiveSheet.ChartObjects
Set cht = chtObj.Chart
For Each srs In chtObj.Chart.SeriesCollection
ThisWorkbook.Worksheets("MyDataSheet").Shapes(slopetextboxes(k)).TextFrame.Characters.Text = srs.Trendlines(1).DataLabel.Text
srs.Trendlines(1).DisplayEquation = False 'Turn it back off
Exit For
Next srs
k = k + 1 ' for the slope textboxes
Next chtObj
Application.ScreenUpdating = True
Update:
I added a sample file based on your description of the issue. You can select 4 different options in an ActiveX ComboBox which copies values to the Y-Values of a chart. It shows the trend-line equation below, based on the formula & through copying the value from the chart into a Textbox shape.
Maybe 2016 is different, but it works perfectly in 2013. Try it out...
Shape Text Box Example.xlsm
I'm writing a simple code to position my shapes (which are actually pictures) in the document. I want them to be positioned:
horizontally to exactly 0 mm. from the left side of the printable area
vertically to 7 mm. below the paragraph (to which the shape is anchored)
I wrote a simple code:
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
Selection.ShapeRange.Left = MillimetersToPoints(0)
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
Selection.ShapeRange.Top = MillimetersToPoints(7)
Selection.ShapeRange.WrapFormat.Type = wdWrapTopBottom
For 1 shape on the page it works fine. But if there are more then 1 shape, it somehow "throws" the 2nd shape to the top of the page. It looks like Word anchors it to the 1st paragraph on the page. but it shouldn't. At the same time horizontal positioning is ok.
I would appreciate any help to fix this issue.
My possible solution for this issue will look as follows:
Sub PositShape_3()
Dim I As Integer
If Selection.InlineShapes.Count <> 0 Then
For I = Selection.InlineShapes.Count To 1 Step -1
Selection.InlineShapes(I).ConvertToShape
Next I
End If
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
Selection.ShapeRange.Left = MillimetersToPoints(0)
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionLine
Selection.ShapeRange.Top = MillimetersToPoints(7)
Selection.ShapeRange.WrapFormat.Type = wdWrapTopBottom
End Sub
In spite of the fact that the use of wdRelativeVerticalPositionLine solved the problem, it is still interesting why the use of wdRelativeVerticalPositionParagraph has such unexpected unwanted consequences.
Note the use of SELECTION in the code you show us. If you don't change the paragraph selection, then the shapes will always be anchored to the same paragraph. Working with a Selection in Word is tricky; it's much better to work with a more tangible object, such as a specific paragraph.
The following code sample illustrates using paragraph objects to anchor and position successively added Shapes.
Sub insertShapesProgressively()
Dim shp As word.Shape
Dim shpRng As word.ShapeRange
Dim rng As word.Range
Dim iParaCounter As Long
'We want to insert the Shape anchored to three different paragraphs
' on the same page
For i = 7 To 9
Set rng = ActiveDocument.Paragraphs(i).Range
Set shp = ActiveDocument.shapes.AddShape(msoShapeWave, 0, 0, 10, 10, rng)
Set shpRng = rng.ShapeRange
shpRng.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
shpRng.Left = MillimetersToPoints(0)
shpRng.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
shpRng.Top = MillimetersToPoints(7)
shpRng.WrapFormat.Type = wdWrapTopBottom
Next
End Sub
I need to be able to create a new .ppt (PowerPoint presentation) from selected slides in my original .ppt. The following macro will take whatever slides you currently have selected and copy them into a new .ppt. I've found the following nice code to do most of the work.
Private Sub NytPPT_Click()
'PURPOSE: Copies selected slides and pastes them into a brand new presentation file
'SOURCE: www.TheSpreadsheetGuru.com
Dim NewPPT As Presentation
Dim OldPPT As Presentation
Dim Selected_slds As SlideRange
Dim Old_sld As Slide
Dim New_sld As Slide
Dim x As Long, y As Long
Dim myArray() As Long
Dim SortTest As Boolean
'Set variable to Active Presentation
Set OldPPT = ActivePresentation
'Set variable equal to only selected slides in Active Presentation
Set Selected_slds = ActiveWindow.Selection.SlideRange
'Sort Selected slides via SlideIndex
'Fill an array with SlideIndex numbers
ReDim myArray(1 To Selected_slds.Count)
For y = LBound(myArray) To UBound(myArray)
myArray(y) = Selected_slds(y).SlideIndex
Next y
'Sort SlideIndex array
Do
SortTest = False
For y = LBound(myArray) To UBound(myArray) - 1
If myArray(y) > myArray(y + 1) Then
Swap = myArray(y)
myArray(y) = myArray(y + 1)
myArray(y + 1) = Swap
SortTest = True
End If
Next y
Loop Until Not SortTest
'Set variable equal to only selected slides in Active Presentation (in numerical order)
Set Selected_slds = OldPPT.Slides.Range(myArray)
'Create a brand new PowerPoint presentation
Set NewPPT = Presentations.Add
'Align Page Setup
NewPPT.PageSetup.SlideHeight = OldPPT.PageSetup.SlideHeight
NewPPT.PageSetup.SlideOrientation = OldPPT.PageSetup.SlideOrientation
NewPPT.PageSetup.SlideSize = OldPPT.PageSetup.SlideSize
NewPPT.PageSetup.SlideWidth = OldPPT.PageSetup.SlideWidth
'Loop through slides in SlideRange
For x = 1 To Selected_slds.Count
'Set variable to a specific slide
Set Old_sld = Selected_slds(x)
'Copy Old Slide
yy = Old_sld.SlideIndex
Old_sld.Copy
'Paste Slide in new PowerPoint
NewPPT.Slides.Paste
Set New_sld = Application.ActiveWindow.View.Slide
'Bring over slides design
New_sld.Design = Old_sld.Design
'Bring over slides custom color formatting
New_sld.ColorScheme = Old_sld.ColorScheme
'Bring over whether or not slide follows Master Slide Layout (True/False)
New_sld.FollowMasterBackground = Old_sld.FollowMasterBackground
Next x
End Sub
What I need to do, is to select which slides to copy - based on check boxes. So, for example if I select Check Box 1 = TRUE, it will create slides 1, 2 and 3. Or if I select Check box 2 = TRUE, that it could select slide 3, 4, 5 and 6. And so, if I selected both boxes it would create slides = 1, 2, 3, 4, 5, 6. Leaving out any duplicates.
I've tried a lot, including this:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
ActivePresentation.Slides.Range(Array(1, 2, 3)).Select
Else
MsgBox "nothing"
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
ActivePresentation.Slides.Range(Array(3, 4, 5, 6)).Select
Else
MsgBox "nothing"
End If
End Sub
I get the error: Slide (unknown member) : Invalid request. This view does not support selection.
I am not sure how I could get this to work? Any help is appreciated, I'am very new to VBA coding.
All credit for code goes to. http://www.thespreadsheetguru.com/the-code-vault/2014/4/3/copy-selected-slides-into-new-powerpoint-presentation
You can switch the view to enabled the slides to be selected as follows:
ActiveWindow.ViewType = ppViewSlideSorter
For some reason, the slides aren't selected in the normal view!
But selecting things in PowerPoint brings its own challenges (as seen with the view type) and you don't need to select them in order to copy and paste them as per this example:
With ActivePresentation.Slides
.Range(Array(1, 2)).Copy
.Paste
End With
This will simplify your code as you don't need to manage windows and their views.
In Excel 2010, using VBA code to create a macro-calling push-button.
NOTE: This is really a question about where to find documentary support for a type of object, not a coding thing as such.
So... Working from the macro recorder, I get this (supposing I want to turn cell M8 into a button):
Sub MakeMyButton()
' Some basic definitions
StarterRow = 8
MyLeft = Cells(StarterRow, "M").Left
MyTop = Cells(StarterRow, "M").Top
MyHeight = Cells(StarterRow, "M").Height
MyWidth = Cells(StarterRow, "M").Width
' Now comes the creation bit
ActiveSheet.Buttons.Add(MyLeft, MyTop, MyWidth, MyHeight).Select
With Selection
.AltText = "View"
.OnAction = "afRoomText"
' .Value = xlOff
' .LinkedCell = "M" & StarterRow
' .Display3DShading = True
End With
End Sub
The problem is, of course, that .Value and .LinkedCell and .Display3DShading, here marked as remarks, are not valid for Buttons.Add -- fair enough. But I cannot seem to find a comprehensive list of what IS valid for this object! Anyone know where to look?
(Incidentally, how come the record-macro thing uses things like "CheckBoxes.Add" that aren't documented anywhere obvious in Excel VBA? If you go hunting for "CheckBoxes" in the documentation, you find a lot of tangential remarks but no actual information, as though this were deprecated code that just happens to be used automatically....)
Always declare all your variables using Dim. For objects (such as Button) this will give you access to autocompletion. Example:
Dim b As Button ' Aha!
Set b = ActiveSheet.Buttons.Add(324, 102, 108, 51)
Another way to create a button:
Dim but As Shape
Dim sh As Worksheet
Set sh = ActiveSheet
Set but = sh.Shapes.AddFormControl(xlButtonControl, 100, 100, 100, 100)
With but
.AlternativeText = "View"
.OnAction = "afRoomText"
With .TextFrame
.Characters.Text = "Press Me"
.Characters.Font.Color = RGB(255, 50, 50)
End With
End With
I want to move image from one location in excel to another using VBA.
How can we do that?
If you need to change the position of the image within a given worksheet, you can use something like this:
ActiveSheet.Shapes.Range(Array("Picture 1")).Select
Selection.ShapeRange.IncrementLeft 100
You can adjust the direction and amount of motion by changing the parameters of the .Increment... command, to animate an image for example.
Another example : move a picture vertically to line up with a specific row
Sheets(1).Shapes("Picture 1").Top = Sheets(1).Rows(24).Top
If we are going quick and dirty and need to move between sheets the following works
Sub CutAndPasteAPicture(shapeName As String, fromSheet As String, toSheet As String, toRange As String)
'Cut and Paste
Sheets(fromSheet).Shapes(shapeName).Cut
Sheets(toSheet).Paste Sheets(toSheet).Range(toRange)
End Sub
Sub Example()
CutAndPasteAPicture "Picture 1", "Sheet1", "Sheet2", "D2"
End Sub
Here is the code to first insert picture to Excel and then adjust or resize the Picture. Later move the Picture towards down or right
'Insert the Picture from the path if its not present already
Set myPict = Thisworkbook.sheets(1).Range("A1:B5").Parent.Pictures.Insert(ThisWorkbook.Path & "\" & "mypic.jpg")
'Adjust the Picture location
myPict.Top = .Top
myPict.Width = .Width
myPict.Height = .Height
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
'myPict.LockAspectRatio = msoTriStateMixed
'Change the width of the Picture
myPict.Width = 85
'change the Height of the Picutre
myPict.Height = 85
End With
'Select the Picutre
myPict.Select
'Move down the picture to 3 points. Negative value move up
Selection.ShapeRange.IncrementTop 3
'Move towards right upto 5 points. Negative value moves towards right
Selection.ShapeRange.IncrementLeft 5