How to select a text box and resize using VBA? - excel

I am trying to resize the text box size using VBA. To do that I change the rectangle number in the VBA every time which is not serving the purpose of VBA.
I need to resize the selected text box based on value in other cells.
Sub ResizeTextBox()
Dim shp As Shape
Set shp = ActiveSheet.Shapes("Rectangle 39")
shp.Height = Application.CentimetersToPoints(Range("Y5").Value)
shp.Width = Application.CentimetersToPoints(Range("Y6").Value)
End Sub
How do I resize the selected text box based on value in cell Y5 and Y6?

You can do something like this:
Sub ResizeTextBox()
Dim shp As Shape
'check a range is not selected
If TypeName(Selection) <> "Range" Then
With Selection
.Height = Application.CentimetersToPoints(Range("Y5").Value)
.Width = Application.CentimetersToPoints(Range("Y6").Value)
End With
Else
MsgBox "First select a a shape for resizing"
End If
End Sub

Related

Picture Group set to Range

Apologies another noob question. I have been tasked with writing a document within excel, the front cover has a lot of images on it and i have grouped these images together. As there is a risk that the user could move this group. I want to set it so each time that sheet is selected it moves back to its original location. I have looked over the web and i can't seem to find anything for a group of images.
I have tried this and it doesnt work at all. :(
Private Sub Worksheet_Activate()
Dim PicGroup As GroupShapes
With Range("A1")
PicGroup.Name = "HeaderGrp"
PicGroup.Visible = True
PicGroup.Top = .Top
PicGroup.Left = .Left
End With
End Sub
So my group of images I have called HeaderGrp I have put this on Activate Worksheet in VBA and i want this to always move or fix to cells A1.
I would also love this to fit to the page width and length if anyone knows how to do that.
Snapshot of what i would like: -
1) on sheet selection, image group moves to the correct location.
2) image group auto adjusts to page width and height.
Thank you in advance,
This works for me. Pictures appear to be treated as a type of Shape.
Private Sub Worksheet_Activate()
Dim p As Shape
With activesheet
Set p = .Shapes("Pics") 'name
p.Top = .Range("a1").Top
p.Left = .Range("a1").Left
End With
End Sub
This piece of code should get your grouped images:
Option Explicit
Private Sub Worksheet_Activate()
Dim shp As Shape
Dim PicGroup As GroupShapes
'loop through all your shapes
For Each shp In Me.Shapes
'if the shape is grouped then
'set your PicGroup variable
'and exit the loop
If shp.Type = msoGroup Then
Set PicGroup = shp.GroupItems
Exit For
End If
Next shp
With Range("A1")
PicGroup.Name = "HeaderGrp"
PicGroup.Visible = True
PicGroup.Top = .Top
PicGroup.Left = .Left
End With
End Sub

Change font size in textboxes

I have pasted textboxes over charts.
I'm trying to change the font size within that textbox for all charts.
This is a picture of what I mean
Sub shapeFont()
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
With shp.ShapeRange.TextFrame2.TextRange.Font
.Size = 30
End With
Next shp
End Sub
These textboxes seems to be embedded into ChartObjects. So you have to loop your ChartObjects instead. Then loop over its Shapes collection and only when you encounter a proper TextBox you should change it's font. Embedded textboxes can be inserted into a chart by selecting a chart and inserting a shape. From then, these shapes move with the chart.
For example:
Sub FindTextBoxes1()
Dim c As ChartObject
Dim s As Shape
For Each c In ActiveSheet.ChartObjects
For Each s In c.Chart.Shapes
If s.Type = msoTextBox Then
s.TextFrame2.TextRange.Font.Size = 30
End If
Next s
Next c
End Sub
Specify a Workbook and Worksheet variable for pinpointing better where you want this macro to operate (instead of an ugly ActiveSheet)
Inspiration from here
Whereas embedded textboxes are great to distinguish shapes you do want to change from the ones you don't want to change, you also don't need to actually select your textboxes first to be able to change your Font.Size in case you want to iterate over all non-embedded textboxes. Simply refer to the Characters within the TextRange. For example:
Sub FindTextBoxes2()
Dim s As Shape
For Each s In ActiveSheet.Shapes
If s.Type = msoTextBox Then
s.TextFrame2.TextRange.Characters.Font.Size = 30
End If
Next
End Sub
VBA is a strange language if you actually select it it is able to do it..
Sub shapeFont()
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
shp.Select
With Selection.ShapeRange.TextFrame2.TextRange.Font
.Size = 30
End With
Next shp
End Sub
If chart object is present and you have separate textboxes this works:
Sub shapeFont()
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
shp.Select
Debug.Print (shp.Type)
If shp.Type = msoTextBox Then
With Selection.ShapeRange.TextFrame2.TextRange.Font
.Size = 50
End With
End If
Next shp
End Sub
If you want to update the boxes on the chart the code provided by https://stackoverflow.com/users/9758194/jvdv will work

