Option Explicit
Public F_Nam As String
Private Sub Workbook_Open()
Application.WindowState = xlMaximized
Application.EnableEvents = True
End Sub
Sub Load_CSV()
' Called by the "Load File" button in the Master Tab
Dim WS_Sht As Worksheet
Dim WS_Name As String
Dim CSV_Name As String
Dim NT_Nam As String 'New Tab Name
Dim L_Row As Long
Dim rng As Range
On Error GoTo 0
' Find the data input sheet, named statement_yyyy-mm
Application.ScreenUpdating = False
ActiveWorkbook.Sheets("Master").Activate
Call WorksheetLoop
ActiveWorkbook.Sheets("Master").Activate
NT_Nam = Range("Input_tab").Value
ActiveWorkbook.Sheets("Master").Activate
' Puts the Tab name into Master!B2
' Range("Master!B2").Select
' Selection.Value = "statement_" + F_Nam
' Selection.Copy
' Selection.PasteSpecial Paste:=xlValues
' Application.CutCopyMode = False
WS_Name = Range("B17").Value
Set WS_Sht = ActiveWorkbook.Worksheets(NT_Nam)
WS_Sht.Activate
' Delete any existing data
Range("A1").Value = Range("Master!B19").Value
ActiveSheet.Name = NT_Nam
L_Row = WS_Sht.Cells(WS_Sht.Rows.Count, "A").End(xlUp).Row
Set rng = Range("A2:A" & L_Row)
If Not rng Is Nothing Then rng.EntireRow.Delete
'Import CSV file
CSV_Name = Range("Master!B3").Value & Range("Master!B17").Value
Call ImportFile("A2", CSV_Name)
Application.EnableEvents = True
Call Entry_Sht
End Sub
Sub WorksheetLoop()
' Finds the Tab with the downloaded data
Dim WS_Count As Integer
Dim WS_Name As String
Dim WS_Sht As Worksheet
Dim I As Integer
WS_Count = ActiveWorkbook.Worksheets.Count
For I = 1 To WS_Count
Set WS_Sht = ActiveWorkbook.Worksheets(I)
ActiveWorkbook.Worksheets(I).Activate
If Mid(WS_Sht.Name, 1, 9) = "statement" Then
ActiveSheet.Name = Sheets("Master").Range("$B$14").Value
Exit Sub
End If
Next I
End Sub
Sub Entry_Sht()
'Setup Entry sheet
Dim Col_Range As Range
Dim Paste_Range As Range
Dim rng As Range
Dim E_Msg As String
Dim M_Title As String
Dim M_Ans As Integer
Dim Last_Row As Integer
Dim C_Row As Integer
Dim S_Sheet As String
Dim S_Rng1 As String
Dim S_Rng2 As String
Dim S_Val As Variant
Dim Test_Val As String
Application.EnableEvents = True
' Delete existing data
ActiveWorkbook.Sheets("Entry").Activate
Last_Row = Worksheets("Entry").Cells(Worksheets("Entry").Rows.Count, "A").End(xlUp).Row
Set rng = Range("A12:A" & Last_Row)
On Error GoTo 0
If Not rng Is Nothing Then rng.EntireRow.Delete
' Copy values across from Statement_xxxxx to Entry Tabs
ActiveWorkbook.Sheets("Entry").Activate
Range("Q:Q").NumberFormat = "@"
Range("G12").Select
C_Row = 12
S_Sheet = ThisWorkbook.Worksheets("Master").Range("Input_tab").Value
Do While Worksheets(S_Sheet).Range("C" & C_Row - 10).Value > 0
Sheets("Entry").Activate
Range("A" & C_Row).Value = "Barclaycard Business"
Range("B" & C_Row).Value = [File_Nam] & "-" & C_Row - 11
Range("C" & C_Row).Value = Sheets(S_Sheet).Range("A" & C_Row - 10).Value
Range("D" & C_Row).Value = Range("C" & C_Row).Value + 31
Range("E" & C_Row).Value = Sheets(S_Sheet).Range("B" & C_Row - 10).Value
Range("F" & C_Row).Value = 1
Sheets(S_Sheet).Activate
S_Val = Range("G" & C_Row - 10).Value + Range("F" & C_Row - 10).Value
Sheets("Entry").Activate
With Range("G" & C_Row)
.Value = S_Val
.NumberFormat = "#,##0.00;[Red]#,##0.00"
End With
Range("P" & C_Row).Value = Range("G" & C_Row).Value
' Range("Q" & C_Row).Value = Left(Range("H" & C_Row).Value, 3)
C_Row = C_Row + 1
Loop
ActiveWorkbook.Sheets("Entry").Activate
ActiveWindow.DisplayZeros = False
Rows("2:999").EntireRow.Hidden = False
Set Col_Range = Range("C12:C500")
Last_Row = Application.WorksheetFunction.CountIf(Col_Range, ">0")
Range("Entry!A1").Select
Range("Entry!h12").Value = Range("Master!B152").Value
Range("Entry!i12").Value = Range("Master!B94").Value
Range("h12:i12").Select
Selection.Copy
Range("h13:i" & Last_Row + 11).Select
ActiveSheet.Paste
Range("Q12").Value = Left(Range("H12").Value, 3)
Range("Q12").Select
Selection.Copy
Range("Q13:Q" & Last_Row + 11).Select
ActiveSheet.Paste
With Range("J:J")
.NumberFormat = "#,##0.00;[Red]#,##0.00"
End With
Application.CutCopyMode = False
' Blank rows which say Payment
Range("E12").Select
C_Row = 12
Do While Range("C" & C_Row).Value > 0
Range("E" & C_Row).Select
If StrComp("Payment", Left(Trim(ActiveCell.Value), 7), vbTextCompare) = 0 Then
Range("G" & C_Row).Value = 0
Rows(ActiveCell.Row).EntireRow.Hidden = True
End If
C_Row = C_Row + 1
Loop
Rows(Last_Row + 12 & ":999").EntireRow.Hidden = True
Range("A12").Select
ActiveWindow.FreezePanes = True
Application.EnableEvents = True
'User Message & instructions
E_Msg = "Please set the appropriate Cost Code & VAT for each line"
E_Msg = E_Msg & vbCr & "Values are selected from a table"
M_Title = "Next"
M_Ans = MsgBox(E_Msg, vbYesNo, M_Title)
If M_Ans = vbYes Then
' Call Check_10
ElseIf M_Ans = vbNo Then
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
' End of the data entry sequence. User selects cells in Col H or I which need altering
End Sub
Sub Export_Sheet()
' Export data. Called by the "Save" button on the Entry Tab
Dim Col_Range As Range
Dim Last_Row As Integer
Dim Local_Dir As String
Dim C_Row As Integer
Application.ScreenUpdating = False
On Error GoTo 0
' Get the Save Directory Name
Local_Dir = Sheets("Master").Range("Save_Dir").Value
'Clear previous entry
Sheets("Output").Select
Range("A1:M999").Clear
' Copy entered data & paste as values in the Output Tab
ActiveWorkbook.Sheets("Entry").Activate
Set Col_Range = Range("C12:C500")
Last_Row = Application.WorksheetFunction.CountIf(Col_Range, ">0")
Range("A11:J" & Last_Row + 11).Select
Selection.Copy
Sheets("Output").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Range("C:D").NumberFormat = "dd/mm/yyyy"
Range("A1").Select
' Copy Net values across
ActiveWorkbook.Sheets("Entry").Activate
Range("P12:P" & Last_Row + 11).Select
Selection.Copy
Sheets("Output").Select
Range("G2").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
' Copy Account codes across
ActiveWorkbook.Sheets("Entry").Activate
Range("Q12:Q" & Last_Row + 11).Select
Selection.Copy
Sheets("Output").Select
Range("h2").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
' Delete rows which say Payment or with cost code = 0
ActiveWorkbook.Sheets("Output").Activate
Range("E2").Select
C_Row = 2
Do While Range("C" & C_Row).Value > 0
Range("E" & C_Row).Select
If StrComp("Payment", Left(Trim(ActiveCell.Value), 7), vbTextCompare) = 0 Then
Rows(ActiveCell.Row).EntireRow.Delete
ElseIf Range("H" & C_Row).Value = 0 Then
Rows(ActiveCell.Row).EntireRow.Delete
Else
C_Row = C_Row + 1
End If
Loop
' Delete blank cells
Call Del_Blank
' Save worksheet
Application.DisplayAlerts = False
Worksheets("master").Select
ThisWorkbook.Save
Application.ScreenUpdating = True
' Save CSV Output File
ThisWorkbook.Sheets("Output").Select
Range("A1").Select
Set Col_Range = Range("C2:C500")
Last_Row = Application.WorksheetFunction.CountIf(Col_Range, ">0")
Range("A1:J" & Last_Row + 1).Select
ThisWorkbook.Sheets("Output").SaveAs Filename:=Local_Dir & Range("File_Nam").Value, FileFormat:=xlCSV
Worksheets("master").Activate
Range("A1").Select
' Close This Workbook
Application.DisplayAlerts = True
MsgBox "All Complete" & vbNewLine & "Exit workbook"
ActiveWindow.Close savechanges:=False
' The End
End Sub
Sub Del_Blank()
'Deletes single cells that are blank located inside a designated range
Dim B_Rng As Range
Dim C_Range As Range
Dim Lst_Row As Integer
Worksheets("Output").Select
'Delete blank cells and shift upward
Set C_Range = Range("C2:C500")
Lst_Row = Application.WorksheetFunction.CountIf(C_Range, ">0")
Range("A" & Lst_Row + 2 & ":J500").EntireRow.Delete
On Error Resume Next
Set B_Rng = Range("K1:Z100").SpecialCells(xlCellTypeBlanks)
B_Rng.Columns.Delete Shift:=xlToLeft
On Error GoTo 0
End Sub
Function ImportFile(myRange As String, myFilePath As String) As Long
' Import CSV file without generating a Query Table
Dim myValue1 As String, myValue2 As String, myValue3 As String
Dim myValue4 As String, myValue5 As String, myValue6 As String
Dim myValue7 As String
Dim myRecordCount As Long: myRecordCount = 0
Open myFilePath For Input As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] ' Open file for input.
Range(myRange).Select
Do While Not EOF(1) ' Loop until end of file.
Input [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , myValue1, myValue2, myValue3, myValue4, myValue5, myValue6, myValue7
'Variable for each data field.
Selection.Offset(myRecordCount, 0).Value = myValue1
Selection.Offset(myRecordCount, 1).Value = myValue2
Selection.Offset(myRecordCount, 2).Value = myValue3
Selection.Offset(myRecordCount, 3).Value = myValue4
Selection.Offset(myRecordCount, 4).Value = myValue5
Selection.Offset(myRecordCount, 5).Value = myValue6
Selection.Offset(myRecordCount, 6).Value = myValue7
myRecordCount = myRecordCount + 1
Loop
ImportFile = myRecordCount
' Close file
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]
End Function