Show/Hide Profile picture on each row based on Employee Name? - excel

I have found a simple script that allows for hiding or showing a picture using the text in a shape. I like the functionality and would like to apply it to our list of employees. However, the way it is constructed right now would require me to add one macro for each person and that is not sustainable in the long run.
Is there a way to re-write this script so it sets the name of the picture based on the name of the employee that is located in column A? Then it would be really simple to just insert images and name them with the Employee name.
I also see that the button is mentioned in the code. So this also needs to be written more dynamically. Could I use a normal format control instead of a shape? (The button does not need to change the displayed text as in this script.)
I really would appreciate your help here. This would look really smooth and I think others would make good use of a VBA like this too.
Sub Macro1()
With ActiveSheet.Shapes("Rounded Rectangle 4").TextFrame2.TextRange.Characters
'Check if shape text is equal to "Hide"
If .Text = "Hide" Then
'Change shape text to "Show"
.Text = "Show"
'Hide shape
ActiveSheet.Shapes("Picture 1").Visible = False
'Continue here if shape is not equal to "Hide"
Else
'Change text to "Hide"
.Text = "Hide"
With ActiveSheet.Shapes("Rounded Rectangle 4")
'Move image named "Picture1" based to lower right corner of shape
ActiveSheet.Shapes("Picture 1").Left = .Left + .Width
ActiveSheet.Shapes("Picture 1").Top = .Top + .Height
'Show image
ActiveSheet.Shapes("Picture 1").Visible = True
End With
End If
End With
End Sub
Reference: https://www.get-digital-help.com/show-and-hide-a-picture-vba/
Example data:

First, I'd like to say that you would be much better off designing something like this in MS Access as opposed to Excel. There are lots of tutorials showing how to build exactly this with Access Forms. It would certainly be much easier to maintain.
That being said, your question was about doing this in Excel and I'll answer that with a simple implementation suggestion. Just bare in mind, it comes with what I would consider "messy" maintenance.
First, you have a bunch of shapes representing the show/hide buttons. Each of these shapes would need to have their own unique name (not important what the name is for this case) and each of them would need to be positioned inside the cell for the row they are meant to operate on (as shown in your example photo).
Next, each employee's photo would need to be named the same as your employee name (the value in column A in your example).
Lastly, you would need to set the "Assigned Macro" of each show/hide button to the same method (I've named my Button_Click()). That method implementation looks like this:
Sub Button_Click()
Dim clickedButton As Shape
Dim employeePhoto As Shape
Dim clickedButtonRow As Long
Dim employeeName As String
'// gets the row number in whcih the clicked button resides
Set clickedButton = ActiveSheet.Shapes(Application.Caller)
clickedButtonRow = clickedButton.TopLeftCell.Row
'// gets the employee name (column A in this case)
employeeName = ActiveSheet.Range("A" & clickedButtonRow).Value
Set employeePhoto = ActiveSheet.Shapes(employeeName)
With clickedButton
' //set the position of the employee photo
employeePhoto.Top = .Top + .Height
employeePhoto.Left = .Left + .Width
With .TextFrame.Characters
'// set the visibility of the associated employee picture based on the text state of the button
employeePhoto.Visible = .Text = "Show"
'// swap the label on the button
If ActiveSheet.Shapes(employeeName).Visible Then
.Text = "Hide"
Else
.Text = "Show"
End If
End With
End With
End Sub

When applying the solution from ArcherBird I discovered a need to add a button in the headline to hide/show all pictures. This provides for a better user experience.
I added this script to the sheet (not in a module) and connected it to a button.
In my document I have our company logo in the headline and I kept this visible at all times.
I hope somebody find this set-up useful! :-)
Dim c As Boolean
Sub Button4_Click()
c = c Xor True
ActiveSheet.Pictures.Visible = c
ActiveSheet.Pictures("CompanyLogo").Visible = True
End Sub

Related

VBA to get selected shape name in Combobox

I am new to VBA. i have few shapes in a worksheet.
I want to get the name of shape to appear in combobox and character name in another Combobox, when any particular shape is selected. so i can rename that shape and link to particular excel column.
i have tried following.
With Selection
ActiveSheet.ComboBox1.Value = ActiveSheet.Shapes(Application.Caller).Name
End with
Not sure where to assign above code.
I tried assigning above code to a shape with .onaction as macro, it work but a marco assiged shape cannot be edited further(For design purpose).
Also It would be great if i can delete selected shape.
Thank you in advance.
You can use your code for any shape and you can change the code whenever you want, but assigning a macro, she shape will will not be selected when clicked... It becomes a kind of control.
Excepting the case when you force it to select:
Debug.Print ActiveSheet.Shapes(Application.Caller).Name
shW.ComboBox1.value.Shapes(Application.Caller).Select
You can change the code from right click context (on the chart bottom side) and choose 'Assign Macro... -> Edit'.
You can find the selected shape using the next code:
Sub testSelectedShape()
Dim shW As Worksheet, sh As Object, selSh As Object
Set shW = ActiveSheet
If TypeName(Selection) <> "Range" Then
Set selSh = Selection
Set sh = shW.Shapes(selSh.Name)
Debug.Print selSh.Name
shW.ComboBox1.value = selSh.Name
End If
End Sub
You can delete it simple using sh.Delete...

