Excel VBA: Nested Loop does not concatenate values - excel

The Sender (Consignor) and Receiver (Consignee) addresses are spread out in 3 different columns each. Hence I create 1 Consignor Address Column and 1 Consignee Address Column into which I want to concatenate the data.
Then I write 2 loops to go through and concatenate the values from the 3 cells.
The first loop works perfectly, while the second one does not really add the values, though when tested with F8 it does run the code so to say.
Can someone explain to me why that is?
Sub CnorCneeAddress()
Columns("Q:Q").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("Q1").Select
ActiveCell.FormulaR1C1 = "Consignee Address"
Columns("I:I").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("I1").Select
ActiveCell.FormulaR1C1 = "Consignor Address"
Dim Worksheet As Worksheet
Set Worksheet = ActiveSheet
LastRow = Worksheet.Cells(Worksheet.Rows.Count, "A").End(xlUp).Row
With Worksheet
For i = 2 To LastRow 'Starting from Row 2 until the LastRow (i , will change depending on the data in column j)
For j = 10 To 12
If IsEmpty(Cells(i, j).Value) Then
Cells(i, 9).Value = Cells(i, 9).Value
ElseIf j = 12 Then
Cells(i, 9).Value = Cells(i, 9).Value & Cells(i, j).Value
Else
Cells(i, 9).Value = Cells(i, 9).Value & Cells(i, j).Value & ", "
End If
Next j
Next i
End With
With Worksheet
For i = 2 To LastRow 'Starting from Row 2 until the LastRow (i , will change depending on the data in column j)
For j = 14 To 16
If IsEmpty(Cells(i, j).Value) Then
Cells(i, 17).Value = Cells(i, 17).Value
ElseIf j = 16 Then
Cells(i, 17).Value = Cells(i, 17).Value & Cells(i, j).Value
Else
Cells(i, 17).Value = Cells(i, 17).Value & Cells(i, j).Value & ", "
End If
Next j
Next i
Columns("M:O").Delete
Columns("J:L").Delete
End With
End Sub
While the data for the Consignor is concatenated correctly, the cells under Consignor Address remain empty.
I do not understand why my code does not concatenate the cells for the Consignee Address.

thank you for your comments. Let me try to make things clearer.
I have data looking roughly so, after the column insertion, that is considered:
Columns 10, 11, 12 are "Consignor City", "Consignor State", "Consignor Post Code"
Column 14, 15, 16 are "Consignee City", Consignee State", "Consignee Post Code"
All variables are strings or integers e.g. if postcode in US..
Please see below picture.
Currently I have the code below running somewhat. I am working with VBA since 4 days, I have taken advanced Python before, so forgive me for still not understanding exactly how VBA works.
How the columns look
Sub CnorCneeAddress()
Columns("N:N").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("N1").Select
ActiveCell.FormulaR1C1 = "Consignee Address"
Columns("I:I").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("I1").Select
ActiveCell.FormulaR1C1 = "Consignor Address"
Dim Worksheet As Worksheet
Set Worksheet = ActiveSheet
lastRow = Worksheet.Cells(Worksheet.Rows.Count, "C").End(xlUp).Row
With Worksheet
For i = 2 To lastRow 'Starting from Row 2 until the LastRow (i , will change depending on the data in column j)
For j = 10 To 12
If IsEmpty(Cells(i, j).Value) Then
Cells(i, 9).Value = Cells(i, 9).Value
ElseIf j = 12 Then
Cells(i, 9).Value = Cells(i, 9).Value & Cells(i, j).Value
Else
Cells(i, 9).Value = Cells(i, 9).Value & Cells(i, j).Value & ", "
End If
Next j
Next i
End With
With Worksheet
For i = 2 To lastRow 'Starting from Row 2 until the LastRow (i , will change depending on the data in column j)
For j = 15 To 17
If IsEmpty(Cells(i, j).Value) Then
Cells(i, 14).Value = Cells(i, 14).Value
ElseIf j = 17 Then
Cells(i, 14).Value = Cells(i, 14).Value & Cells(i, j).Value
Else
Cells(i, 14).Value = Cells(i, 14).Value & Cells(i, j).Value & ", "
End If
Next j
Next i
Columns("O:Q").Delete
Columns("J:L").Delete
End With
End Sub

