How do I concatenate rows between cells of specified value? - excel

I am trying to use VBA to concatenate everything between two specified rows. What's the best way to go about this?
Basically I want to leave the lines where the third cell is "U" intact, and make the sixth cell of that row the concatenation of the rows below, until we run into another row that contains a "U" in the third cell. Then the process would repeat. The number of rows between the cells containing "U" is varied.
Pic is below

Ok, this should work (haven't tested it though):
Sub My_Amazing_Skills()
Dim l As Long, i As Long
l = 1
i = 1
Do Until i > Range("A1048576").End(xlUp).Row
If Range("C" & l).Value = "U" Then
i = i + 1
Do Until Range("C" & i).Value = "U"
Range("F" & l).Value = Range("F" & l).Value & " " & Range("C" & i).Value
i = i + 1
Loop
Range("F" & l).Value = Trim(Range("F" & l).Value)
End If
l = i
Loop
MsgBox "Bow down to the great Jeremy!", vbInformation, "Your concatenating is done"
End Sub
I presume you know know where to copy this to?

Related

Types Mismatch error on range value, can't figure out why

So Every time I run my code I get the Types Mismatch error and I can't find what to change into what in order to run it properly. Even though this might still not entirely Solve the greater problem which is how to formulate my question into the vba code, but first things first. How to get this line of code without errors:
Range("I" & r & ":" & "I" & B).Value = Range("I" & r & ":" & "I" & B).Value + 1
Where r = the current row the code is checking ( 5 to 44)
and B is the last row the code can check ( 44)
All I want this line to do is to add one to the already existing value of the cell (which is 0 if nothing is done in that row or a formula if conditions are met, this formula will make a value from 1 to 40)
You cannot do this the way you are doing -> you cannot add a number to a range of cells at once. You must add the number to one cell at a time.
Try this:
Dim i As Integer
For i = r To B
Range("I" & i).Value = Range("I" & i).Value + 1
Next
To handle formulas:
Dim i As Integer
For i = r To B
Dim numberToAdd As Integer
numberToAdd = 1
If Range("I" & i).HasFormula Then
Range("I" & i).Value = Range("I" & i).Formula & "+" & Trim(Str(numberToAdd))
Else
Range("I" & i).Value = Range("I" & i).Value + 1
End If
Next

Count one column if unique values in another column VBA

I have a spreadsheet that I need to sum how many values "UDL" there is in column(N:N), but the problem is they should only be counted for every unique number in column (C.C)
So for every number that also has "UDL" in column N they should only be counted once as there can be a lot of duplicates.
Hope you can help :)
Seems like a loop & delimited string would be a quick way to get what you're wanting. Assuming Column A is always filled & has a header in row 1, something like:
Dim u as string
Dim i as long
dim t as long
i = 2
Do While Range("A" & i).value <> ""
If Range("N" & i).value = "UDL" Then
If InStr(1, u, "|" & Range("C" & i).value & "|") = 0 Then
t = t + 1
u = u & "|" & Range("C" & i).value & "|"
End If 'else the value in C is non-unique
End If 'else not UDL so no action needed
i = i + 1
Loop
Range("A" & i + 1).value = t 'Outputs t to column A with a blank row between it & your data
That'll need some adjusting based on what column should control the loop, what row the loop should start on, and where you want the count to go.

Find both duplicates in the same ROW VBA

