I have a user-defined function with a few parameters -- two of them take a different range of cells from a worksheet (each range has the same number of rows). The UDF will ultimately do some evaluation on the numbers within some columns of the ranges to find the MIN and MAX values.
But before that can happen, I need to split up one of the ranges into three separate range variables, so each of my ranges contains only one column. That much works. After that, I need to cut off the last row from each range. I'm pretty sure that part also works. Next, I need to loop through one of the ranges to look for a specific value -- "LD". If it finds it, I want that same row to be deleted from all four of the ranges.
I decided to see if CoPilot could help me with this. Someone told me it has become pretty good at writing code, so I thought I would give it a shot. Well... it gave me some code that does not work. But, I can't figure out what exactly is wrong. (I modified the code every so slightly to fit my needs. But the relevant part of trying to delete the rows from the Range variables is directly from it.)
When I ran the code, the two Debug.Print statements showed the same number of rows. So the rows were not being deleted. And I confirmed that one of the rows in LDCol had "LD" in it. So it should have deleted 1 row from each range. I stepped through the code and saw it execute the 4 .Delete lines, but they didn't seem to do anything. After, the row with "LD" was still in LDCol.
Here is that other function.
Does anyone have any idea why this code isn't working? This is my first attempt at deleting rows from range variables, so I'm a bit out of my league here.
But before that can happen, I need to split up one of the ranges into three separate range variables, so each of my ranges contains only one column. That much works. After that, I need to cut off the last row from each range. I'm pretty sure that part also works. Next, I need to loop through one of the ranges to look for a specific value -- "LD". If it finds it, I want that same row to be deleted from all four of the ranges.
I decided to see if CoPilot could help me with this. Someone told me it has become pretty good at writing code, so I thought I would give it a shot. Well... it gave me some code that does not work. But, I can't figure out what exactly is wrong. (I modified the code every so slightly to fit my needs. But the relevant part of trying to delete the rows from the Range variables is directly from it.)
VBA Code:
Dim Temp_MainPercentCol As Range
Dim Temp_Fringe1PercentCol As Range
Dim Temp_Fringe2PercentCol As Range
Dim MainPercentCol As Range
Dim Fringe1PercentCol As Range
Dim Fringe2PercentCol As Range
Dim LDCol As Range
'NumOfRows is a parameter of the UDF.
'PercentCols is also a parameter of the UDF.
This breaks it up into 3 ranges, so each range has only 1 column. This part works.
Set Temp_MainPercentCol = PercentCols.Columns(1).Resize(RowSize:=NumOfRows)
Set Temp_Fringe1PercentCol = PercentCols.Columns(2).Resize(RowSize:=NumOfRows)
Set Temp_Fringe2PercentCol = PercentCols.Columns(3).Resize(RowSize:=NumOfRows)
'This will remove the last row from each range because that row is not needed. It does this with a function that ChatGPT gave me, which I will paste below. I think this part is working, but am not 100% sure.
Set MainPercentCol = RemoveLastRowFromRange(Temp_MainPercentCol)
Set Fringe1PercentCol = RemoveLastRowFromRange(Temp_Fringe1PercentCol)
Set Fringe2PercentCol = RemoveLastRowFromRange(Temp_Fringe2PercentCol)
Set LDCol = RemoveLastRowFromRange(TheLDCol) 'TheLDCol is another parameter of the UDF.
'This will loop through LDCol. If it finds a row equal to "LD", it is supposed to delete that row from all four of the ranges.
lastRow = LDCol.Rows.Count
Debug.Print "Number of rows in MainPercentCol before delete: " & MainPercentCol.Rows.Count
For i = lastRow To 1 Step -1
If LDCol.Cells(i, 1).Value = "LD" Then
LDCol.Cells(i, 1).EntireRow.Delete
MainPercentCol.Cells(i, 1).EntireRow.Delete
Fringe1PercentCol.Cells(i, 1).EntireRow.Delete
Fringe2PercentCol.Cells(i, 1).EntireRow.Delete
End If
Next i
Debug.Print "Number of rows in MainPercentCol after delete: " & MainPercentCol.Rows.Count
When I ran the code, the two Debug.Print statements showed the same number of rows. So the rows were not being deleted. And I confirmed that one of the rows in LDCol had "LD" in it. So it should have deleted 1 row from each range. I stepped through the code and saw it execute the 4 .Delete lines, but they didn't seem to do anything. After, the row with "LD" was still in LDCol.
Here is that other function.
VBA Code:
Function RemoveLastRowFromRange(rng As Range) As Range
' Check if the range has more than one row
If rng.Rows.Count > 1 Then
' Resize the range to exclude the last row
Set RemoveLastRowFromRange = rng.Resize(rng.Rows.Count - 1)
Else
' If the range has only one row, make no change.
Set RemoveLastRowFromRange = rng
End If
End Function
Does anyone have any idea why this code isn't working? This is my first attempt at deleting rows from range variables, so I'm a bit out of my league here.