This code outputs to a text file the last recorded job in a sheet. It gets the info from…
- User
This code outputs to a text file the last recorded job in a sheet. It gets the info from row 2 to row 10. The code words perfectly. How can I add to the code so that it first puts the value of B1 of the sheet named in wst into the text followed by the values of the last row 2 to 10
Dim sheetName As String Dim rowValues As String Dim filePath As String Dim wst As Worksheet Dim lastRow As Long Dim i As Integer
A3A80 = False
sheetName = Me.Range("D" & Target.Row).Value On Error Resume Next Set wst = ThisWorkbook.Sheets(sheetName) If wst Is Nothing Then Exit Sub End If On Error GoTo 0 Set wst = ThisWorkbook.Sheets(sheetName) lastRow = wst.Cells(Rows.Count, "J").End(xlUp).Row For i = 2 To 10 rowValues = rowValues & wst.Cells(lastRow, i).Value & "," Next i rowValues = Left(rowValues, Len(rowValues) - 1) filePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\LastJob.txt" Open filePath For Output As #1 For i = 2 To 10 Print #1, wst.Cells(lastRow, i).Value Next i Close #1 Shell "notepad.exe " & filePath, vbNormalFocus End Sub
- Assistant
To add the value of B1 from the sheet named in wst to the text file, you can add the following line before the For loop:
Print #1, wst.Range("B1").Value
This will print the value of B1 to the text file before printing the values of row 2 to 10 on the next lines.
- User
I use this code to print to text, services that will be due within 30 days before Today and 7 days after today. Unfortunatel, it is also listing files outside of this period. Is there an error in the way the code is written
Sub ServiceIn30() Dim todayDate As Date Dim startDate As Date Dim endDate As Date Dim lastRow As Long Dim i As Long Dim j As Long todayDate = Date ' Set start and end date range startDate = DateAdd("d", -30, todayDate) endDate = DateAdd("d", 7, todayDate) ' Export rows to text file Dim rowValues As String Dim filePath As String Dim wst As Worksheet Set wst = ActiveSheet ' set ActiveSheet as worksheet to export filePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\ServiceDue.txt" lastRow = wst.Cells(wst.Rows.Count, "H").End(xlUp).Row Open filePath For Output As #1 For i = 1 To lastRow ' If date in range, copy row to text file If Format(Cells(i, "H").Value, "dd/mm/yyyy") >= Format(startDate, "dd/mm/yyyy") And Format(Cells(i, "H").Value, "dd/mm/yyyy") <= Format(endDate, "dd/mm/yyyy") Then rowValues = "" For j = 7 To 11 ' assuming a header in row 1 and columns G to K rowValues = rowValues & wst.Cells(i, j).Value & vbTab Next j Print #1, Replace(Replace(rowValues, vbCrLf, " "), vbTab, " ") & vbCrLf & vbCrLf End If Next i Close #1 ' Open text file Shell "notepad " & filePath, vbNormalFocus End Sub
- Assistant
The code seems to be correct, it's just possible that the issue lies with the data in the worksheet. Here are a couple of things you can check:
-
Make sure that the date in column H is in the format "dd/mm/yyyy". If the format is different, the code may not correctly identify the date in the specified range.
-
Double-check the start and end dates to make sure they are set correctly. You can add some debugging code to print out the values of startDate and endDate to make sure they reflect the correct date range.
If the issue persists, you can try stepping through the code using the VBA debugger to see where the problem lies.
-
- User
I have checked the data and found it to be accurate. I used the filter function built into excel and it outputs the correct results.
- Assistant
In that case, you can try modifying the code to use the DateValue function instead of the Format function to compare the dates. Here's an updated version of the code:
Sub ServiceIn30() Dim todayDate As Date Dim startDate As Date Dim endDate As Date Dim lastRow As Long Dim i As Long Dim j As Long todayDate = Date ' Set start and end date range startDate = DateAdd("d", -30, todayDate) endDate = DateAdd("d", 7, todayDate) ' Export rows to text file Dim rowValues As String Dim filePath As String Dim wst As Worksheet Set wst = ActiveSheet ' set ActiveSheet as worksheet to export filePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\ServiceDue.txt" lastRow = wst.Cells(wst.Rows.Count, "H").End(xlUp).Row Open filePath For Output As #1 For i = 1 To lastRow ' If date in range, copy row to text file If DateValue(wst.Cells(i, "H").Value) >= startDate And DateValue(wst.Cells(i, "H").Value) <= endDate Then rowValues = "" For j = 7 To 11 ' assuming a header in row 1 and columns G to K rowValues = rowValues & wst.Cells(i, j).Value & vbTab Next j Print #1, Replace(Replace(rowValues, vbCrLf, " "), vbTab, " ") & vbCrLf & vbCrLf End If Next i Close #1 ' Open text file Shell "notepad " & filePath, vbNormalFocus End Sub - User
it is still giving me out of the parameter
- Assistant
In that case, you can try adding some debug code to print out the values of the date variables and the date values in the worksheet to see where the issue lies. Here's an updated version of the code with the debug code:
Sub ServiceIn30() Dim todayDate As Date Dim startDate As Date Dim endDate As Date Dim lastRow As Long Dim i As Long Dim j As Long todayDate = Date ' Set start and end date range startDate = DateAdd("d", -30, todayDate) endDate = DateAdd("d", 7, todayDate) Debug.Print "Start Date: " & startDate Debug.Print "End Date: " & endDate ' Export rows to text file Dim rowValues As String Dim filePath As String Dim wst As Worksheet Set wst = ActiveSheet ' set ActiveSheet as worksheet to export filePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\ServiceDue.txt" lastRow = wst.Cells(wst.Rows.Count, "H").End(xlUp).Row Open filePath For Output As #1 For i = 1 To lastRow Debug.Print "Date Value: " & wst.Cells(i, "H").Value ' If date in range, copy row to text file If DateValue(wst.Cells(i, "H").Value) >= startDate And DateValue(wst.Cells(i, "H").Value) <= endDate Then rowValues = "" For j = 7 To 11 ' assuming a header in row 1 and columns G to K rowValues = rowValues & wst.Cells(i, j).Value & vbTab Next j Print #1, Replace(Replace(rowValues, vbCrLf, " "), vbTab, " ") & vbCrLf & vbCrLf End If Next i Close #1 ' Open text file Shell "notepad " & filePath, vbNormalFocus End Sub