I am getting an error on the line: lastRow = .Range("B3:B23").Cells(.Rows.Count,…
- User
I am getting an error on the line: lastRow = .Range("B3:B23").Cells(.Rows.Count, "B").End(xlUp).Row
Sub ABCToNotepad() ' SHOWS DATA PRESENT UNTIL LAST DATE IN RANGE Application.EnableEvents = False Application.ScreenUpdating = False
Dim rng As Range Dim lastRow As Long ' Find the last non-empty row in range B3:B23 With ThisWorkbook.Sheets("HolCard") If .Range("B3:B23").Cells.Count = 0 Then Exit Sub ' All rows are empty Else lastRow = .Range("B3:B23").Cells(.Rows.Count, "B").End(xlUp).Row End If End With ' Set the range to capture data (A1:B&lastRow) Set rng = ThisWorkbook.Sheets("HolCard").Range("A1:B" & lastRow) ' Define a constant for the text file name Const TEXT_FILE_NAME As String = "leave.txt" ' Replace with your desired name ' Create the full path to the text file (assuming it's in the same folder as your workbook) Dim tempFileName As String tempFileName = ThisWorkbook.Path & "\" & TEXT_FILE_NAME ' Open the text file for writing Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") Dim outFile As Object Set outFile = fso.CreateTextFile(tempFileName, True) ' Overwrites existing content ' Write data to the temporary file Dim cell As Range For Each Row In rng.Rows For Each cell In Row.Cells outFile.WriteLine cell.Value & vbTab Next cell outFile.WriteLine vbCrLf ' Add a newline after each row Next Row outFile.Close ' Close the temporary file ' Open the text file in Notepad Shell "notepad.exe " & tempFileName, vbNormalFocus Application.EnableEvents = True Application.ScreenUpdating = TrueEnd Sub
- Assistant