Hi All,
Line 42 is creating an error - User-defined type not defined - i have copied this code from a previous macro, and i cant see them defining this variable anywhere?
also would anyone know how i could incorporate another listbox for years (multiple years to be selected) and the data copied over as per the user requirments
Line 42 is creating an error - User-defined type not defined - i have copied this code from a previous macro, and i cant see them defining this variable anywhere?
also would anyone know how i could incorporate another listbox for years (multiple years to be selected) and the data copied over as per the user requirments
VBA Code:
Option Explicit
'These are global constants available throughtout the programme. They are usually initialised in the subroutine intialises
'Many take values from Parameters sheet. The programme does not change
' the value in these variables after they are intialised
Dim ThisFolder As String
Dim ProgramFile As String
Dim InputFolder As String
Dim OutputFolder As String
Dim WorkFile As String
Dim TableFolder As String
Dim FolderExtension2004 As String
Dim FolderExtension2006 As String
Dim FolderExtension2011 As String
Dim RetVal
Dim LowerYear As Integer
Dim UpperYear As Integer
Dim FirstRow As Integer
Dim KeyRow As Integer
Dim KeyCol As Integer
Dim KeyLength As Integer
Dim MaxRows As Integer
Dim RemoveLinks As Boolean
'Used by the dialog box that displays a change folers list
Public Type BRWOSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
'32-Bit API Declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszpath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Sub CreateTimseries(FileName, SheetName, VariableName, VariableCol)
Dim Count, Year, Row, Col, LastRow, Pos, HeadingColour, TitleColour, AddYear As Integer
Dim CellColour, CellPattern, CellIndex As Integer
Dim Name, LowerYearText, Folder, Text As String
If Not intialise Then Exit Sub
Sheets("Output").Select
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
On Error GoTo Error1
Workbooks.Open FileName:=InputFolder & "\" & FileName
On Error GoTo 0
Sheets(SheetName).Select
Cells(MaxRows, 3).Select
Selection.Delete Shift:=xlUp
LastRow = ActiveCell.Row
LowerYearText = Format(LowerYear, "0")
'copy across heading rows from table file to output sheet
TitleColour = Cells(1, 1).Interior.ColorIndex
headingcolor = Cells(FirstRow - 1, 1).Interior.ColorIndex
For Count = 1 To FirstRow - 1
Windows(FileName).Activate
Col = Columns("A:A").ColumnWidth
Cells(Count, 1).Copy
Windows(ProgramFile).actiavte
Col = Columns("a:a").ColumnWidth = Col
Cells(Count, 1).Select
ActiveSheet.Paste
Windows(FileName).Activate
Columns("B:B").ColumnWidth = Col
Cells(Count, 2).Select
Windows(ProgramFile).Activate
Columns("B:B").ColumnWidth = Col
Cells(Count, 2).Select
ActiveSheet.Paste
Next Count
Windows(FileName).Close
If LowerYear < 2004 Then
If UpperYear >= 2004 And UpperYear < 2006 Then
AddYear = 1
ElseIf UpperYear >= 2006 And UpperYear < 2011 Then
AddYear = 2
ElseIf UpperYear >= 2011 Then
AddYear = 3
Else
AddYear = 0
End If
ElseIf LowerYear >= 2004 And LowerYear < 2006 Then
If UpperYear >= 2006 And UpperYear < 2011 Then
AddYear = 1
ElseIf UpperYear >= 2011 Then
AddYear = 2
Else
AddYear = 0
End If
ElseIf LowerYear >= 2006 And LowerYear < 2011 Then
If UpperYear >= 2011 Then
AddYear = 1
Else
AddYear = 0
End If
Else
AddYear = 0
End If
For Count = LowerYear To UpperYear + AddYear
Col = 3 + Count - LowerYear
Windows(ProgramFile).Activate
Columns("B:B").ColumnWidth = Col
Cells(Count, 2).Select
ActiveSheet.Paste
Next Count
Windows(FileName).Close
If LowerYear < 2004 Then
If UpperYear >= 2004 And UpperYear < 2006 Then
AddYear = 1
ElseIf UpperYear >= 2006 And UpperYear < 2011 Then
AddYear = 2
ElseIf UpperYear >= 2011 Then
AddYear = 3
Else
AddYear = 0
End If
ElseIf LowerYear >= 2004 And LowerYear < 2006 Then
If UpperYear >= 2006 And UpperYear < 2011 Then
AddYear = 1
ElseIf UpperYear >= 2011 Then
AddYear = 2
Else
AddYear = 0
End If
ElseIf LowerYear >= 2006 And LowerYear < 2011 Then
If UpperYear >= 2011 Then
AddYear = 1
Else
AddYear = 0
End If
Else
AddYear = 0
End If
For Count = LowerYear To UpperYear + AddYear
Col = 3 + Count - LowerYear
Year = Count
Range(Cells(1, Col), Cells(2, Col)).Interior.Colorlodex = TitleColor
Range(Cells(1, Col), Cells(2, Col)).Interior.ColorIndex -TitleColour
Cells(2, Col).Borders(xlEdgeBottom).LineStyle -xlContinuous
Cells(2, Col).Borders(xlEdgeBottom).Weight -xlSolid
Cells(2, Col).Borders(xlEdgeBottom).Weight = xlMedium
Cells(2, Col).Borders(xlEdgeBottom).ColorIndex = xlAutomatic
Range(Cells(3, Col), Cells(FirstRow - 1, Col)).Interior.ColorIndex = HeadingColour
Cells(FirstRow - 1, Col).Borders(xlEdgeBottom).LineStyle = xlContinuous
Cells(FirstRow - 1, Col).Borders(xlEdgeBottom).Weight = xlThin
If Count >= 2004 Then
'There are two sets of data for 2004, 2006 and 2011
If Count = 2004 Then
Cells(FirstRow - 1, Col) = Year & "**"
ElseIf Count = 2006 Then
Cells(FirstRow - 1, Col) = Year & "**"
ElseIf Count = 2011 Then
Cells(FirstRow - 1, Col) = Year & "**"
Else
Year = Count - 1
Cells(FirstRow - 1, Col) = Year
End If
Else
Cells(FirstRow - 1, Col) = Year
End If
Cells(FirstRow - 1, Col).HorizontalAlignment = xlRight
Text = "Processing year: " & Year & "."
Display_Message Text
Pos = InStr(1, FileName, LowerYearText)
If Count = 2004 Then
Folder = Left(InputFolder, Pos - 1) & Format(Year, "0") & FolderExtension2004 & Right(InputFolder, 1 + Len(InputFolder) - (Pos + Len(LowerYearText)))
ElseIf Count = 2006 Then
Folder = Left(InputFolder, Pos - 1) & Format(Year, "0") & FolderExtension2006 & Right(InputFolder, 1 + Len(InputFolder) - (Pos + Len(LowerYearText)))
ElseIf Count = 2011 Then
Folder = Left(InputFolder, Pos - 1) & Format(Year, "0") & FolderExtension2011 & Right(InputFolder, 1 + Len(InputFolder) - (Pos + Len(LowerYearText)))
Else
Folder = Left(InputFolder, Pos - 1) & Format(Year, "0") & Right(InputFolder, 1 + Len(InputFolder) - (Pos + Len(LowerYearText)))
End If
On Error GoTo Error2
Workbooks.Open FileName:=Folder & "\" & Name
GoTo Continue1
Error2:
On Error GoTo 0
RetVal = MsgBox("Unable to open table: " & Name & vbCrLf & vbCrLf & "Continue with the next year?", vbYesNo)
If RetVal = vbNo Then
Display_Message ""
Exit Sub
End If
On Error GoTo O
GoTo Continue2
Continuel:
Sheets(SheetName).Select
For Row = FirstRow To LastRow
Application.StatusBar = Text & Format(100 * ((Row - 1) / LastRow), "O") & "9"
If 0 = Count - LowerYear Then
Windows(Name).Activate
Range(Cells(Row, 1), Cells(Row, 2)).Copy
Windows(ProgramFile).Activate
Cells(Row, 1).Select
ActiveSheet.Paste
Windows(Name).Activate
CellColour = Cells(Row, Val(VariableCol)).Interior.ColorIndex
CellPattern = Cells(Row, Val(VariableCol)).Interior.Pattern
CellIndex = Cells(Row, Val(VariableCol)).Interior.PatternColorIndex
Windows(ProgramFile).Activate
Cells(Row, Col).Interior.ColorIndex = CellColour
Cells(Row, Col).Interior.Pattern -CellPattern
Cells(Row, Col).Interior.PatternColorIndex = CellIndex
Cells(Row, Col).FormulaR1C1 = "='" & Name & "]" & SheetName & "'!R" & Row & "C" & VariableCol
Next Row
If RemoveLinks Then
Range(Cells(FirstRow, Col), Cells(LastRow, Col)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=x1None, SkipBlanks:= _
False, Transpose:=False
End If
Create Timeseries
If Count = UpperYear + AddYear Then
Windows(Name).Activate
Range(Cells(KeyRow, KeyCol), Cells(KeyRow + KeyLength - 1, KeyCol)).Copy
Windows(ProgramFile).Activate
Cells(KeyRow, Col + 2).Select
ActiveSheet.Paste
Columns(Col + 2).ColumnWidth -21.3
End If
Windows(Name).Close
Continue2:
Next Count
Sheets("Output").Select
ActiveWindow.DisplayZeros = False
Pos -InStr(1, FileName, LowerYearText)
Name = Left(FileName, Pos - 1) & LowerYearText & "_" & Format(UpperYear, "0") & Right(FileName, 1 + Len(FileName) - (Pos + Len(LowerYearText)))
Display_Message "Saving time series file to: " & OutputFolder & SheetName & " " & VariableName & " & Name"
Sheets("Output").Copy
ActiveWorkbook.SaveAs FileName:=OutputFolder & SheetName & "" & VariableName & "" & Name, _
FileFormat:=xlNormal, Password:="", WriteResPassword:="*", _
ReadOnlyRecommended:=False, CreateBackup:=False
Sheets("Output").Name = VariableName
Cells(4, 3).value = VariableName
Range(Cells(4, 3), Cells(4, 4 + (UpperYear - LowerYear))).Select
With Selection
HorizontalAlignment = xlCenter
VerticalAlignment = xlBottom
.WrapText = False
Orientation = 0
.ShrinkToFit = False
.MergeCells -True
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = x1None
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Range(Cells(LastRow, 3), Cells(LastRow, 4 + (UpperYear - LowerYear))).Select
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("A1").Select
Create_Growth Sheet
ActiveWorkbook.Close savechanges:=True
Sheets("Output").Select
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Sheets("Start").Select
Cells(18, 1).value = OutputFolder & SheetName & "" & VariableName & "" & Name
Display_Message
Exit Sub
Errorl:
MsgBox "Unable to open first table: " & InputFolder & "\" & FileName
Display_Message ""
End Sub
Sub OpenTSFile()
Dim FileName, VariableName, Name, LowerYearText, Folder, Text As String
Dim Pos As Integer
FileName = Sheets("Start").Cells(18, 1).value
FileName -Sheets("Start").Cells(18, 1).value
If "" = FileName Then
MsgBox "No timeseries name found in cell A18 of the Start sheet.", vbCritical
Exit Sub
End If
On Error GoTo Error2
Workbooks.Open FileName:=FileName
On Error GoTo 0
Exit Sub
Error2:
MagBox "Unable to open file:= & FileName, vbcritical"
Display_Message ""
End Sub
Sub Create()
Growth Sheet()
Dim Lastcol, Count As Integer
Dim Name As String
Sheets(1).Select
Name = Sheets(1).Name
Sheets(1).Copy After:=Sheets(1)
Sheets(2).Select
Sheets(2).Name = "Growth"
Cells(6, 3).Select 'Delete first column of data, no data to calculate growth rate on
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("d6").Select
ActiveCell.FormulaR1C1 = _
"=If (AND (" & Name & "!RC>0," & Name & "!RC[-1]>0), 100 * (" & Name & "!RC/" & Name & "!RC[-1]-1),"""")"
Selection.Number Format - "0.0"
Cells(5, 40).Select
Selection.End(xlToLeft).Select
Lastcol -ActiveCell.Column
Range("06").Select
Selection.Copy
Range(Cells(6, 4), Cells(6, Lastcol)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
For Count = 4 To Lastcol
If Cells(5, Count).value - "2004" Then
'Delete extra 2004 column as there are two 2004 results
Cells(6, Count).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
ElseIf Cells(5, Count).value = "2006" Then
ElseIf Cells(5, Count).value = "2006" Then
'Delete extra 2006 column as there are two 2006 results
Cells(6, Count).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
ElseIf Cells(5, Count).value = "2011" Then
'Delete extra 2011 column as there are two 2011 results
Cells(6, Count).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
End If
Next Count
Range("A1").Select
End Sub
Function Initialise() As Boolean
'Sets global constants.
'Values are mainly read from the parameters sheet, although some come from the Start sheet
'Folder names depend on the location of this workbook.
Dim Text As String
Dim Count As Integer, Pos, LastPosi, LastPos2 As Integer
Initialise = False
Application.ScreenUpdating -False
ThisFolder -ActiveWorkbook.path
ThisFolder = ThisFolder & ""
ProgramFile -ActiveWorkbook.Name
'Folder name containing the files to be imported
'Input input folder from user if folder not given in cell J15 of Start sheet
InputFolder = Sheets("Start").Range("C25").value
If InputFolder = Empty Then
InputFolder = GetDirectory
If InputFolder = "" Then Exit Function
Sheets("Start").Range("C25").value = InputFolder
End If
'Append on to workfolder name if it does not already have a
InputFolder = InputFolder & IIf("\" = Right(InputFolder, 1), "", "\")
'Folder name to where files are to be output
'Output Output folder from user if folder not given in cell J15 of Start sheet
Output Fold - Sheets("Start").Range("C26").value
If OutputFolder = Empty Then
OutputFolder = GetDirectory
If OutputFolder = "" Then Exit Function
Sheets("Start").Range("C28").value = OutputFolder
End If
'Append on to workfolder name if it does not already have a
OutputFolder = OutputFolder & IIf("\" - Right(OutputFolder, 1), "", "\")
LowerYear -Sheets("Start").Range("C25").value
'Extract year from filename
Last Posl - 1
Last Pos2 = 1
For Count = 1 To 20
Pos = InStr(LastPoul + 1, LowerYear, "")
If Poy = 0 Then Exit For
LastPos2 = LastPosl
LastPosl = Pos
Next Count
LowerYear = Mid(LowerYear, LastPos2 + 1, 4)
UpperYear -Sheets("Parameters").Range("D4")
FirstRow = Sheets("Parameters").Range("04").value
FolderExtension 2004 = Sheets("Parameters").Range("F4").value
FolderExtension 2006 = Sheets("Parameters").Range("F5").value
FolderExtension 2011 = Sheets("Parameters").Range("F6").value
KeyRow = Sheets("Parameters").Range("G4").value
KeyCol = Sheets("Parameters").Range("G5").value
KeyLength = Sheets("Parameters").Range("G6").value
RemoveLinks = False
If "YES" = UCase(Sheets("Parameters").Range("H4").value) Then
RemoveLinks = True
End If
MaxRows = Sheets("Parameters").Range("I4").value
Initialise = True
End Function
Function GetDirectory(Optional Msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, Pos As Integer
'Root Folder - Desktop
bInfo.pidlRoot = 0&
'Title in the dialog
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If
'Type of directory to return
bInfo.ulFlags = &H1
'Display the dialog
x = SHBrowseForFolder(bInfo)
'Parse the result
path = Spaces(512)
r -SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
Pos -InStr(path, Chr$(0))
GetDirectory -Left(path, Pos - 1)
Else
GetDirectory = ""
End If
End Function
Sub Display_Message(Message As String)
'Displayeg a message on the status bar at the bottom of the screen.
'Text = the message to display. If blank then the status bar will be reset to the default.
Application.ScreenUpdating = False
If Len(Message) > 0 Then
Application.StatusBar -Message & " Please wait....."
Else
Application.StatusBar -False
End If
End Sub
Sub TEST()
Dim value
value = Cells(298, 4).value
If value = "" Then
Stop
End If
End Sub