Change Themes colours when I open workbook

oberti

New Member
Joined
Dec 9, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I wanted to know if it´s possible to change the theme color every time I open an excel workbook with vba.

Every time I open my workbook I would like to randomly switch between the available color themes

Thanks in advance.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
1727786706163.png
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Change Themes colours when I open workbook
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
First, place the following code in a regular module (Visual Basic Editor ~ Insert ~ Module)...

VBA Code:
Sub setThemeColorScheme()

    Dim themeColorSchemeFolder As String
    Dim themeColorSchemeFile As String
 
    themeColorSchemeFolder = "C:\Program Files (x86)\Microsoft Office\root\Document Themes 16\Theme Colors\" 'change the path accordingly
 
    If Right$(themeColorSchemeFolder, 1) <> "\" Then
        themeColorSchemeFolder = themeColorSchemeFolder & "\"
    End If
 
    themeColorSchemeFile = getRandomThemeColorScheme(themeColorSchemeFolder)
 
    If Len(themeColorSchemeFile) > 0 Then
        ThisWorkbook.Theme.themeColorScheme.Load themeColorSchemeFolder & themeColorSchemeFile
    End If
 
End Sub

Private Function getRandomThemeColorScheme(ByVal themeColorSchemeFolder As String) As String

    Dim arrThemeColorFileNames() As String
    Dim currentFileName As String
    Dim fileCount As Long
    Dim fileIndex As Long
 
    If Right$(themeColorSchemeFolder, 1) <> "\" Then
        themeColorSchemeFolder = themeColorSchemeFolder & "\"
    End If
 
    currentFileName = Dir(themeColorSchemeFolder & "*.xml", vbNormal)
 
    If Len(currentFileName) = 0 Then
        getRandomThemeColorScheme = vbNullString
        Exit Function
    End If
 
    ReDim arrThemeColorFileNames(1 To 50)
 
    fileCount = 0
    Do
        fileCount = fileCount + 1
        arrThemeColorFileNames(fileCount) = currentFileName
        If fileCount Mod 50 = 0 Then
            ReDim Preserve arrThemeColorFileNames(1 To (fileCount + 50))
        End If
        currentFileName = Dir
    Loop While (Len(currentFileName) > 0)
 
    ReDim Preserve arrThemeColorFileNames(1 To fileCount)
 
    Randomize
    fileIndex = Int((fileCount * Rnd) + 1)
 
    getRandomThemeColorScheme = arrThemeColorFileNames(fileIndex)
 
End Function

Then place the following code in the module for ThisWorkbook (right-click ThisWorkbook in the Project Explorer Window for your workbook)...

VBA Code:
Private Sub Workbook_Open()
    setThemeColorScheme
End Sub

Now save, close, and re-open your workbook. A random theme color should automatically be chosen for your workbook.

Hope this helps!
 
Upvote 0
Solution
First, place the following code in a regular module (Visual Basic Editor ~ Insert ~ Module)...

VBA Code:
Sub setThemeColorScheme()

    Dim themeColorSchemeFolder As String
    Dim themeColorSchemeFile As String
 
    themeColorSchemeFolder = "C:\Program Files (x86)\Microsoft Office\root\Document Themes 16\Theme Colors\" 'change the path accordingly
 
    If Right$(themeColorSchemeFolder, 1) <> "\" Then
        themeColorSchemeFolder = themeColorSchemeFolder & "\"
    End If
 
    themeColorSchemeFile = getRandomThemeColorScheme(themeColorSchemeFolder)
 
    If Len(themeColorSchemeFile) > 0 Then
        ThisWorkbook.Theme.themeColorScheme.Load themeColorSchemeFolder & themeColorSchemeFile
    End If
 
End Sub

Private Function getRandomThemeColorScheme(ByVal themeColorSchemeFolder As String) As String

    Dim arrThemeColorFileNames() As String
    Dim currentFileName As String
    Dim fileCount As Long
    Dim fileIndex As Long
 
    If Right$(themeColorSchemeFolder, 1) <> "\" Then
        themeColorSchemeFolder = themeColorSchemeFolder & "\"
    End If
 
    currentFileName = Dir(themeColorSchemeFolder & "*.xml", vbNormal)
 
    If Len(currentFileName) = 0 Then
        getRandomThemeColorScheme = vbNullString
        Exit Function
    End If
 
    ReDim arrThemeColorFileNames(1 To 50)
 
    fileCount = 0
    Do
        fileCount = fileCount + 1
        arrThemeColorFileNames(fileCount) = currentFileName
        If fileCount Mod 50 = 0 Then
            ReDim Preserve arrThemeColorFileNames(1 To (fileCount + 50))
        End If
        currentFileName = Dir
    Loop While (Len(currentFileName) > 0)
 
    ReDim Preserve arrThemeColorFileNames(1 To fileCount)
 
    Randomize
    fileIndex = Int((fileCount * Rnd) + 1)
 
    getRandomThemeColorScheme = arrThemeColorFileNames(fileIndex)
 
End Function

Then place the following code in the module for ThisWorkbook (right-click ThisWorkbook in the Project Explorer Window for your workbook)...

VBA Code:
Private Sub Workbook_Open()
    setThemeColorScheme
End Sub

Now save, close, and re-open your workbook. A random theme color should automatically be chosen for your workbook.

Hope this helps!
Fantastic , it' working

THANKS
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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