Resize of cells in different codes displays an error. Required to edit to count <=0 or >0

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello guys

First of all, I am very grateful, to JohnnyL for sharing the codes with comments. With the help of formulas and codes shared by many of the members in the forum and with the help of JohnnyL’s comments, I have tried to edit and create my first code. Thank you to each of them.

I tried to edit JohnnyL’s code to improvise and edited major changes and some additional changes in it. Except for some manual work, I have the code good and running but I am stuck at some places for which I need your expertise to complete it. I have commented some of the issues, in all the codes which need to be replaced by editing some lines. Out of the 7 macro buttons, 2 are dummy as mentioned next to the macro. Those steps have to be done manually, To get the final result, the macro buttons have to be pressed in the same order.

Now, for the problems I could not solve and need your expertise to solve and complete this project.

  • I have to manually select the cells from AC2 to AU2 and double click every time. I need help to write the code in such a way that it can be included in the “Move PasteData to CopyData” code.
  • When I press the button “Get NA Ledgers to MasterData”, and if there are no NA ledgers in the MasterData sheet, it generates an error. Hence I need to add an extra line If B2 =<1 Then exit sub… else play the rest of the code… something like that. Same solution goes for all the other button codes.
  • In each code where changes are required, which I was unable to write the code, I have commented at each line where the code requires editing.
  • The split address code is in the MasterData sheet which displays the correct result when played in that sheet only. Also, it doesn’t run in option explicit. I need your expertise to correct the code and include it one of the above codes wherever it is required.
  • Most of the Application lines of code have changed or deleted while editing each macro - Application.ScreenUpdading, True / False, Application.CutCopyMode = False / True, etc., I have no idea why and where to place them.
  • When the project is over, there will be only 2 sheets on display – List of Ledgers and PasteData. Rest of the sheets need to be hidden with a code. (This is possible and I can do it once the above problems are solved.
  • Finally, for the most difficult and biggest problem. Enter January and February in List of ledgers sheet Cells A1 and A2 respectively and run the buttons one by one.
If possible, then, try to combine all the 7 codes in 2 or max 3 buttons. I would really appreciate that. Like ClearData, Generate Master XML and Generate Purchases XML. Just wondering if it is possible, to put it all in one button and get 2 xml’s generated and saved on the desktop.

Please note: I have a copy of all the sheets in the workbook and hidden it. By any chance, while testing or editing the code, it gets deleted or the formulas and data are deleted, it will not be a problem to copy the data again - by clicking the select sheet button, copy and paste.

Important Note: Please do not run JohnnyL’s code. It is just for reference only as the presentation,working and range of cells in the code of the sheets have changed.
Edit & Combine codes.xlsm
 
JohnnyL. I went through your code and when I was sure, I have made a few changes, not much. The biggest problem is the Split Address code. The result is perfect. The problem is it is situated in the MasterData sheet. I want to include that code in the Get NA ledgers sheet and delete that button. If you have understood what the code does, you can replace it with your own code with the required variables with comments. That will be just awesome. I will share the updated sheet once you are online. Till then trying to find if I have missed anything else.
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I Just noticed that you have already activated the Split Address button in the List of Ledgers Sheet. ?
 
Upvote 0
Ok. I think I have addressed all of the posts you made since my last post.

Link to file

Try that and let us know where you are at.
 
Upvote 0
Thank you @Fluff. I had though about that due to the rules, but decided against it because it pertains to several subroutines.

I am catching your drift though. So here goes ...

VBA Code:
Sub ClearOldWorkings()
'
    Dim LastRowInSheetImportMasters         As Long, LastRowInSheetImportPurchase           As Long, LastRowInSheetPurchaseData             As Long
    Dim LastColumnLetterSheetImportMasters  As String, LastColumnLetterSheetImportPurchase  As String, LastColumnLetterSheetPurchaseData    As String
'
    Application.ScreenUpdating = False                                                              ' Turn ScreenUpdating off
'
    Sheets("CopyData").Range("A2:AB2", Sheets("CopyData").Range("A2:AB2").End(xlDown)).ClearContents
    Sheets("CopyData").Range("AC3:AU3", Sheets("CopyData").Range("AC3:AU3").End(xlDown)).ClearContents
'
'
''    Sheets("MasterData").Range("B2:B2", Sheets("MasterData").Range("B2:B2").End(xlDown)).ClearContents
    Sheets("MasterData").Range("B2:E2", Sheets("MasterData").Range("B2:E2").End(xlDown)).ClearContents
    Sheets("MasterData").Range("F2:I10", Sheets("MasterData").Range("F2:I10").End(xlDown)).ClearContents
'
'
    LastColumnLetterSheetPurchaseData = Split(Cells(1, (Sheets("PurchaseData").Cells.Find("*", _
            , xlFormulas, , xlByColumns, xlPrevious).Column)).Address, "$")(1)                      ' Get last column letter used in Sheets("PurchaseData")
    LastRowInSheetPurchaseData = Sheets("PurchaseData").Cells.Find("*", , xlFormulas, _
            , xlByRows, xlPrevious).Row                                                             ' Find last row # used in Sheets("PurchaseData")
    Sheets("PurchaseData").Range("A3:" & LastColumnLetterSheetPurchaseData & _
            LastRowInSheetPurchaseData + 1).ClearContents                                           ' Clear contents of cells in Sheets("PurchaseData")
'
'
    LastColumnLetterSheetImportMasters = Split(Cells(1, (Sheets("ImportMasters").Cells.Find("*", _
            , xlFormulas, , xlByColumns, xlPrevious).Column)).Address, "$")(1)                      ' Get last column letter used in Sheets("ImportMasters")
    LastRowInSheetImportMasters = Sheets("ImportMasters").Cells.Find("*", , xlFormulas, _
            , xlByRows, xlPrevious).Row                                                             ' Find last row # used in Sheets("ImportMasters")
    Sheets("ImportMasters").Range("A3:" & LastColumnLetterSheetImportMasters & _
            LastRowInSheetImportMasters + 1).ClearContents                                          ' Clear old data from Sheets("ImportMasters")
'
'
    LastColumnLetterSheetImportPurchase = Split(Cells(1, (Sheets("ImportPurchase").Cells.Find("*", _
            , xlFormulas, , xlByColumns, xlPrevious).Column)).Address, "$")(1)                      ' Get last column letter used in Sheets("ImportPurchase")
    LastRowInSheetImportPurchase = Sheets("ImportPurchase").Cells.Find("*", , xlFormulas, _
            , xlByRows, xlPrevious).Row                                                             ' Find last row # used in Sheets("ImportPurchase")
    Sheets("ImportPurchase").Range("A3:" & LastColumnLetterSheetImportPurchase & _
            LastRowInSheetImportPurchase + 1).ClearContents                                         ' Clear old data from Sheets("ImportPurchase")
'
    Application.Goto Sheets("List of Ledgers").Range("A1")
'
    Application.ScreenUpdating = True                                                               ' Turn ScreenUpdating back on
'
    MsgBox "Old Data Cleared."
End Sub


VBA Code:
Sub Move_PasteData_to_CopyData()
'
    Dim c, R, a, l&
'
    With Sheets("PasteData")
        l = .Cells(Rows.Count, 1).End(xlUp).Row
        c = .Evaluate("iferror(MATCH(CopyData!A1:Z1,A1:zz1,),99)")
        R = .Evaluate("ROW(A2:A" & l & ")")
        a = Application.Index(.[a:zz], R, c)
'
        Sheets("CopyData").[A2:Z2].Resize(UBound(a)) = a    'if additional expense columns added then change range Z2
    End With
'
    Sheets("CopyData").Range("AC2:AU" & Sheets("CopyData").Cells(Rows.Count, 1).End(xlUp).Row).FillDown ' Copy the AC2:AU2 formulas down to last row of A
'
    Application.Goto Sheets("List of Ledgers").Range("A1")                                              ' Return to 'List of Ledgers' sheet cell A1
End Sub


VBA Code:
Sub Split_Address()
'
    Dim i, J, k, n, ar, nChar, xstr
    Dim t()     As String
    Dim arr()
    Dim ws1     As Worksheet
'
    Set ws1 = Worksheets("MasterData")
'
    With ws1
        ar = .[A1].CurrentRegion    'row number..?
    End With
'
    ReDim Preserve arr(1 To UBound(ar, 1), 1 To 6)
'
    k = 1
    nChar = 30              'Restricts the number of characters in a cell up to total 120 characters, can edit if required in future
'
    For i = 2 To UBound(ar, 1)
        If ar(i, 5) = "" Then GoTo nexti  ' 5 is the full address in column E
        t = Split(ar(i, 5), ",")
        xstr = t(0)
        n = 1
        nChar = 20
'
        For J = 1 To UBound(t)
            t(J) = Trim(t(J))
'
            If t(J) <> "" Then
                If Len(xstr & t(J)) <= nChar Then
                    xstr = xstr & " " & t(J)
                Else
'                   ReDim Preserve arr(1 To 4, 1 To n)
                    arr(k, n) = Trim(xstr)
                    xstr = t(J)
                    n = n + 1
'
                    If n = 4 Then nChar = 100
                End If
            End If
        Next J
'
        If arr(k, n) = "" Then arr(k, n) = Trim(xstr)   'removes special characters and trims to fit 30 characters in each column
nexti:
        k = k + 1
    Next i
'
    ws1.[F2].Resize(UBound(arr, 1), 6) = arr        'destination first cell where data is split



''  If .Count =<1 Then                                                                          '   If dictionary count > 0 then ...
'       Exit sub..?????    Application.Transpose(.Keys)

'
    Application.Goto Sheets("List of Ledgers").Range("A1")                                              ' Return to 'List of Ledgers' sheet cell A1
End Sub


VBA Code:
Sub Get_NA_Ledgers()
'
    Dim Data, Ledger, Chk, i As Long
'
    With Sheets("CopyData")
        Data = .Range("N2:N" & .Cells(.Rows.Count, 14).End(xlUp).Row).Value
    End With
'
    With Sheets("List of Ledgers")
        Ledger = .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row).Value
    End With
