Option Explicit
Dim LedgerCount As Long
Sub SaveAsBankXML()
Dim LastColumnNumberInRow As Long
Dim LastFillDownRow As Long
Dim LedgerCount As Long
Dim StartRow As Long
Dim x As Long
Dim LastColumnLetterSheetExtract As String
Dim LastColumnLetterSheetImportBank As String
Dim strData As String
Dim strTempFile As String
'
StartRow = 2 ' <--- Set this to the starting row of data in
' ' sheets 'BankData' & 'Extract'
' if data is not entered
If Sheets("BankData").Range("A3") = vbNullString Then
MsgBox "Data not entered in cell A3."
Exit Sub
End If
'
If Sheets("BankData").Range("A4") = vbNullString Then LedgerCount = 1
'
LastFillDownRow = Sheets("BankData").Range("A" & Rows.Count).End(xlUp).Row - 1 ' Determine Last Row to fill down to in other sheets
'
'code ClearOldWorkings of Import bank from A3:BD Rows.Count).End(xlUp)
With Sheets("ImportBank")
LastColumnLetterSheetImportBank = Split(Cells(1, (.Cells.Find("*", _
, xlFormulas, , xlByColumns, xlPrevious).Column)).Address, "$")(1) ' Get last column letter used in Sheets("ImportBank")
'
If .Range("A" & .Rows.Count).End(xlUp).Row > StartRow Then ' If there is more than 1 row to delete then ...
.Range("A3:" & LastColumnLetterSheetImportBank & _
.Range("A" & .Rows.Count).End(xlUp).Row).ClearContents ' Erase A3:BDx range of data
End If
'
If LedgerCount <> 1 Then ' If more than one record is found in 'BankData' then ...
.Range("A" & StartRow & ":" & LastColumnLetterSheetImportBank & StartRow).AutoFill _
Destination:=.Range("A" & StartRow & ":" & _
LastColumnLetterSheetImportBank & LastFillDownRow) ' Fill the formulas down the 'ImportBank' range
End If
'
.UsedRange.EntireColumn.AutoFit ' Set all used columns on sheet wide enough for data
End With
'code ClearOldWorkings of Extract from A3:C Rows.Count).End(xlUp)
With Sheets("Extract")
LastColumnLetterSheetExtract = Split(Cells(1, (.Cells.Find("*", _
, xlFormulas, , xlByColumns, xlPrevious).Column)).Address, "$")(1) ' Get last column letter used in Sheets("Extract")
'
If .Range("A" & .Rows.Count).End(xlUp).Row > StartRow Then ' If there is more than 1 row to delete then ...
.Range("A3:" & LastColumnLetterSheetExtract & _
.Range("A" & .Rows.Count).End(xlUp).Row).ClearContents ' Erase A3:Cx range of data
End If
'
If LedgerCount <> 1 Then ' If more than one record is found in 'BankData' then ...
.Range("A" & StartRow & ":" & LastColumnLetterSheetExtract & StartRow).AutoFill _
Destination:=.Range("A" & StartRow & ":" & _
LastColumnLetterSheetExtract & LastFillDownRow) ' Fill the formulas down the 'Extract' range
End If
'
.UsedRange.EntireColumn.AutoFit ' Set all used columns on sheet wide enough for data
End With
'
x = Sheets("BankData").Range("A3:B" & Sheets("BankData").Range("A" & Rows.Count).End(xlUp).Row).Rows.Count ' Get count of rows to write to file
'
LastColumnNumberInRow = Sheets("ImportBank").Cells(2, Sheets("ImportBank").Columns.Count).End(xlToLeft).Column ' Get last column number in row
'
Sheets("ImportBank").Range("A2").Resize(x, LastColumnNumberInRow).Copy
'
strData = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("Text") ' Save contents into strData
'
strTempFile = "C:\Users\" & Environ("username") & "\Desktop\Bank.xml"
CreateObject("Scripting.FileSystemObject").CreateTextFile(strTempFile, True).Write strData ' Write the data to file
'
Application.CutCopyMode = False
MsgBox ("File saved on Desktop as Bank.XML Rename the File. Copy path and paste in tally.")
Sheets("BankData").Activate
Range("A2").Select
End Sub