Click on cell, Have range of data display through linked picture

First time posting, hope this all makes sense. I have created a graphic in excel that shows 88 lines 1. In another worksheet there are rows of data documented what is on each line 2 When a line is clicked on the graphic I want the corresponding data from the other worksheet to pop up 3
I have managed it achieve this by making a transparent button/shape over each line to hide and show a linked image 4. However, I've had to create a separate button and macro for each line with 100+ lines this seems every inefficient. this is the code I used :
Sub LINE1A1()
With ActiveSheet.Shapes("Rectangle 9").TextFrame2.TextRange.Characters
If .Text = "Hide" Then
.Text = "Show"
ActiveSheet.Shapes("Picture 3").Visible = False
Else
.Text = "Hide"
With ActiveSheet.Shapes("Rectangle 9")
ActiveSheet.Shapes("Picture 3").Left = .Left + .Width
ActiveSheet.Shapes("Picture 3").Top = .Top + .Height
ActiveSheet.Shapes("Picture 3").Visible = True
End With
End If
End With
End Sub
What would be a better way to achieve this? side not I used linked image as the data and range of data is subject to changes as each line could have more than 1 row of data.
You can create those buttons or shapes with a macro (as you said for example 100 buttons) and move them with a macro so they are in the right place. They are going to use the same macro. It will just do a different thing depending where the button is or maybe better depending on its name.
There is a nice tutorial.
https://www.youtube.com/watch?v=bF28JhlC7yc&index=18&list=PLpOAvcoMay5SE3XTp2YN2v6NcJuXKM9KX
Here's a simple example where a common Sub uses Application.Caller to figure out which shape triggered it.
Sub Tester()
Dim s As Shape, c, s2 As Shape
c = Application.Caller '<< name of your clicked shape
Debug.Print c
Set s = ActiveSheet.Shapes(c) '<< the clicked shape
With s.TextFrame2.TextRange.Characters
'here you need some way to transform the name of the clicked
' shape to get the name of the other shape to be shown/hidden/moved
Set s2 = ActiveSheet.Shapes(c & "_linked")
s2.Visible = (.Text = "Show")
.Text = IIf(.Text = "Show", "Hide", "Show") 'toggle text
End With
End Sub

Excel: Hiding a group of objects based on the contents of the shape

Within Excel, I have created a group of shapes, these comprise of some check boxes and a white rectangle. I've named these "grouponeone". I have also created a rectangle called "oneonebutton"
What I would like to happen is that when the rectangle "oneonebutton" is inactive the text frame references a a named cell (call it "namedcell"). When clicked it changes to "Select Options" and the group is shown underneath the button. The check boxes, when clicked plot scatter values on a graph. When clicked again it reverts back to initial state.
This is my code so far, but I'm stuck and being new to VBA I've been hacking about with out much luck. Help very much appreciated.
Sub checkboxmacro()
If SelShp.TextFrame2.TextRange.Characters.Text = Range("namedcell") Then
group11.Visible= False
SelShp.TextFrame2.TextRange.Characters.Text = "Select Options"
Else
group11.Visible = True
SelShp.TextFrame2.TextRange.Characters.Text = Range("experimentoneonename")
End If
End Sub
Cheers in advance
What you have so far looks OK to me (some suggested changes below).
Sub Tester()
Const STR_SELECT As String = "Select Options (click here when done)"
Dim shp As Shape, tr As TextRange2, grp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
Set tr = shp.TextFrame2.TextRange
Set grp = ActiveSheet.Shapes("grpOne")
If tr.Characters.Text <> STR_SELECT Then
tr.Characters.Text = STR_SELECT
grp.Visible = True
Else
tr.Characters.Text = ActiveSheet.Range("namedcell")
grp.Visible = False
End If
End Sub
How to proceed might depend in part on what type of checkboxes you have.
Eg: if you use Forms checkboxes then you can link them all up tp something like the sub below (switching the action you take based on the name of the checkbox)
Sub Checker()
Dim ac As String
ac = Application.Caller
Debug.Print ac, ActiveSheet.CheckBoxes(ac).Value
'do something based on checkbox name and value...
End Sub

Set cell link at run time for Forms Check Box

