I have this code in VBA that builds a list of all currently opened workbooks, and adds them to a named range, so that I can use that as input for a selection list. This way the user is able to select the workbook that should be processed.
Sub Lijst_bouwen()
Dim i As Integer
Dim wb_pnt As Workbook
Set wb_pnt = Application.ThisWorkbook
Dim wb_overzicht As Workbook
Dim ws As Worksheet
Set ws = wb_pnt.Worksheets("Werkboeken")
For i = 1 To Workbooks.Count 'build list of open workbooks
ws.Cells(i, 1) = Workbooks(i).Name
Next i
Dim Rng As Range
Set Rng = ws.Range(Cells(1, 1), Cells(i - 1, 1))
wb_pnt.Names.Add Name:="Lijst", RefersTo:=Rng
End Sub
As a regular sub this works perfectly fine.
When I try to run it as a Workbook_Open()
sub it doesn't.
The first line of the code then becomes:
Private Sub Workbook_Open()
But no results are presented, nor any error messages.