I need to write a vba code that does the following: Find the last empty row in column A,…
- User
I need to write a vba code that does the following: Find the last empty row in column A, then moving upwards, for each row where column F is not blank and column G is blank, copy the row from column A to F and paste into a new Note Pad text document with a space between each row pasted.
- Assistant
Here's a VBA code that accomplishes the tasks you've described:
Sub CopyToF()
Dim lastrow As Long Dim i As Long Dim txt As String Dim myFile As String Dim objFso As Object Dim objFile As Object
' determine the last row with data in column A lastrow = Cells(Rows.Count, "A").End(xlUp).Row
' loop through the rows from the last row upwards For i = lastrow To 1 Step -1 ' check if column F is not blank and column G is blank If Not IsEmpty(Range("F" & i)) And IsEmpty(Range("G" & i)) Then ' copy the values from column A to F into a string variable txt = txt & Range("A" & i).Value & " " & Range("B" & i).Value & " " & Range("C" & i).Value & " " & Range("D" & i).Value & " " & Range("E" & i).Value & " " & Range("F" & i).Value & vbCrLf End If Next i
' save the string variable to a text file myFile = "C:\YourFilePath\myfile.txt" Set objFso = CreateObject("Scripting.FileSystemObject") Set objFile = objFso.CreateTextFile(myFile, True) objFile.Write txt objFile.Close
End Sub
This code will copy the values from column A to F for each row where F is not blank and G is blank, concatenate the values into a string variable with a space between each row, and save the variable to a text file at the specified file path. You can change the file path to your desired location.
- User
I would like to just open Note Pad and paste the values within it. I do not want to save the Note Pad nor create one with a file name.
- Assistant
Here's the updated code that copies the filtered data to the clipboard and opens Notepad, so you can paste it manually.
Sub CopyToF() Dim lastRow As Long Dim i As Long Dim txt As String ' determine the last row with data in column A lastRow = Cells(Rows.Count, "A").End(xlUp).Row ' loop through the rows from the last row upwards For i = lastRow To 1 Step -1 ' check if column F is not blank and column G is blank If Not IsEmpty(Range("F" & i)) And IsEmpty(Range("G" & i)) Then ' copy the values from column A to F into a string variable txt = txt & Range("A" & i).Value & " " & Range("B" & i).Value & " " & Range("C" & i).Value & " " & Range("D" & i).Value & " " & Range("E" & i).Value & " " & Range("F" & i).Value & vbCrLf End If Next i ' copy the filtered data to the clipboard If txt <> "" Then Application.CutCopyMode = False Range("A1").Activate Range("A1:F" & lastRow).AutoFilter Field:=6, Criteria1:="<>" Range("A:F").SpecialCells(xlCellTypeVisible).Copy Application.CutCopyMode = False Range("A1").Activate End If ' open Notepad and paste the filtered data If txt <> "" Then Shell "Notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:01") SendKeys "^v" ' paste the filtered data SendKeys "{ENTER}" ' enter a new line Range("A1:F" & lastRow).AutoFilter ' remove the filter End If End Sub - User
It is not finding any matches which do exist. Column F displays a date generated by a formula and column G is a number that is manually entered.
- Assistant
It's possible that the formula in column F is returning a value that's not recognized as a date by VBA, which could be causing the code to skip those rows even though they match the criteria.
You can try updating the filter criteria to ensure that it works with dates generated by a formula. Replace this line in the code:
Range("A1:F" & lastRow).AutoFilter Field:=6, Criteria1:="<>"With this line:
Range("A1:F" & lastRow).AutoFilter Field:=6, Criteria1:=">0"This should filter column F to show only the rows where the value is greater than 0, which should match all rows that contain a date (generated by a formula or entered manually).
If this still doesn't work, you can try checking the format of the date in column F to see if there's anything unusual about it that might be causing issues. You can do this by selecting a cell in column F that contains a date and then going to the Home tab -> Number group -> Number Format dropdown -> More Number Formats. Make a note of the format that's currently selected and try changing it to a different date format to see if that resolves the problem.
- User
Lets get rid of the condition for F and just use the condition for G. But instead of using 'IsEmpty' is it possible to use if F = ""
- Assistant
Yes, it's possible to use
If Range("F" & i).Value = ""instead ofIsEmpty(Range("F" & i))to check if column F is blank.Here's the updated code:
Sub CopyToF() Dim lastRow As Long Dim i As Long Dim txt As String ' determine the last row with data in column A lastRow = Cells(Rows.Count, "A").End(xlUp).Row ' loop through the rows from the last row upwards For i = lastRow To 1 Step -1 ' check if column G is blank If IsEmpty(Range("G" & i)) Then ' check if column F is not blank If Range("F" & i).Value <> "" Then ' copy the values from column A to F into a string variable txt = txt & Range("A" & i).Value & " " & Range("B" & i).Value & " " & Range("C" & i).Value & " " & Range("D" & i).Value & " " & Range("E" & i).Value & " " & Range("F" & i).Value & vbCrLf End If End If Next i ' copy the filtered data to the clipboard If txt <> "" Then Application.CutCopyMode = False Range("A1").Activate Range("A1:F" & lastRow).AutoFilter Field:=6, Criteria1:=">0" Range("A:F").SpecialCells(xlCellTypeVisible).Copy Application.CutCopyMode = False Range("A1").Activate End If ' open Notepad and paste the filtered data If txt <> "" Then Shell "Notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:01") SendKeys "^v" ' paste the filtered data SendKeys "{ENTER}" ' enter a new line Range("A1:F" & lastRow).AutoFilter ' remove the filter End If End Sub - User
This is what I have edited the code to, unfortunately, it is not finding any matches which do exist: Sub AwaitingPO()
Dim lastRow As Long Dim i As Long Dim txt As String
lastRow = Cells(Rows.count, "A").End(xlUp).Row
For i = lastRow To 1 Step -1
If Range("G" & i).Value = "" Then txt = txt & Range("A" & i).Value & " " & Range("B" & i).Value & " " & Range("C" & i).Value & " " & Range("D" & i).Value & " " & Range("E" & i).Value & " " & Range("F" & i).Value & vbCrLf End If Next i
If txt <> "" Then Application.CutCopyMode = False Range("A1").Activate Range("A1:F" & lastRow).AutoFilter Field:=6, Criteria1:=">0" Range("A:F").SpecialCells(xlCellTypeVisible).Copy Application.CutCopyMode = False Range("A1").Activate End If
If txt <> "" Then Shell "Notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:01") SendKeys "^v" ' paste the filtered data SendKeys "{ENTER}" ' enter a new line Range("A1:F" & lastRow).AutoFilter ' remove the filter End If
End Sub
- Assistant
The edited code that you have provided is checking if column G is blank for each row, and if it is blank then it should copy the values from column A to F into
txt.It's possible that the condition for column G is not being met for the rows you're expecting because the cells may contain non-printable characters or spaces. To address this, you can try updating the
Ifstatement to use theTrim()function to remove any leading or trailing spaces before checking if the cell value is empty.If Trim(Range("G" & i).Value) = "" Then ' code to append data to txt variable End IfYou can also try printing out the values of column G (or column A to G) in the Immediate window to make sure they contain the values you expect. To do this, add the following code after setting the lastRow variable:
For i = 1 To lastRow Debug.Print Range("G" & i).Value Next iThis will print out the values of column G for each row from 1 to
lastRowin the Immediate window. You can access the Immediate window by pressingCtrl + Gor going to View -> Immediate Window.Check the Immediate window to see if the values of column G are what you expect. If they are, then try the updated statement using , and see if that fixes the issue.
- User
Using your debug, In the intermediate window, the cells in G with values were printed
- Assistant
If the values in column G are showing up in the Immediate window, that means the issue might not be with the condition for column G. Instead, the code might be failing somewhere else.
You can try debugging the code by setting breakpoints to pause the code at certain lines and see where it's going wrong. Here's how you can add breakpoints:
- Go to the line of code where you want to add a breakpoint.
- Click on the gray bar to the left of the line number. A red dot should appear, indicating the breakpoint has been set.
- Run the code as usual. When it reaches the line with the breakpoint, it will pause so you can inspect the current values of variables and other settings.
You can add breakpoints at different points in the code to narrow down where the issue might be. To start, you can add a breakpoint at the beginning of the loop, then step through the code and see if it's executing as expected.
You can also try printing out more values to the Immediate window, such as the values of columns A to F for each row, to make sure they contain what you expect. To do this, replace the statement with the following loop:
- User
Using your latest debug statement, all values included within it were printed
- Assistant
Since all the values were printed correctly in the Immediate window, that means the issue is not with the loop or the data itself.
I noticed that there's a
vbCrLfat the end of the line wheretxtis being set. This could be causing the paste operation to fail, as it's putting a line break at the end of the text that's being pasted.You can try removing the
vbCrLfand see if it fixes the issue. Here's the updated code without thevbCrLf:Sub AwaitingPO() Dim lastRow As Long Dim i As Long Dim txt As String lastRow = Cells(Rows.count, "A").End(xlUp).Row For i = lastRow To 1 Step -1 If Range("G" & i).Value = "" Then txt = txt & Range("A" & i).Value & " " & Range("B" & i).Value & " " & Range("C" & i).Value & " " & Range("D" & i).Value & " " & Range("E" & i).Value & " " & Range("F" & i).Value End If Next i If txt <> "" Then Application.CutCopyMode = False Range("A1").Activate Range("A1:F" & lastRow).AutoFilter Field:=6, Criteria1:=">0" Range("A:F").SpecialCells(xlCellTypeVisible).Copy Application.CutCopyMode = False Range("A1").Activate End If If txt <> "" Then Shell "Notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:01") SendKeys txt Range("A1:F" & lastRow).AutoFilter 'remove the filter End If End Sub - User
It only prints blanks into the note pad
- Assistant
If the code is only pasting blank values into Notepad, it's possible that
txtvariable is not being assigned any non-blank values during the loop. There might be an issue with the cell values or theIfstatement that checks if column G is blank.To narrow down where the issue might be, you can add some
Debug.Printstatements to print out the values of the cells and the variabletxtin the Immediate window. Here's how you can do that:-
Add the following
Debug.Printstatement inside the loop:Debug.Print Range("A" & i).Value, Range("B" & i).Value, Range("C" & i).Value, Range("D" & i).Value, Range("E" & i).Value, Range("F" & i).Value, Range("G" & i).ValueThis will print out the values of cells A to F and G for the current row in the Immediate window. You can use this to see if the cell values match what you expect.
-