Replacing image in cell with Text "1" - excel

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

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

Excel VBA - Convert existing image in cell to comment picture

I'm trying to use VBA in Excel to convert a bunch of pictures in a column (one per cell) to a pop up comment image instead so that the sheet is more easily readable.
I can find the image I need by iterating through the shapes, and I can set this as an object; but I can't seem to use that onject to populate the comment field. It seems to be looking for a true file path instead.
I don't particularly want to have to save each image and then reload it, seems kind of pointless.
For Each Pic In ActiveSheet.Shapes
If Pic.TopLeftCell.Address = ActiveCell.Address Then
If Pic.Type = msoPicture Then
Pic.Select
Application.ActiveCell.AddComment.Shape.Fill.UserPicture **(ActiveSheet.Shapes(Pic.name))** 'if I use a path here its okay
'SelectPictureAtActiveCell = name
Exit For
End If
End If
Next
any thoughts?
CJ
I think you want to show one image if you select a specific cell then
See
Making shapes invisible/visible in excel through VBA
with
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Macro1
End Sub
You can make images hide and show using
ActiveSheet.Shapes("ImageName").Visible = False or True
for example when you click on cell A1 first image is hidden else all images are visible
Sub Macro1()
Dim shp As Shape
If ActiveCell.Address = "$A$1" Then
For Each shp In ActiveSheet.Shapes
ActiveSheet.Shapes(1).Visible = False
' or you can use image name as
'ActiveSheet.Shapes("ImageName").Visible = False
'shp.Visible = False
Next
Else
For Each shp In ActiveSheet.Shapes
shp.Visible = True
Next
End If
End Sub

Delete one autoshape in a range - VBA

This is what i try to do. When i click on one button, the autoshape above is copied in the A8:F12 rectangle.
The code for the 1st button(recorded macro):
Sub addTextbox1()
Range("A2:C3").Select
Range("C2").Activate
Selection.Copy
Range("B9").Select
ActiveSheet.Paste
End Sub
My problem is i want to make a button which can erase the autoshape in the A8:F12 range. I found this which delete all autoshapes in the worksheet:
Sub DeleteShapes()
Dim Shp As Shape
For Each Shp In ActiveSheet.Shapes
Shp.Delete
Next Shp
End Sub
or delete an autoshape by its name (don't work in my case, new autoshapes are generated so i don't know their name)
I know it's fast to click on the shape and press 'delete' but by curiosity i wanted to know if it's possible to do it in VBA
One way below is to test whether the top left cell of the shape lies in your deletion range:
Sub DeleteShapes()
Dim Shp As Shape
For Each Shp In ActiveSheet.Shapes
If Not Application.Intersect(Shp.TopLeftCell, ActiveSheet.Range("A8:F12")) Is Nothing Then Shp.Delete
Next Shp
End Sub

How to select a text box and resize using VBA?

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

Resources