I am working on pulling data out of a standardized form in excel. There is a Forms Control CheckBox that I need the state of. Apparently the only way to get this is from the cell link, where the value is placed into a cell. The problem is, whomever put this form together did not set a cell link. Is there any way to do this using VBA at run time. There are many of these forms that I must go through, so I'm trying to avoid doing it manually.
I think you are referring to a forms checkbox control placed on a worksheet, in which case you can get the state of the control without setting a cell link. Like this:
Sub HTH()
Dim iLoop As Integer
'// Get value of check box by its index
MsgBox (GetCheckBoxState(1))
'// Get value of check box by its name
MsgBox (GetCheckBoxState("Check Box 1"))
'// Loop through all checkboxes and get values
For iLoop = 1 To ActiveSheet.CheckBoxes.Count
MsgBox (GetCheckBoxState(iLoop))
Next
End Sub
Function GetCheckBoxState(vCheckBox As Variant) As String
Select Case ActiveSheet.CheckBoxes(vCheckBox).Value
Case xlOn
GetCheckBoxState = "Checked"
Case xlOff
GetCheckBoxState = "UnChecked"
Case xlMixed
GetCheckBoxState = "Mixed"
End Select
End Function
If you are referring to a check box control on a userform then as Tim pointed out it should be a case of something like:
MsgBox (UserForm1.CheckBox1.Value)

excel 2010 vba image logo swap switch

Have several logo images (similarly sized) inserted on a resources sheet. Need a way for users to easily select which company they want, and have that logo replace the default logo in upper left corner of several sheets.
Want to use a dropdown menu, have used dynamically before with great results. Dropdown can be in a userform or on just out on dashboard sheet. I already looked as stacking logos and trying a z-axis switch but Excel doesn't seem to support this. I've also tried .Replace and .Copy.
Again, logos are already pasted into a resource sheet that gets hidden, so I don't want users to go hunt down an image directory nor rely on an internet connection to fetch the image (they sometimes work offline). A default image is already emplaced in upper left corners, just need a way to match their (text) company selection to the corresponding logo image/name, and then switch the old logo with new on one several pages that I specify, in the same upper left corner.
Edit:
Here's a mishmash of what's I've tried so far, various lines uncommented at various times, and at this point some lines really don't make sense in the way it's presented. Only posting for street cred I guess. I'm just trying to get one small feature figured out, not asking anyone to write my program for me (which is a big difference in scope):
Private Sub CompanySelectComboBox_Change()
If CompanySelectComboBox.Value <> "Select a company" Then
' select logo here Sheets(Sheets("TaskNew").Index + TaskSheetsComboBox.ListIndex + 1).Activate
'Private Sub TaskSheetsComboBox_Click()
'If TaskSheetsComboBox.Value <> "Go directly to a yellow task sheet" Then
' Sheets(Sheets("TaskNew").Index + TaskSheetsComboBox.ListIndex + 1).Activate
'End If
'End Sub
MsgBox CompanySelectComboBox.Value
MsgBox CompanySelectComboBox.ListIndex
Image("Logo").Replace Image("Logo"), Sheets("Config").Image("Logo2")
'Logo.Select
' another possibility:
' LogoPic.Picture = LoadPicture(Fname)
' another possibility:
'Sheets("Configs").Image("Logo").Copy Before:=Sheets("TaskEnd")
' another possibility:
'CodeNames of Sheets
'Sheets("Configs").Shapes("Picture 1").Copy
'Sheets("Dashboard").Range("A1").PasteSpecial
Else
' user didn't select a company, so just keep default (Generic) for now
End If
End Sub
Try
http://www.officevb.com/2009/11/utilizando-o-procv-com-imagens.html
Well, after your explanation I changed -1 to +1. Let's break the problem into parts.
First, in your resource sheet, place your pictures in column B. Give each picture a (company name) in column A. You can adjust the row height so each picture fits into it's own row.
Then this is an example how you associate the names with those pictures:
Dim sh As Worksheet, pic As Shape
Set sh = ThisWorkbook.Worksheets("Pictures")
For Each pic In sh.Shapes
If pic.Type = msoPicture Then
Debug.Print pic.TopLeftCell.Cells(1, 0) ' print the company name
End If
Next
Now, you can create a combo box or user dialog from that, ask the user which company he wants and let him select a name. Here is an example function to copy a picture of a given name to the clipboard:
Function CopyLogoToClipboard(picName As String) As Boolean
Dim sh As Worksheet, pic As Shape
Set sh = ThisWorkbook.Worksheets("Pictures")
For Each pic In sh.Shapes
If pic.Type = msoPicture And pic.TopLeftCell.Cells(1, 0) = picName Then
pic.Copy
CopyLogoToClipboard = True
Exit Function
End If
Next
CopyLogoToClipboard = False
End Function
(don't forget to check the return value when you use it).
Now, the last part is to insert the logo to the places where you want it. For example, putting it in the upper left corner on the active sheet:
ActiveSheet.Paste
Set pic = Selection.ShapeRange(1)
pic.Top = 0
pic.Left = 0
Hope this helps.

Resources