Option Explicit
Sub GenerateNALedgers()
Application.ScreenUpdating = False ' Turn ScreenUpdating off
Dim DictionaryRow As Long
Dim SourceHeaderColumnNumber As Long, SourceHeaderRow As Long, SourceLastRow As Long
Dim cell As Range
Dim SourceLastColumnLetter As String
Dim AvoidText As String
Dim DataDictionary As Variant
Dim List_of_LedgersFormulasColumnArray As Variant, OriginalParticularsDataArray As Variant
Dim SourceWS As Worksheet
Sheets("Workings").Select
Cells.Select
Selection.Clear
Range("A1").Select
Sheets("List of Ledgers").Select
Cells.Select
Range("A5").Activate
Sheets("Original").Select
Cells.Select
Selection.Copy
Sheets("Workings").Select
Cells.Select
ActiveSheet.Paste
'need to edit below line to find row with particulars which may be in any of the rows
Rows("10:10").Select
Application.CutCopyMode = False
Selection.UnMerge
Range("B10").Select
Selection.Cut Destination:=Range("C10")
Sheets("List of Ledgers").Select
Columns("E:E").Select
Selection.Clear
Range("E6").Select
Sheets("MasterData").Select
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("B1").Select
Sheets("List of Ledgers").Select
Range("E6").Select
Set SourceWS = Sheets("Workings") ' <--- Set this to the source sheet
SourceLastColumnLetter = Split(Cells(1, (SourceWS.Cells.Find("*", , xlFormulas, , _
xlByColumns, xlPrevious).Column)).Address, "$")(1) ' Get last column letter used in the source sheet
SourceLastRow = SourceWS.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row - 1 ' Get the last source row of data minus the total row
With SourceWS.Range("A1:" & SourceLastColumnLetter & SourceLastRow) ' Look through the source sheet for the header row
Set cell = .Find("Particulars", LookIn:=xlValues) ' Find the header called 'Particulars'
If Not cell Is Nothing Then ' If 'Particulars' is found then ...
SourceHeaderRow = cell.Row ' Save the row # into SourceHeaderRow
SourceHeaderColumnNumber = cell.Column ' Save the Column # into SourceHeaderColumn
End If
End With
OriginalParticularsDataArray = SourceWS.Range(SourceWS.Cells(SourceHeaderRow + 1, SourceHeaderColumnNumber), _
SourceWS.Cells(SourceLastRow, SourceHeaderColumnNumber)) ' Save Data to be pasted into 2D 1 Based OriginalParticularsDataArray
Sheets("List of Ledgers").Range("E6").Resize(UBound(OriginalParticularsDataArray, _
1)) = OriginalParticularsDataArray 'Display OriginalParticularsDataArray to Sheets("List of Ledgers")
List_of_LedgersFormulasColumnArray = Sheets("List of Ledgers").Range("F6:F" & SourceLastRow) ' Load formula column from Sheets("List of Ledgers") to array
DataDictionary = Sheets("List of Ledgers").Range("E6", Sheets("List of Ledgers").Cells(Rows.Count, "F").End(xlUp)) ' Create DataDictionary
AvoidText = "Opening Balance, (as per details), Closing Balance"
AvoidText = AvoidText & ", " & DataDictionary(UBound(DataDictionary) - 1, 1)
With CreateObject("Scripting.Dictionary")
For DictionaryRow = 1 To UBound(DataDictionary) ' Loop through each row of DataDictionary
If Not .Exists(DataDictionary(DictionaryRow, 1)) And IsError(List_of_LedgersFormulasColumnArray(DictionaryRow, 1)) Then ' If uniue value found & ...
' ' Error found in column to the right then ...
If InStr(1, AvoidText, DataDictionary(DictionaryRow, 1)) = 0 Then
.Add DataDictionary(DictionaryRow, 1), Array(DataDictionary(DictionaryRow, 2)) ' add unique value to DataDictionary
End If
End If
Next ' Loop back
Sheets("MasterData").Range("B2").Resize(.Count) = Application.Transpose(.keys) ' Display unique values on Sheets("MasterData")
Dim LastColumnNumberInRow As Long
Dim x As Long
Dim xmlFile As Object
Dim LastColumnLetterSheetImportMasters As String
Dim strData As String
Dim strTempFile As String
Sheets("ImportMasters").Range("A3:AAA10000").ClearContents
x = Sheets("MasterData").Range("B2:B" & Sheets("MasterData").Range("B" & Rows.Count).End(xlUp).Row).Rows.Count ' Get count of rows to write to file
LastColumnNumberInRow = Sheets("ImportMasters").Cells(2, Sheets("ImportMasters").Columns.Count).End(xlToLeft).Column ' Get last column number in row
LastColumnLetterSheetImportMasters = Split(Cells(1, (Sheets("ImportMasters").Cells.Find("*", , xlFormulas, _
, xlByColumns, xlPrevious).Column)).Address, "$")(1) ' Get last column letter used in Sheets("ImportMasters")
Sheets("ImportMasters").Range("A2:" & LastColumnLetterSheetImportMasters & x + 1).FillDown ' Create range needed to copy
Sheets("ImportMasters").Range("A2").Resize(x, LastColumnNumberInRow).Copy
strData = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("Text") ' Save contents into strData
strTempFile = "C:\Users\" & Environ("username") & "\Desktop\Master.xml"
CreateObject("Scripting.FileSystemObject").CreateTextFile(strTempFile, True).Write strData ' Write the data to file
MsgBox ("File saved on Desktop as Master.XML.")
End With
Application.ScreenUpdating = True ' Turn ScreenUpdating back on
End Sub