'
    ReDim Temp(1 To UBound(Data))
'
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(Data)
            Chk = Application.Match(Data(i, 1), Application.Index(Ledger, , 1), 0)
            If IsError(Chk) And Not .Exists(Data(i, 1)) Then .Add Data(i, 1), ""
        Next i
'
        If .Count > 0 Then                                                                      '   If dictionary count > 0 then ...
            Sheets("MasterData").Range("B2").Resize(.Count) = Application.Transpose(.keys)
        Else
            MsgBox "All Ledgers Available."                                                     '       Display message to user
        End If
    End With
'
    Sheets("MasterData").Range("C:E").NumberFormat = "General"                                  ' Set columns to General format

' Convert existing formulas on 'MasterData' sheet to use LastRow detected instead of hard coded '30'
   
    Sheets("MasterData").Range("C2").Formula = "=IFERROR(IF(B2="""","""",VLOOKUP(B2,CopyData!$N$2" & _
            ":$O$" & Sheets("CopyData").Cells(Rows.Count, 1).End(xlUp).Row & ",2,0)),"""")"     ' Write updated formula to C2

    Sheets("MasterData").Range("D2").Formula = "=IFERROR(VLOOKUP(LEFT($C2,2)+0," & _
            "'States Code'!$A$1:$B$37,2,0),"""")"                                               ' Write formula to D2
    
    Sheets("MasterData").Range("E2").Formula = "=IFERROR(VLOOKUP(B2,CopyData!$N$2:$P$" & _
            Sheets("CopyData").Cells(Rows.Count, 1).End(xlUp).Row & ",3,0),"""")"               ' Write updated formula to E2
'
    Sheets("MasterData").Range("C2:E" & _
            Sheets("MasterData").Cells(Rows.Count, 2).End(xlUp).Row).FillDown                   ' Copy the C2:E2 formulas down to last row of B
'
    Application.Goto Sheets("List of Ledgers").Range("A1")                                      ' Return to 'List of Ledgers' sheet cell A1
End Sub


VBA Code:
Sub GenerateMasterXML()
'
    Dim LastColumnNumberInRow               As Long
    Dim x                                   As Long
    Dim LastColumnLetterSheetImportMasters  As String
    Dim strData                             As String
    Dim strTempFile                         As String
    
    Application.ScreenUpdating = False
    
    If Sheets("MasterData").Range("B2") = vbNullString Then                                                         ' If B2 in MasterData is blank then ...
        MsgBox "All Ledgers Available. Press Generate Purchase.XML"                                                 '   Display message to user
        Exit Sub                                                                                                    '   Exit the code
    End If
'
    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")
'
    If x > 1 Then Sheets("ImportMasters").Range("A2:" & LastColumnLetterSheetImportMasters & x + 1).FillDown    ' If LedgerCount > 1 Then 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
'

'   If .Count =1 Then                                                                          '   If dictionary count > 0 then ...
'       Exit sub..?????    Application.Transpose(.Keys)
'
    Application.Goto Sheets("List of Ledgers").Range("A1")                                      ' Return to 'List of Ledgers' sheet cell A1
'
    Application.ScreenUpdating = True                                                           ' Turn ScreenUpdating back on
'
    MsgBox ("File saved on Desktop as Master.XML.")
End Sub


VBA Code:
Sub GeneratePurchaseXML()
'
    Dim LastColumnNumberInRowImportPurchase As Long
    Dim LastColumnNumberInRowPurchaseData   As Long
    Dim R                                   As Long
    Dim x                                   As Long
    Dim LastColumnLetterSheetImportPurchase As String
    Dim LastColumnLetterSheetPurchaseData   As String
    Dim strData                             As String
    Dim strTempFile                         As String
'
    Application.ScreenUpdating = False


    If Sheets("PurchaseData").Range("A2") = "" Then
        MsgBox "Data Not Found"
        Exit Sub
    End If
'
    R = Sheets("CopyData").Range("A2:A" & Sheets("CopyData").Range("A" & Rows.Count).End(xlUp).Row).Rows.Count                            ' Get count of rows to write to file
    LastColumnNumberInRowPurchaseData = Sheets("PurchaseData").Cells(2, Sheets("PurchaseData").Columns.Count).End(xlToLeft).Column  ' Get last column number in row
    LastColumnLetterSheetPurchaseData = Split(Cells(1, (Sheets("PurchaseData").Cells.Find("*", , xlFormulas, _
            , xlByColumns, xlPrevious).Column)).Address, "$")(1)                                            ' Get last column letter used in Sheets("PurchaseData")
    Sheets("PurchaseData").Range("A2:" & LastColumnLetterSheetPurchaseData & R + 1).FillDown                            ' Create range needed to copy
'
    x = Sheets("PurchaseData").Range("B2:B" & Sheets("PurchaseData").Range("B" & Rows.Count).End(xlUp).Row).Rows.Count                            ' Get count of rows to write to file
    LastColumnNumberInRowImportPurchase = Sheets("ImportPurchase").Cells(2, Sheets("ImportPurchase").Columns.Count).End(xlToLeft).Column  ' Get last column number in row
    LastColumnLetterSheetImportPurchase = Split(Cells(1, (Sheets("ImportPurchase").Cells.Find("*", , xlFormulas, _
            , xlByColumns, xlPrevious).Column)).Address, "$")(1)                                            ' Get last column letter used in Sheets("ImportPurchase")
    
    If x > 1 Then Sheets("ImportPurchase").Range("A2:" & LastColumnLetterSheetImportPurchase & x + 1).FillDown  ' If LedgerCount > 1 Then Create range needed to copy
    Sheets("ImportPurchase").Range("A2").Resize(x, LastColumnNumberInRowImportPurchase).Copy
'
    strData = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("Text")                                       ' Save contents into strData
    strTempFile = "C:\Users\" & Environ("username") & "\Desktop\Purchase.xml"
    CreateObject("Scripting.FileSystemObject").CreateTextFile(strTempFile, True).Write strData                      ' Write the data to file
'
    Application.Goto Sheets("List of Ledgers").Range("A1")                                      ' Return to 'List of Ledgers' sheet cell A1


'   If .Count =1 Then                                                                          '   If dictionary count > 0 then ...
'       Exit sub..?????    Application.Transpose(.Keys)
'
    Application.ScreenUpdating = True                                                           ' Turn ScreenUpdating back on
'
    MsgBox ("File saved on Desktop as Purchase.XML. Copy path and paste in tally.")
End Sub
 
Upvote 0
If I replace this code, it displays the message, but it copies the headings from C1:D1 to C2:D2 which will affect the ImportMasters sheet.
Issue 1. When all the ledgers are available, as I said, the code is copying the headings from C1:D1 to C2:D2 which will in turn affect the ImportMasters sheet.
 
Upvote 0
I checked the code, if all ledger are available, the code resizes the headings with another set of headings.
Untitled.png
 
Upvote 0
I know, I know. It is not your code. So, it is not your mistake. But the issue needs to be resolved PLEASE.
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,228
Members
453,025
Latest member
Hannah_Pham93

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top