Code not to resize if count is 1

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello guys
With the help of JohnnyL's code I am trying to add a few lines from it to another application. But I am not able to place or edit the code properly. If I add those lines to the code, the data gets deleted. Can someone tell me where am I going wrong.?
Johnnyl's code where it worked in another application which I am referring to is
Resize of cells in different codes displays an error. Required to edit to count <=0 or >0
This is the link to my workbook.
edit code not to resize when count is1.xlsm
 
Is single row working in your system.? It is not working the second time I run a single row.
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Not working, is not helpful. You have to say what should happen as opposed to what is happening, not doing anything, errors, etc.
 
Upvote 0
Just close the application, open it again with a single row of data and run the code. If it works then please
share your workbook.
 
Upvote 0
Yep, the code works on my end.

Link to file
Even your file is not working the second time when there is a single row. First time run single row it works. But second time same single row run, it is deleting the data of both the sheets.
 
Upvote 0
Ahh, Why didn't you just say run a single line of data twice in a row? Lemme test that.
 
Upvote 0
I think I tracked it down, gimme a couple of minutes.
 
Upvote 0
Here you go:

VBA Code:
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
 
Upvote 0
Solution
Phew!!! Finally. Will check with the original data and revert back.
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,948
Members
449,198
Latest member
MhammadishaqKhan

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