Related

For Loop in Worksheet change malfunction

Please I have an issue, everytime a change occcurs on the sheet it affects all the rows instead of the row (i) concerned. Confused. Don't for-loops work for worksheet_change ? Pls help. Thanks.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long
'create a variable for last row of column C, LR
LR = Cells(Rows.Count, "C").End(xlUp).Row
For i = 2 To LR
If Cells(i, 6) = "Yes" And Cells(i, 7).Value = "Full" Then
Target.Value = Cells(i, 3).Value
Cells(i, 9).ClearContents
Cells(i, 10).Value = Cells(i, 8).Value + Cells(i, 9).Value
End If
If Not Intersect(Target, Range("G" & i & ":G" & LR)) Is Nothing And Range("F" & i) = "Yes"
And Target.Value = "Full" Then
Application.EnableEvents = False
Cells(i, 8).Value = Cells(i, 3).Value
Cells(i, 9).ClearContents
Cells(i, 10).Value = Cells(i, 8).Value + Cells(i, 9).Value
Application.EnableEvents = True
End If
If Not Intersect(Target, Range("G" & i & ":G" & LR)) Is Nothing And Range("F" & i) = "Yes" And
Target.Value = "Portion" Then
Application.EnableEvents = False
Cells(i, 8).Value = Cells(i, 3).Value
Cells(i, 10).Value = Cells(i, 8).Value + Cells(i, 9).Value
Application.EnableEvents = True
End If
Next i
End Sub
It seems you need to launch this event for the columns A-E. So, you can start your macro with:
IF Target.Column <= 5 THEN
...
END IF 'at the end of your macro
Like this, when you launch code like Cells(i, 8).Value = ..., Cells(i, 10).Value = ..., ... this macro will be called but it will be stopped immediately.
Apparently you are checking on column, maximum 10, which is in the range of the cells you are changing within your macro. Let's go for another approach:
At the very beginning of your macro, put this line:
Application.EnableEvents = False
At the very end of your macro, put this line:
Application.EnableEvents = True
(and remove the other occurences).
This will make sure you don't call your macro while running it.

My code does not place the values every row, instead skips rows based on source sheet layout

I have a piece of code that searches through Sheet("Sub Tasks") and if the number in column A is a decimal, it then makes cells in Sheet("PBS") Column D = to the offset of the decimal cell found in column A. However, I would like the code to work by making row 2,3,4,5,6, and so on (+1) in sheet PBS display the info from Sub tasks.
But at the moment, the info is displayed in the same layout as it is in Sheet("Sub Tasks"). What am i missing from the code.
Dim Lastrow3 As Long, r As Long, n As Long, cell As Range, ws As Worksheet, Lastrow1 As Long
Lastrow3 = Sheets("Sub Tasks").Range("B" & Rows.Count).End(xlUp).Row
Lastrow1 = Sheets("PBS ").Range("D" & Rows.Count).End(xlUp).Row
n = 2
With Worksheets("Sub Tasks")
For Each cell In Sheets("Sub Tasks").Range("A2:A" & Lastrow3)
If IsNumeric(cell.value) Then
If cell.value = Int(cell.value) Then
Worksheets("PBS ").Cells(n, "C").value = cell.value
Worksheets("PBS ").Cells(n, "D").value = cell.Offset(0, 1).value
Worksheets("PBS ").Cells(n, "B").value = cell.Offset(0, 8).value
Worksheets("PBS ").Cells(n, "A").value = cell.Offset(0, 7).value
Worksheets("PBS ").Cells(n, "H").value = cell.Offset(0, 23).value
Worksheets("PBS ").Cells(n, "E").value = cell.Offset(0, 3).value
Else
End If
Else
End If
n = n + 1
Next cell
End With
``````````````````````````
move n = n + 1 to another place.
n = 2
With Worksheets("Sheet1")
For Each cell In Sheets("Sheet1").Range("A2:A" & Lastrow3)
If IsNumeric(cell.Value) Then
If cell.Value = Int(cell.Value) Then
Worksheets("Sheet3").Cells(n, "C").Value = cell.Value
Worksheets("Sheet3").Cells(n, "D").Value = cell.Offset(0, 1).Value
Worksheets("Sheet3").Cells(n, "B").Value = cell.Offset(0, 8).Value
Worksheets("Sheet3").Cells(n, "A").Value = cell.Offset(0, 7).Value
Worksheets("Sheet3").Cells(n, "H").Value = cell.Offset(0, 23).Value
Worksheets("Sheet3").Cells(n, "E").Value = cell.Offset(0, 3).Value
n = n + 1
Else
End If
Else
End If
Next cell
End With

