please help me to correct this code: Sub OutstandingPO() 'Application.EnableEvents =…
- User
please help me to correct this code: Sub OutstandingPO()
'Application.EnableEvents = False
Dim i As Long, lastRow As Long Dim myRange As Range, myCell As Range Dim filePath As String, myFile As Integer
lastRow = Cells(Rows.count, "G").End(xlUp).Row Set myRange = Range("G1:G" & lastRow)
Shell "notepad.exe", vbNormalFocus
For Each myCell In myRange If myCell.Value = "" Then For i = 1 To 6 'Assuming data range is from A to F If Cells(myCell.Row, i).Value <> "" Then Copy.Cells(myCell.Row, i).text End If Next i
SendKeys "^V" SendKeys vbCrLf End If Next myCell
'Application.EnableEvents = True
End Sub
- Assistant
Sub OutstandingPO()
Application.EnableEvents = False
Dim i As Long, lastRow As Long Dim myRange As Range, myCell As Range Dim filePath As String, myFile As Integer
lastRow = Cells(Rows.Count, "G").End(xlUp).Row Set myRange = Range("G1:G" & lastRow)
Shell "notepad.exe", vbNormalFocus
For Each myCell In myRange If myCell.Value = "" Then For i = 1 To 6 'Assuming data range is from A to F If Cells(myCell.Row, i).Value <> "" Then Copy.Cells(myCell.Row, i).text End If Next i SendKeys "^V" SendKeys vbCrLf End If Next myCell
Application.EnableEvents = True
End Sub
- User
I have error Expected Variable or Function on this line: Copy.Cells(myCell.Row, i).text
- Assistant
The error is because
Copy.Cells(myCell.Row, i)is not a valid syntax. You may be trying to copy the values from the cell to the clipboard. To do this, you can use the following code:Sub OutstandingPO() Application.EnableEvents = False Dim i As Long, lastRow As Long Dim myRange As Range, myCell As Range Dim filePath As String, myFile As Integer lastRow = Cells(Rows.Count, "G").End(xlUp).Row Set myRange = Range("G1:G" & lastRow) Shell "notepad.exe", vbNormalFocus For Each myCell In myRange If myCell.Value = "" Then For i = 1 To 6 'Assuming data range is from A to F If Cells(myCell.Row, i).Value <> "" Then 'Copy the contents of the cell to clipboard Cells(myCell.Row, i).Copy End If Next i 'Paste the clipboard contents to notepad SendKeys "^V" SendKeys vbCrLf End If Next myCell Application.EnableEvents = True End SubIn this code, the
Copymethod has been removed and replaced withCells(myCell.Row, i).Copywhich will copy the contents of the cell to clipboard. The special character is used to paste the clipboard contents to notepad. - User
When I run the code below, I get the results I want, but every line and result starts and ends with a quote mark " ". How do I remove the quote marks : Sub OutstandingPO()
'Application.EnableEvents = False
Dim i As Long, lastRow As Long Dim myRange As Range, myCell As Range Dim filePath As String, myFile As Integer
'Define the range to search for empty cells in column G lastRow = Cells(Rows.count, "G").End(xlUp).Row Set myRange = Range("G1:G" & lastRow)
'Ask user for the file path and name. Default is "results.txt" in the same folder as the Excel file filePath = Application.ThisWorkbook.path & "\results.txt"
'Open the text file for writing (overwriting previous content) myFile = FreeFile() Open filePath For Output As myFile
'Loop through each cell in the range and copy the corresponding non-blank rows to the text file For Each myCell In myRange If myCell.Value = "" Then 'Empty cell found, copy corresponding non-blank rows to text file For i = 1 To 6 'Assuming data range is from A to F If Cells(myCell.Row, i).Value <> "" Then Write #myFile, Cells(myCell.Row, i).text End If Next i 'Add a new line to separate results for each row ''''''Write #myFile, "" Write #myFile, vbCrLf End If Next myCell
'Close the text file 'Close myFile
'Show a message box with the results file path MsgBox "Results saved to file: " & filePath, vbInformation
- Assistant
To remove the quote marks, you can modify the
Write #myFilestatement in the following way:'Loop through each cell in the range and copy the corresponding non-blank rows to the text file For Each myCell In myRange If myCell.Value = "" Then 'Empty cell found, copy corresponding non-blank rows to text file For i = 1 To 6 'Assuming data range is from A to F If Cells(myCell.Row, i).Value <> "" Then 'Write the contents of the cell to the file without quotes Write #myFile, Trim(Cells(myCell.Row, i).Value) End If Next i 'Add a new line to separate results for each row Write #myFile, vbCrLf End If Next myCellIn this code, the
Trimfunction is used to remove any leading or trailing spaces from the contents of the cell. TheValueproperty of the cell is used instead ofTextproperty to get the cell's contents without the quotes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.