Replacing image in cell with Text "1"

I have an excel that has 1 column with picture in each cell, that picture determines something but i would like to change it to 1 instead. There is only 1 picture, and to my exact, the column is J;Name =PO history/release documentation . Can someone help me to do it with VBA Thank you!
Public Sub Replace_Picture()
Const Replace_Text = "OK"
Dim shp as Shape
For Each shp In ActiveSheet.Shapes
If shp.TopLeftCell.Address = shp.BottomRightCell.Address Then
shp.TopLeftCell.Value = Replace_Text
shp.Delete
End If
Next
Set shp = Nothing
End Sub
I tried the above code but its not working and const replace_text is in red.
I just want to change those cells with the picture to "1", while those blanks will be leave it to be.
I guess you want to delete the PO history shapes from SAP.
you may have a try by below VBA code.
Another way is you may 'Copy+Paste' data from SAP directly instead of 'Export to Excel'
Sub Replace_Shapes()
Dim Shp As Shape
Dim Shp_Address
For Each Shp In ActiveSheet.Shapes
If Shp.Type = msoPicture Then
'Identify the address of Shape by TopLeft
Shp_Address = Shp.TopLeftCell.Address
Range(Shp_Address) = 1
Shp.Delete
End If Next
End Sub

Excel-Changing pictures automatically using cell value in vba

