VBA to format selected picture

AntonT

New Member
Joined
Jun 9, 2021
Messages
3
Office Version
  1. 2007
Platform
  1. Windows
Hi.

I have a situation where I have already selected a picture that I have inserted into my Excel worksheet, locked to a cell.

Then I need a macro to do the following:

1. If it is landscape (ie the height is greater than the width), rotate 90 degrees.
2. Copy the Alternative text into the cell in which the object resides.
3. Use the alternative text as a hyperlink on the picture.

Thank you.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

AntonT

New Member
Joined
Jun 9, 2021
Messages
3
Office Version
  1. 2007
Platform
  1. Windows
Here is what I have so far:

VBA Code:
Sub macro()
Dim Hyp As String

Hyp = Selection.ShapeRange.AlternativeText

If Selection.ShapeRange.Height > Selection.ShapeRange.Width Then Selection.ShapeRange.Rotation = "90"


End Sub

I can't get the hyperlink to work, nor how to deselect the object and select the cell behind it.

Thank you.
 

AntonT

New Member
Joined
Jun 9, 2021
Messages
3
Office Version
  1. 2007
Platform
  1. Windows
Solved:

VBA Code:
Sub macro()
Dim Hyp As String

Hyp = Selection.ShapeRange.AlternativeText
ShortHyp = Left(Hyp, Len(Hyp) - 4)

'Change from landscape to portrait if required
If Selection.ShapeRange.Height > Selection.ShapeRange.Width Then Selection.ShapeRange.Rotation = "90"

'Add hyperlink to photo
With ActiveSheet
.Hyperlinks.Add Anchor:=.Shapes(Selection.ShapeRange.Name), Address:=Hyp
End With

'Input photo name in cell
ActiveCell.Select
ActiveCell = ShortHyp


End Sub
 
Solution

Forum statistics

Threads
1,147,499
Messages
5,741,507
Members
423,663
Latest member
kaveh87rsh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top