How to sum and remove duplicates on 2 columns

Application Match for one column works but for 2 columns is giving me error
With Sht
LastRow = .Cells(.Rows.Count, "J").End(xlUp).Row
For i = LastRow To 2 Step -1
DupRow = Application.Match(Cells(i, 9).Value, Range(Cells(1, 9), Cells(i - 1, 9)), 0)
DoEvents
If Not IsError(DupRow) Then
Cells(i, 8).Value = Cells(i, 8).Value + Cells(DupRow, 8).Value
Cells(i, 9).Value = Cells(i, 9).Value + Cells(DupRow, 9).Value
Rows(DupRow).Delete
End If
Next i
End With
for 2 columns Error runtime 1004
DupRow = Application.Match(Cells(i, 4).Value & Cells(i, 5).Value, Range(Cells(1, 4) & Cells(1, 5), Cells(i - 1, 4) & Cells(i - 1, 5)), 0)
What is the correct way of doing this?
In my opinion it's better to use dictionary for that, which has built-in method for managing keys, so it can be used to get unique values:
Sub teest()
Dim val As String
Set dict = CreateObject("Scripting.Dictionary")
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = LastRow To 2 Step -1
val = Cells(i, 1).Value & Cells(i, 2).Value
If dict.Exists(val) Then
Rows(i).Delete
Else
dict.Add val, 0
End If
Next
End Sub

Excel: How do I 'gather' values to display in another cell

I have two columns, Column A has a set of a few standard values and column B has all unique values. I'm only just experimenting with more complex ways of compiling data than the beginner level so I'm a bit at a loss.
I need to either have a lookup or create a macro that will list only the values in A (once each) but also display which values in B correspond to those in A
for example
A | B
va1|abc
va1|bcd
Va2|xyz
va3|zab
will show (in a single cell) the following
va1: abc, bcd
va2: xyz
va3: zab
Please help!
Option Explicit
Sub Test()
Dim i As Long, j As Long, k As Long
k = 1
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Application.CountIf(Range("C:C"), Cells(i, 1).Value) = 0 Then
Cells(k, 3).Value = Cells(i, 1).Value
For j = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(j, 1).Value = Cells(k, 3).Value And _
InStr(Cells(k, 4).Value, Cells(j, 2).Value) = 0 Then
If Cells(k, 4).Value = "" Then
Cells(k, 4).Value = Cells(j, 2).Value
Else
Cells(k, 4).Value = Cells(k, 4).Value & ", " & Cells(j, 2).Value
End If
End If
Next j
k = k + 1
End If
Next i
For i = 1 To Cells(Rows.Count, 3).End(xlUp).Row
Cells(i, 3).Value = Cells(i, 3).Value & ": " & Cells(i, 4).Value
Cells(i, 4).ClearContents
Next i
End Sub
Edited for single cell
In case your requirement is to "have the grouped data", and not exactly "have one single string per A", you can do this with a "pivot table" putting A and B in the row labels, like in the following picture:

Enter the workbook name to target sheet in loop

I have written the below script which will loop through a range of data and copy from one sheet and paste to another, which works fine. I’m now looking to add the workbook name to column D on each row the data is pasted yet all attempts fail.
This is what I’m trying, please help.
For i = 6 To LastRow
If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then
Range(Cells(i, 1), Cells(i, 4)).Select
Selection.Copy
erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
Worksheets("iForms").Cells(1, 5).Value = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
If Cells(i, 1) <> "" Then Cells(i, 22).Value = "Yes"
If Cells(i, 22) <> "" Then Cells(i, 23).Value = Time
If Cells(i, 23) <> "" Then Cells(i, 24).Value = Environ("UserName")
ActiveWorkbook.Save
End If
Next i

Resources