Somehow after few days of googleing I didn't find any satisfying answer.
I have to find duplicates in the same column and copy them both (or more) to the new sheet to show where are the issues.
The only way I managed to do that was
For i = 2 To lastCell
If dataArray(i, 3) <> "" Then
For j = i + 1 To lastCell
If dataArray(i, 3) = dataArray(j, 3) Then
results.Range("A" & k & ":" & lastCol & k).Value = checkbook.Range("A" & i & ":" & lastCol & i).Value '
results.Range(commentAddress & k).Value = "Duplicate ID"
k = k + 1
results.Range("A" & k & ":" & lastCol & k).Value = checkbook.Range("A" & j & ":" & lastCol & j).Value
results.Range(commentAddress & k).Value = "Duplicate ID"
k = k + 1
End If
Next j
End If
Next I
But this is taking too long! I found that dictionary could be very helpful but don't really know how to use this - and it only shows the SECOND value (I need both)
So are there any other solutions to find duplicates? I need the fastest one as the file I am working on has 100K+ rows (loop in a loop is killing me)
You could try something similar to this to recreate the column with blanks replacing the duplicate values. Then you can just filter on blank values in the column to find which values or IDs are duplicates. Or write a formula that loops through column and collects addresses of cells that are empty.
=IF(A1="";"";IF(COUNTIF(A1:A100;A1)=1;A1;""))
Replace A1:A100 with the range your data occupy, or the whole column if you prefere.

VBA code to select a cell when specific letters appears and sort

I need some help to understand and modify some code I found in this answer:
Sub Main()
Dim cell As Range
Dim nextRow As Long
For Each cell In Range("K50:K200")
If StrComp(cell, "Late", vbTextCompare) = 0 Then
nextRow = Range("J" & Rows.Count).End(xlUp).Row + 1
Range("J" & nextRow) = Range("B" & cell.Row)
Range("K" & nextRow) = Range("C" & cell.Row)
Range("N" & nextRow) = Range("G" & cell.Row)
Range("O" & nextRow) = Range("H" & cell.Row)
End If
Next
End Sub
What I'd like is:
If late is along with some other words, say late submit, copy values in B and paste in J
if row K has progress along with say in progress, copy values in C to K
If sick is with say on sick leave, copy values in G to N
I tried so hard, but I still didn't succeed in modifying this code to suit my needs.
If late is along with some other words, say late submit
In that case, instead of StrComp, use Instr
If Instr(1, cell, "Late", vbTextCompare) > 0 Then
Similarly for the rest.
If you check Excel's help you will notice that Instr returns a Variant (Long) specifying the position of the first occurrence of one string within another.
Syntax
InStr([start, ]string1, string2[, compare])

VBA script when dividing by zero

I am trying to do a formula in VBA, but I am coming across errors because of a zero in the denominator. If I have a zero in the denominator, I'd like the active cell to be set to zero. Whatever I am doing is incorrect, and I am not a programmer. I have no idea what I'm doing. Any help would be greatly appreciated. Here is what I
Range("H" & Row).Activate
If Range("F" & Row) = 0 Then ActiveCell.Formula = 0
ActiveCell.Formula = Range("G" & Row) / Range("F" & Row)
try using an else
something like
Range("H" & Row).Activate
If Range("F" & Row) = 0 Then
ActiveCell.Formula = 0
Else
ActiveCell.Formula = Range("G" & Row) / Range("F" & Row)
End If
You could just build the statement into the formula:
For Row = 1 To 3
Cells(Row, "H").Formula = "=IF(F" & Row & "=0,0,G" & Row & "/F" & Row & ")"
Next Row
And instead of using a loop...
With Range("H2:H" & Cells(Rows.Count, "G").End(xlUp).Row)
.Formula = "=IF(F" & .Row & "=0,0,G" & .Row & "/F" & .Row & ")"
.Value = .Value
End With
I do not recommend Cor_Blimey or tigeravatar recommend - both make the code less readable. Cor_Blimey's suggestion to use the line continuation character " _" and the end of statement character ":" do not add to the succinctness of the code and is logically the same as the code above. Tigeravatar is sortof on the right track with suggesting that the cell handle the error case itself, but his code suggestion makes everything more difficult to read.
My suggested change is to drop the VBA entirely and use the following formula in the cell(s) in column H that would be doing the calculations:
=IfError(G1/F1,0)
This would work because you are not using any values in your calculations other than what is already on the worksheet.

Resources