I want to automatically insert a picture in cell AH32 depending on value in AB32.
I am able to insert the picture but not depending on the value in AB32. How do I fix this please?
Code:
Sub Picture()
Range("AH32").Select
Dim picname As String
If Range("AB32").Value < 85# Then
picname = "C:\Users\20149308\Desktop\sucess\images" & ".png" 'Link to the Picture
ActiveSheet.Pictures.Insert(picname).Select
With Selection
.Left = Range("AH32").Left
.Top = Range("AH32").Top
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 80#
.ShapeRange.Width = 80#
.ShapeRange.Rotation = 0#
End With
ElseIf Range("AB32").Value >= 85# Then
picname = "C:\Users\20149308\Desktop\sucess\succ" & ".jpg" 'Link to the Picture
ActiveSheet.Pictures.Insert(picname).Select
With Selection
.Left = Range("AH32").Left
.Top = Range("AH32").Top
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 80#
.ShapeRange.Width = 80#
.ShapeRange.Rotation = 0#
End With
End If
Range("AH32").Select
Application.ScreenUpdating = True
Exit Sub
ErrNoPhoto:
MsgBox "Unable to Find Photo" 'Shows message box if picture not found
Exit Sub
End Sub
Here is one way of writing it in a more condensed form with some basic error checking.
Option Explicit
Sub Picture()
Application.ScreenUpdating = True
Dim testRange As Range
Dim picname As String
Set testRange = ActiveSheet.Range("AB32")
If IsEmpty(testRange) Then
MsgBox "No value in cell AB32"
Exit Sub
End If
Select Case True
Case Not IsNumeric(testRange.Value2)
MsgBox "Value in cell AB32 is not numeric"
Exit Sub
Case testRange.Value2 < 85#
picname = "C:\Users\20149308\Desktop\sucess\images" & ".png"
Case testRange.Value2 >= 85#
picname = "C:\Users\20149308\Desktop\sucess\succ" & ".jpg"
End Select
On Error GoTo ErrNoPhoto
ActiveSheet.Pictures.Insert(picname).Select
With Selection
.Left = Range("AH32").Left
.Top = Range("AH32").Top
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 80#
.ShapeRange.Width = 80#
.ShapeRange.Rotation = 0#
End With
Application.ScreenUpdating = True
Exit Sub
ErrNoPhoto:
MsgBox "Unable to Find Photo" 'Shows message box if picture not found
Exit Sub
End Sub
You can do this without any VBA using the Camera. You can find this by selecting File followed by Options and Customize Ribbon and adding the camera icon to your ribbon.
Create a blank worksheet and adjust the column width/row height so each of your pictures will sit within the boundaries of a cell (in my example I'm using B2 and B4).
Select one of these cells and click the camera icon to take a photo of it.
Switch to your reporting sheet and click on it to paste the photo you just took. You'll see a picture of the cell you originally clicked on within a picture frame that you can rotate and resize.
Paste your two pictures into the cells on the blank worksheet. The picture frame on the reporting sheet will now display whichever picture is in the cell you clicked on.
Create a named range using this formula (adjust sheet names to suit):
=IF(Sheet1!$AB$32<85,Sheet2!$B$2,Sheet2!$B$4) - absolute referencing is important here.
I called the range DisplayImage.
Select the picture frame and change the formula in the formula bar to =DisplayImage.
The image will now update based on the value in cell AB32.

Insert and print stationery

I'm trying this for a few hours now, but I can't figure out how to get an image as stationery in my background in Excel 2010. In all ways it seems I just can't get it spread from the top left to bottom right corner.
Can I accomplish this with a macro, or is there some other way to do it?
See the microsoft link
To quote
"In Excel, you can use a picture as a sheet background for display purposes only. A sheet background is not printed and is not retained in an individual worksheet or in an item that you save as a Web page. It is retained only when you publish an entire workbook as a Web page.
Important Because a sheet background is not printed, it cannot be used as a watermark. You can, however, mimic a watermark by inserting a graphic in a header or footer."
This piece of code will let you choose a picture (you can already have one and adapt this code), it will resize the picture to fit the printarea and align it on the top left of the printarea:
Option Explicit
Private Sub Test()
Dim PicLocation As String
Dim MyRange As Range, TargetCell As Range
Set MyRange = Range(ActiveSheet.PageSetup.PrintArea)
Set TargetCell = MyRange.Cells(1, 1)
PicLocation = Application.GetSaveAsFilename("C:\", "Image Files (*.jpg),*.jpg", , "Specify Image Location")
If PicLocation <> "False" Then
ActiveSheet.Pictures.Insert(PicLocation).Select
Else
Exit Sub
End If
With Selection.ShapeRange
.LockAspectRatio = msoTrue
If .Width > .Height Then
.Width = MyRange.Width
If .Height > MyRange.Height Then .Height = MyRange.Height + ActiveSheet.PageSetup.HeaderMargin + ActiveSheet.PageSetup.BottomMargin
Else
.Height = MyRange.Height
If .Width > MyRange.Width Then .Width = MyRange.Width + ActiveSheet.PageSetup.LeftMargin + ActiveSheet.PageSetup.RightMargin
End If
.Left = TargetCell.Left - ActiveSheet.PageSetup.LeftMargin
.Top = TargetCell.Top - ActiveSheet.PageSetup.HeaderMargin
End With
With Selection
.Placement = xlMoveAndSize
.PrintObject = True
End With
End Sub

Resources