help again, user defined colors

bensonsearch

Well-known Member
Joined
May 26, 2011
Messages
844
Hi All,
Im stuck again. im sure its simple but cant figure it out.

What I want is to be able for a user to select a color or image that will apply to all forms (so the sake of it form1-5). this is for user uniquness.

I can get the color and image working fine if manually selected but i cant get it to store the preferance and load next time.

it all works as so, user starts program with default style, user changes background to either a color or an image. (now the for some reason hard part) storing what the user has selected perminatley (or until changed again) and applying to all forms.

the way i have tried is storing the info in a txt file (eg: c:/image1.jpg or &H00FF0000&) the coding can load the image part fine but if i try the color code it cant do it (passed by a variable with userform1.backcolor=variable1) plus i have no idea how to code the logic of finding if its an image location or a color. have to code one or the other from what im seeing.

hope this all makes sense and someone can help me out. any way so long as simple :)
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
When the user selects a new colour, save the colour in a hidden worksheet:-
Code:
Private Sub UserSelectsColour_Click()
   UserForm1.BackColor = TextBox1.Value [COLOR=green]' (or wherever the user put the colour code)[/COLOR]
   ThisWorkbook.Sheets("HiddenConfigSheet").Range("A1") = TextBox1.Value
End Sub

Then when you load the form, set the colour from wherever you saved it:-
Code:
Private Sub UserForm_Initialize()
   UserForm1.BackColor = ThisWorkbook.Sheets("HiddenConfigSheet").Range("A1").Value
End Sub

Use a similar approach for the background picture.

To decide whether the user has entered a colour or a file name, use Dir() to check whether whatever he has entered exists as a file:-
Code:
If Dir(TextBox1.Value) <> "" Then
[COLOR=green]   ' a file exists with that name - set the form's .Picture[/COLOR]
Else
[COLOR=green]   ' it's a colour - set the form's .BackColor[/COLOR]
End If
 
Last edited:
Upvote 0
Thanx for the incredible quick reply. it stores the image as a directory (from a dialog box for them to choose) thats fine but when i try and load it it says its invalid, i think because its not "" at either end. is there a way to fix that? the code im using to store is:

Dim data As String
filetoopen = Application.GetOpenFilename _
(Title:="Please choose a file to import")
''
If filetoopen = False Then
MsgBox ("No file specified.")
Exit Sub
Else
data = filetoopen
TextBox1.Text = data
ThisWorkbook.Sheets("Sheet1").Range("A2") = data
End If
 
Upvote 0
You can use the Designer object to apply the chosen color to the control directly. This will be saved with the workbook.
Code:
Sub test()
    Dim colorChosen As Long
    
    If Application.Dialogs(xlDialogEditColor).Show(arg1:=3) Then
        With ActiveWorkbook
            colorChosen = .Colors(3)
            .Colors(3) = RGB(255, 0, 0)
        End With
        With ThisWorkbook.VBProject.VBComponents("Userform1").Designer
            .CheckBox1.BackColor = (colorChosen)
        End With
    End If
    
End Sub
 
Upvote 0
well as above when i try and pass the location it doesnt work (but if i type in location it does) havent tried ur example but not sure if it would work as its backcolor, i use form1.picture = loadpicture(c:\test.jpg)
 
Upvote 0
well i have sorted it out thanx. code i used below:

Private Sub UserForm_Activate()
UserForm1.Picture = Nothing
Extras.Picture = Nothing
info.Picture = Nothing
links.Picture = Nothing
UserForm1.BackColor = &H80000005
Extras.BackColor = &H80000005
info.BackColor = &H80000005
links.BackColor = &H80000005
TextBox2.Text = ThisWorkbook.Sheets("Sheet1").Range("A2").Value
If Dir(TextBox2.Text) <> "" Then
UserForm1.Picture = LoadPicture(ThisWorkbook.Sheets("Sheet1").Range("A2").Value)
Extras.Picture = LoadPicture(ThisWorkbook.Sheets("Sheet1").Range("A2").Value)
info.Picture = LoadPicture(ThisWorkbook.Sheets("Sheet1").Range("A2").Value)
links.Picture = LoadPicture(ThisWorkbook.Sheets("Sheet1").Range("A2").Value)
Else
UserForm1.BackColor = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
Extras.BackColor = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
info.BackColor = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
links.BackColor = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
End If
End Sub


thanx again for all your help
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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
Back
Top