The following code opens the text document but does not print the data row found into…
- User
The following code opens the text document but does not print the data row found into it. Sub ServiceIn90()
Dim todayDate As Date Dim startDate As Date Dim endDate As Date Dim lastRow As Long Dim i As Long
todayDate = Date ' Set start and end date range startDate = DateAdd("d", -90, 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
lastRow = wst.Cells(wst.Rows.Count, "H").End(xlUp).Row
Open "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Service in 90.txt" For Output As #1
For i = 1 To lastRow
' If date in range, copy row to text file If Cells(i, "H").Value >= startDate And 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, " ") End If
Next i
Close #1
' Open text file Shell "notepad " & filePath, vbNormalFocus
End Sub
- Assistant
To fix the issue, change the line:
Print #1, Replace(Replace(rowValues, vbCrLf, " "), vbTab, " ")
to:
Print #1, Replace(Replace(rowValues, vbCrLf, " "), vbTab, " ") & vbCrLf
This will add a new line after each row, so that each row is on a separate line in the text file.
- User
It appears the code is not capturing the requested data
- Assistant
One possible reason why the code is not capturing the requested data could be that the date format in the Excel sheet and the VBA code are not consistent.
In the code, the start and end dates are defined using the VBA DateAdd function, which returns the date in the default format for the computer it is running on. If the date format in the Excel sheet is different from the default format, the comparison between the dates may not work as expected.
To ensure that the date format is consistent, you can convert the date values to a string with a specific format using the VBA Format function.
For example, you can modify the line:
If Cells(i, "H").Value >= startDate And Cells(i, "H").Value <= endDate Then
to:
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
This will format the date values as string in "dd-mm-yyyy" format, which should be consistent with most date formats in Excel.
- User
Still not capturing any data
- Assistant
If the code is still not capturing any data, there might be other issues that need to be addressed. Here are some troubleshooting steps you can try:
-
Check the worksheet name: Make sure that the active sheet is the same sheet that you want to export. If it is a different sheet, modify the code to reference the correct worksheet name.
-
Check the cell range: Make sure that the cell range where the date values are stored is correct. It should match the column and row range where the dates are stored in the worksheet.
-
Check the date format: Ensure that the date format in the cell matches the format used in the VBA code. The code assumes that the date format is in "dd/mm/yyyy" format. If it is in a different format, modify the VBA code to reflect the correct date format.
-
Debug the code: Use breakpoints and the Debug.Print statement to check if the code is executing properly. Add breakpoints to the code and step through it to ensure that it is executing each line of code correctly. You can also add Debug.Print statements to output the values of variables and check if they match your expectations.
-
- User
In the code the statement For j = 7 To 11 shouldnt it be For i = 7 To 11
- Assistant
Yes, you are correct. The code should use
iinstead ofjin the inner loop becauseiis already being used for the outer loop. This will allow the row values to be properly concatenated in therowValuesvariable.So, the line:
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 jshould be changed to:
For i = 7 To 11 ' assuming a header in row 1 and columns G to K rowValues = rowValues & wst.Cells(i, j).Value & vbTab Next iWith this change, the code should properly concatenate the row values and export the rows to the text file.
- User
when I change the j to i and run the code I get a Compile error: For control variable already in use
- Assistant
This error occurs because both the outer loop and the inner loop are using the same variable
ias the control variable. As the error message suggests, you cannot use the same control variable for two nested loops.To fix this error, you can choose a different variable name for the inner loop control variable. For example, you could use
jinstead ofi:For i = 1 To lastRow ' If date in range, copy row to text file If Cells(i, "H").Value >= startDate And 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, " ") End If Next iWith this change, the inner loop will use the variable
jas the control variable instead ofi, which allows both loops to execute without error. - User
still does not capture any data
- Assistant
If the code is still not capturing any data, there might be other issues that need to be addressed. Here are some troubleshooting steps you can try:
-
Check the worksheet name: Make sure that the active sheet is the same sheet that you want to export. If it is a different sheet, modify the code to reference the correct worksheet name.
-
Check the cell range: Make sure that the cell range where the date values are stored is correct. It should match the column and row range where the dates are stored in the worksheet.
-
Check the date format: Ensure that the date format in the cell matches the format used in the VBA code. The code assumes that the date format is in "dd/mm/yyyy" format. If it is in a different format, modify the VBA code to reflect the correct date format.
-
- User
All checks done and everything is in order. Can you please check the entire code again for possible errors or modifications..... Sub ServiceIn90()
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", -90, 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
lastRow = wst.Cells(wst.Rows.Count, "H").End(xlUp).Row
Open "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Service in 90.txt" For Output As #1
For i = 1 To lastRow ' If date in range, copy row to text file If Cells(i, "H").Value >= startDate And 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, " ") End If Next i
Close #1
' Open text file Shell "notepad " & filePath, vbNormalFocus
End Sub
- Assistant
The code looks mostly correct, but there are a few things that might need to be checked:
-
The
filePathvariable is not being set to a value, so the code might not be able to open the output file using the function. You might want to set this variable to the correct file path and make sure that you have write permissions to that location.
-
- User
It worked. Can I have two spaces between each row
- Assistant
Yes, you can modify the
Printstatement to add two spaces between each row in the output text file. To do this, you can modify the line:Print #1, Replace(Replace(rowValues, vbCrLf, " "), vbTab, " ")to:
Print #1, Replace(Replace(rowValues, vbCrLf, " "), vbTab, " ") & vbCrLf & vbCrLfThis adds two spaces between each row by replacing the
vbCrLf(carriage return and line feed) with two spaces (" ") and then adding an additional to create a blank line between each row.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.