List all Items in Named Range in MsgBox

johnny51981

Active Member
Joined
Jun 8, 2015
Messages
398
Howdy,
I'm trying to update the following VBA to be dynamic based on the population of a table, rather than being written in the vba.
VBA Code:
Public Sub RefreshAll()
' Macro to Refresh All Queries
' Keyboard Shortcut: Ctrl+r

' Refreshes the File Date queries
    Application.CommandBars("Queries and Connections").Visible = True
    Application.CommandBars("Queries and Connections").Width = 700 'Change width as suits.
    DoEvents
         
' Message Box to begin Refresh

        
    Dim answer1 As Integer
    answer1 = MsgBox("Would you like to refresh all the Data?" & vbNewLine & vbNewLine &"Note: Please make sure you added the following updated files to the Raw Data Folder:" & vbNewLine & vbNewLine & "File1.csv" & vbNewLine & vbNewLine & "File2.xlsx" & vbNewLine & vbNewLine & "File3.xlsx" & vbNewLine & vbNewLine & "File4.xlsx" & vbNewLine & vbNewLine & "File5.xlsx" & vbNewLine & vbNewLine & "File6.xlsx" & vbNewLine & vbNewLine & "File7.xlsx" & vbNewLine & vbNewLine & "File8.xlsx" & vbNewLine & vbNewLine & "File9.xlsx" & vbNewLine & vbNewLine & "File10.xlsx", vbQuestion + vbYesNo + vbDefaultButton2, "Refresh All Data")
    If answer1 = vbYes Then
 
' Refreshes all queries, provides Refresh Start and Stop Times, and Refresh Status
    Call Refresh_QueriesOnly
    answer1 = MsgBox("Data will now refresh.", vbOKOnly)
    Else: answer1 = MsgBox("Data refresh has been cancelled.", vbOKOnly)
    End
    End If
' Changes Report Enviornment to "Production"
    Call ReportEnvironment_Production
    
End Sub

Instead of the File names being written, I would like to use the Named Range of "SourceDataFiles" that is already scoped for the Workbook. And it references a Table that has a column of File Names.
 
Ok, then I'm getting a type mismatch error with this:
VBA Code:
Public Sub RefreshAll()

' Macro to Refresh All Queries
' Keyboard Shortcut: Ctrl+r

' Refreshes the File Date queries
    Application.CommandBars("Queries and Connections").Visible = True
    Application.CommandBars("Queries and Connections").Width = 700 'Change width as suits.
    DoEvents
         
' Message Box to begin Refresh

        
    Dim answer1 As Integer
        answer1 = MsgBox("Would you like to refresh all the Data?" _
                    & vbNewLine & vbNewLine & _
                    "Note: Please make sure you added the following updated files to the Raw Data Folder:" _
                    & vbNewLine & vbNewLine & Join([transpose(SourceDataFiles)], vbLf))
    If answer1 = vbYes Then
 
' Refreshes all queries, provides Refresh Start and Stop Times, and Refresh Status
    Call Refresh_QueriesOnly
    answer1 = MsgBox("Data will now refresh.", vbOKOnly)
    Else: answer1 = MsgBox("Data refresh has been cancelled.", vbOKOnly)
    End
    End If
' Changes Report Enviornment to "Production"
    Call ReportEnvironment_Production
    
End Sub
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
It would be best if you could censor any sensitive information and share the file via Dropbox/Google Drive/whatever, it's hard to troubleshoot like this.
 
Upvote 0
This should work?

VBA Code:
Public Sub RefreshAll()

' Macro to Refresh All Queries
' Keyboard Shortcut: Ctrl+r

' Refreshes the File Date queries
    Application.CommandBars("Queries and Connections").Visible = True
    Application.CommandBars("Queries and Connections").Width = 700 'Change width as suits.
    DoEvents
        
' Message Box to begin Refresh

        
    Dim answer1 As Integer
        answer1 = MsgBox("Would you like to refresh all the Data?" _
                    & vbNewLine & vbNewLine & _
                    "Note: Please make sure you added the following updated files to the Raw Data Folder:" _
                    & vbNewLine & vbNewLine & Join([transpose(SourceFileNames)], vbLf))
    If answer1 = vbYes Then
 
' Refreshes all queries, provides Refresh Start and Stop Times, and Refresh Status
    Call Refresh_QueriesOnly
    answer1 = MsgBox("Data will now refresh.", vbOKOnly)
    Else: answer1 = MsgBox("Data refresh has been cancelled.", vbOKOnly)
    End
    End If
' Changes Report Enviornment to "Production"
    Call ReportEnvironment_Production
    
End Sub
 
Upvote 1
Solution
Ok, then I'm getting a type mismatch error with this:

In your earlier post the named range was SourceFileNames, but your code references SourceDataFiles

VBA Code:
Join([transpose(SourceDataFiles)], vbLf)

You will have to change either the code (as per @Engberg's last post) or else change the name of the named range to SourceDataFiles
 
Upvote 0
This should work?

VBA Code:
Public Sub RefreshAll()

' Macro to Refresh All Queries
' Keyboard Shortcut: Ctrl+r

' Refreshes the File Date queries
    Application.CommandBars("Queries and Connections").Visible = True
    Application.CommandBars("Queries and Connections").Width = 700 'Change width as suits.
    DoEvents
       
' Message Box to begin Refresh

       
    Dim answer1 As Integer
        answer1 = MsgBox("Would you like to refresh all the Data?" _
                    & vbNewLine & vbNewLine & _
                    "Note: Please make sure you added the following updated files to the Raw Data Folder:" _
                    & vbNewLine & vbNewLine & Join([transpose(SourceFileNames)], vbLf))
    If answer1 = vbYes Then
 
' Refreshes all queries, provides Refresh Start and Stop Times, and Refresh Status
    Call Refresh_QueriesOnly
    answer1 = MsgBox("Data will now refresh.", vbOKOnly)
    Else: answer1 = MsgBox("Data refresh has been cancelled.", vbOKOnly)
    End
    End If
' Changes Report Enviornment to "Production"
    Call ReportEnvironment_Production
   
End Sub
It does! Thank you!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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