Dim copyRange As Range Dim i As Long Dim lastRow As Long Dim offsetCount As Long lastRow…
- User
Dim copyRange As Range Dim i As Long Dim lastRow As Long Dim offsetCount As Long
lastRow = wscf.Cells(wscf.Rows.Count, “I”).End(xlUp).Row offsetCount = 0
For i = 5 To lastRow If wscf.Cells(i, “I”).Value = “” Then If copyRange Is Nothing Then Set copyRange = wscf.Cells(i, “J”) Else Set copyRange = Union(copyRange, wscf.Cells(i, “J”)) End If If wscf.Cells(i, “H”).Value <> “” Then offsetCount = offsetCount + 1 copyRange.Cells(offsetCount).Offset(0, 1).Value = wscf.Cells(i, “J”).Value & " " & wscf.Cells(i, “H”).Value End If End If Next i
The Values in column "I" are listed as follows: I5: 20/10/23 I6: I7: I8: I9: I10:
The Values in column "J" are listed as follows: J5: Completed Tasks J6: Shower Head descale J7: Legionella Analysis J8: Hot & Cold Water Temperature J9: Water Tank Inspection J10: Sports Building Water Tank
The Corresponding row values in "H" are listed as follows: H5: H6: 05/09/2023 H7: 09/12/2023 H8: 15/09/2023 H9: 28/08/2024 H10:
When I run the code above, this is what I am expecting as the output: Shower Head descale 05/09/2023 Legionella Analysis 09/12/2023 Hot & Cold Water Temperature 15/09/2023 Water Tank Inspection 28/08/2024 Sports Building Water Tank
Unfortunately the values in H are not concatenate correctly as shown in the output below: Shower Head descale05/09/202305/09/202305/09/2023 Legionella Analysis Hot & Cold Water Temperature Checks Water Tank Inspection Sports Building Water Tank Sediment Clean
- Assistant
You can modify the code to concatenate the values correctly by adjusting the "offsetCount" variable and using the correct copyRange.Cells(offsetCount) when setting the value. Here's the updated code:
Dim copyRange As Range Dim i As Long Dim lastRow As Long Dim offsetCount As Long lastRow = wscf.Cells(wscf.Rows.Count, "I").End(xlUp).Row offsetCount = 0 For i = 5 To lastRow If wscf.Cells(i, "I").Value = "" Then If copyRange Is Nothing Then Set copyRange = wscf.Cells(i, "J") Else Set copyRange = Union(copyRange, wscf.Cells(i, "J")) End If If wscf.Cells(i, "H").Value <> "" Then offsetCount = offsetCount + 1 If wscf.Cells(i, "H").Value = "" Then copyRange.Cells(offsetCount).Value = wscf.Cells(i, "J").Value Else copyRange.Cells(offsetCount).Value = wscf.Cells(i, "J").Value & " " & wscf.Cells(i, "H").Value End If End If End If Next iThis should give you the expected output:
Shower Head descale 05/09/2023 Legionella Analysis 09/12/2023 Hot & Cold Water Temperature 15/09/2023 Water Tank Inspection 28/08/2024 Sports Building Water Tank - User
There is still one error, which is to do with the very first value of J. It is duplicating the H value on the first value of J several times. On the other values of J, the concatenated value is correct, as shown below:
Shower Head descale05/09/202305/09/202305/09/2023 05/09/2023 Legionella Analysis 09/12/2023 Hot & Cold Water Temperature Checks 15/09/2023 Water Tank Inspection 28/08/2024 Sports Building Water Tank Sediment Clean
- Assistant
Apologies for the oversight. To fix the issue with the first value of J being duplicated, you can add an additional condition to only concatenate the corresponding value from column H if the copyRange.Cells(offsetCount) is not equal to wscf.Cells(i, "J").
Here's the updated code:
Dim copyRange As Range Dim i As Long Dim lastRow As Long Dim offsetCount As Long lastRow = wscf.Cells(wscf.Rows.Count, "I").End(xlUp).Row offsetCount = 0 For i = 5 To lastRow If wscf.Cells(i, "I").Value = "" Then If copyRange Is Nothing Then Set copyRange = wscf.Cells(i, "J") Else Set copyRange = Union(copyRange, wscf.Cells(i, "J")) End If If wscf.Cells(i, "H").Value <> "" Then offsetCount = offsetCount + 1 copyRange.Cells(offsetCount).Value = wscf.Cells(i, "J").Value If copyRange.Cells(offsetCount).Value <> wscf.Cells(i, "J").Value Then copyRange.Cells(offsetCount).Value = copyRange.Cells(offsetCount).Value & " " & wscf.Cells(i, "H").Value End If End If End If Next i - User
It is still doing the same thing. The first Value of that is found has a conccateneated value that is correct but it is repaeated four times. The other values have a concatenated value that is correct and not repaeated. I have include the problem output: Shower Head descale05/09/202305/09/202305/09/2023 05/09/2023 Legionella Analysis 09/12/2023 Hot & Cold Water Temperature 15/09/2023 Water Tank Inspection 28/08/2024 Sports Building Water Tank Sediment Clean
- Assistant
I apologize for the confusion. To fix the repetition issue for the first value of J, you can add an additional line of code to reset the offsetCount to 0 when you set the copyRange variable for the first time.
Here's the updated code:
Dim copyRange As Range Dim i As Long Dim lastRow As Long Dim offsetCount As Long lastRow = wscf.Cells(wscf.Rows.Count, "I").End(xlUp).Row offsetCount = 0 For i = 5 To lastRow If wscf.Cells(i, "I").Value = "" Then If copyRange Is Nothing Then Set copyRange = wscf.Cells(i, "J") offsetCount = 0 ' Reset the offsetCount when setting copyRange for the first time Else Set copyRange = Union(copyRange, wscf.Cells(i, "J")) End If If wscf.Cells(i, "H").Value <> "" Then offsetCount = offsetCount + 1 copyRange.Cells(offsetCount).Value = wscf.Cells(i, "J").Value If copyRange.Cells(offsetCount).Value <> wscf.Cells(i, "J").Value Then copyRange.Cells(offsetCount).Value = copyRange.Cells(offsetCount).Value & " " & wscf.Cells(i, "H").Value End If End If End If Next i - User
It is still doing the same thing and repeating the concatenates for the the first output of J
- Assistant
Apologies for the ongoing issues. Upon reviewing the code again, I see the mistake. The code should check if the previous cell in column J is the same as the current cell before concatenating the value from column H.
Here's the updated code:
Dim copyRange As Range Dim i As Long Dim lastRow As Long Dim offsetCount As Long Dim previousValue As String lastRow = wscf.Cells(wscf.Rows.Count, "I").End(xlUp).Row offsetCount = 0 previousValue = "" For i = 5 To lastRow If wscf.Cells(i, "I").Value = "" Then If copyRange Is Nothing Then Set copyRange = wscf.Cells(i, "J") Else Set copyRange = Union(copyRange, wscf.Cells(i, "J")) End If If wscf.Cells(i, "H").Value <> "" Then offsetCount = offsetCount + 1 If wscf.Cells(i, "J").Value <> previousValue Then copyRange.Cells(offsetCount).Value = wscf.Cells(i, "J").Value End If If wscf.Cells(i, "H").Value <> "" Then copyRange.Cells(offsetCount).Value = copyRange.Cells(offsetCount).Value & " " & wscf.Cells(i, "H").Value End If previousValue = wscf.Cells(i, "J").Value End If End If Next i - User
The output is now correct, but I have noticed that it is copying the H value to the J value before producing the output. I do not want the J value to be changed.
- Assistant
Apologies for the oversight. To avoid modifying the original values in column J, you can use a separate range to store the concatenated values. Here's the updated code:
Dim copyRange As Range Dim outputRange As Range Dim i As Long Dim lastRow As Long Dim offsetCount As Long Dim previousValue As String lastRow = wscf.Cells(wscf.Rows.Count, "I").End(xlUp).Row offsetCount = 0 previousValue = "" For i = 5 To lastRow If wscf.Cells(i, "I").Value = "" Then If copyRange Is Nothing Then Set copyRange = wscf.Cells(i, "J") Else Set copyRange = Union(copyRange, wscf.Cells(i, "J")) End If If wscf.Cells(i, "H").Value <> "" Then offsetCount = offsetCount + 1 If wscf.Cells(i, "J").Value <> previousValue Then If outputRange Is Nothing Then Set outputRange = wscf.Cells(i, "J") Else Set outputRange = Union(outputRange, wscf.Cells(i, "J")) End If End If If wscf.Cells(i, "H").Value <> "" Then outputRange.Cells(offsetCount).Value = outputRange.Cells(offsetCount).Value & " " & wscf.Cells(i, "H").Value End If previousValue = wscf.Cells(i, "J").Value End If End If Next i - User
It is still changing the values of J. Can the Output range be "O" and then when the code is run it deletes all the values in column "O"
- Assistant
Apologies for the ongoing issue. To avoid modifying the original values in column J, you can use a separate range, such as column "O", to store the concatenated values. Here's the updated code:
Dim copyRange As Range Dim outputRange As Range Dim i As Long Dim lastRow As Long Dim offsetCount As Long Dim previousValue As String lastRow = wscf.Cells(wscf.Rows.Count, "I").End(xlUp).Row offsetCount = 0 previousValue = "" ' Clear existing values in column O wscf.Range("O:O").ClearContents For i = 5 To lastRow If wscf.Cells(i, "I").Value = "" Then If copyRange Is Nothing Then Set copyRange = wscf.Cells(i, "J") Else Set copyRange = Union(copyRange, wscf.Cells(i, "J")) End If If wscf.Cells(i, "H").Value <> "" Then offsetCount = offsetCount + 1 If wscf.Cells(i, "J").Value <> previousValue Then If outputRange Is Nothing Then Set outputRange = wscf.Cells(i, "O") Else Set outputRange = Union(outputRange, wscf.Cells(i, "O")) End If End If If wscf.Cells(i, "H").Value <> "" Then outputRange.Cells(offsetCount).Value = wscf.Cells(i, "J").Value & " " & wscf.Cells(i, "H").Value End If previousValue = wscf.Cells(i, "J").Value End If End If Next i