logandiana
Board Regular
- Joined
- Feb 21, 2017
- Messages
- 107
I am trying to combine multiple entries on multiple columns to create one long list.
I am using nested For Next loops. The code does exactly what I want it to do as I step through it, but crashes Excel every time when I run it outright.
Not sure what I need to do differently.
The data is similar to this:
1st data
1 ABC
1 DEF
1 GHI
2 CBA
2 FED
2 IHG
2nd data
1 XYZ
1 ZYX
2 LMN
2 NML
End data needed
1 XYZ ABC
1 XYZ DEF
1 XYZ GHI
1 ZYX ABC
1 ZYX DEF
1 ZYX GHI
2 LMN CBA
2 LMN FED
2 LMN IHG
2 NML CBA
2 NML FED
2 NML IHG
On the orders tab (Ord) I have 4 columns and a total of 3784 lines. On the mail tab (mail) I have 2 columns with 20764 lines. The total unique lines at the end should be 33072.
Here's my code.
I keep getting a message that "excel has stopped working and will restart"
I am using nested For Next loops. The code does exactly what I want it to do as I step through it, but crashes Excel every time when I run it outright.
Not sure what I need to do differently.
The data is similar to this:
1st data
1 ABC
1 DEF
1 GHI
2 CBA
2 FED
2 IHG
2nd data
1 XYZ
1 ZYX
2 LMN
2 NML
End data needed
1 XYZ ABC
1 XYZ DEF
1 XYZ GHI
1 ZYX ABC
1 ZYX DEF
1 ZYX GHI
2 LMN CBA
2 LMN FED
2 LMN IHG
2 NML CBA
2 NML FED
2 NML IHG
On the orders tab (Ord) I have 4 columns and a total of 3784 lines. On the mail tab (mail) I have 2 columns with 20764 lines. The total unique lines at the end should be 33072.
Here's my code.
Code:
Dim j as Long
Dim k as Long
LR4 = Ord.Cells(Rows.Count, 1).End(xlUp).Row
LR5 = mail.Cells(Rows.Count, 1).End(xlUp).Row
For j = 2 To LR5
For k = 2 To LR4
If Ord.Cells(k, 1) = mail.Cells(j, 1) Then
Range(Ord.Cells(k, 2), Ord.Cells(k, 5)).Copy
Range("F" & mail.Cells(Rows.Count, 6).End(xlUp).Row).Offset(1, 0).PasteSpecial xlPasteValues
Range("D" & mail.Cells(Rows.Count, 4).End(xlUp).Row).Offset(1, 0).Value = mail.Cells(j, 1).Value
Range("E" & mail.Cells(Rows.Count, 5).End(xlUp).Row).Offset(1, 0).Value = mail.Cells(j, 2).Value
End If
Next k
Next j
I keep getting a message that "excel has stopped working and will restart"