leighhobson89
New Member
- Joined
- Aug 25, 2016
- Messages
- 36
Hi guys,
Currently the simple code below reads two values on a row, one from column "C" and one from column "D". It concatenates them and writes the new string in column "B".
Sometimes the list in column C will contain more than one string exactly the same. I don't want it to write a value if it matches one previously written in the list, but I dont want empty rows, so I presume somewhere I need to include a one dimensional array and set a flag when a specific string is written, then before writing the next one, check all values in the array for a match, and skip if it finds one, otherwise write it in. I have read about arrays and I just can't get my head around them until I see a working example that I can relateto. Can anyone help finish this off for me? The code follows:
Currently the simple code below reads two values on a row, one from column "C" and one from column "D". It concatenates them and writes the new string in column "B".
Sometimes the list in column C will contain more than one string exactly the same. I don't want it to write a value if it matches one previously written in the list, but I dont want empty rows, so I presume somewhere I need to include a one dimensional array and set a flag when a specific string is written, then before writing the next one, check all values in the array for a match, and skip if it finds one, otherwise write it in. I have read about arrays and I just can't get my head around them until I see a working example that I can relateto. Can anyone help finish this off for me? The code follows:
Code:
Private Sub ClearButton_Click()
Range("A5:D5000").Value = ""
End Sub
Private Sub MergeButton_Click()
Dim result As String
Dim dumpSongName As String
Dim dumpArtist As String
Dim searchOrder As Integer
Dim selectionCellArtist As String
Dim selectionCellSong As String
Dim counter As Integer
'Count number of rows
counter = 0
selectionCellArtist = Cells(counter + 5, 3).Value
selectionCellSong = Cells(counter + 5, 4).Value
Do While selectionCellArtist <> "" And selectionCellSong <> ""
counter = counter + 1
selectionCellArtist = Cells(counter + 5, 3).Value
selectionCellSong = Cells(counter + 5, 4).Value
Loop
If counter = 0 Then
MsgBox ("You need to dump something in both the Artist and Song dump columns to merge them!")
End
End If
' Merge results and write in Search term field
searchOrder = 0
For i = 1 To counter [COLOR=#ff0000]' CHANGE THIS LOOP TO WRITE FIRST ITEM IN "Cells(i + 4), 2)" THEN CHECK IF FOLLOWING ITEMS MATCH ANY OF THE PREVIOUS ONES, IF NOT WRITE THEM IN THE CELL BELOW, IF MATCH THEN SKIP[/COLOR]
dumpArtist = Cells((i + 4), 3).Value
dumpSongName = Cells((i + 4), 4).Value
result = dumpArtist + " " + dumpSongName
If result = " " Then result = ""
If result = "" Then Exit For
Cells((i + 4), 2).Value = result
searchOrder = searchOrder + 1
Cells((i + 4), 1).Value = searchOrder
Next i
End Sub