Matt_in_CA
New Member
- Joined
- Feb 4, 2015
- Messages
- 5
Hello,
I’ve written the below VBA code, and it works, but it seems klunky. Can you streamline/improve?
This code populates a blank column based upon a possible text string in the neighboring cell. For example if the neighboring cell contains anywhere the string “DOG” then the value for the ActiveCell will be “CANINE”. If the neighbor cell contains “CAT” the ActiveCell will be “FELINE”. If the neighbor cell contains “HORSE”, the ActiveCell will be “EQUINE”. And so on.
The idea is that the macro creates a “sort code column” based upon a chunk of text from a file that downloads from an outside system. Then, I just SUMIF and manipulate the data now that each record has a category (sort) code.
I can attach a sample worksheet, but I don't see where in the forum to add attachments.
Like I say, it works, but there are 2 problems: 1. I think it might be a bit klunky. Is there any simpler way to code this? Like using Select-Case structure? Unlike my example, in the actual application there are about 40 different Sort/Category codes and hundreds of downloaded records. 2. The other problem is that my code does not provide for a multi criterion selection. In other words in the neighbor cell contains “CAT” and (anywhere) “SIAMESE”, then the ActiveCell should read “CAT-EXOTIC”. If the neighbor cell contains “HORSE” and “KENTUCKY” then the ActiveCell should read “RACEHORSE”. And so on.
CODE HERE:
Sub FillSortCodePost()
Do While ActiveCell.Offset(0, 1).Value <> Empty
If InStr(1, ActiveCell.Offset(0, 1).Value, "CAT") <> 0 Then ActiveCell.Value = "FELINE"
If InStr(1, ActiveCell.Offset(0, 1).Value, "DOG") <> 0 Then ActiveCell.Value = "CANINE"
If InStr(1, ActiveCell.Offset(0, 1).Value, "HORSE") <> 0 Then ActiveCell.Value = "EQUINE"
ActiveCell.Offset(1, 0).Select
Loop
End Sub
END OF CODE HERE.
Thanks so much!
Matt
I’ve written the below VBA code, and it works, but it seems klunky. Can you streamline/improve?
This code populates a blank column based upon a possible text string in the neighboring cell. For example if the neighboring cell contains anywhere the string “DOG” then the value for the ActiveCell will be “CANINE”. If the neighbor cell contains “CAT” the ActiveCell will be “FELINE”. If the neighbor cell contains “HORSE”, the ActiveCell will be “EQUINE”. And so on.
The idea is that the macro creates a “sort code column” based upon a chunk of text from a file that downloads from an outside system. Then, I just SUMIF and manipulate the data now that each record has a category (sort) code.
I can attach a sample worksheet, but I don't see where in the forum to add attachments.
Like I say, it works, but there are 2 problems: 1. I think it might be a bit klunky. Is there any simpler way to code this? Like using Select-Case structure? Unlike my example, in the actual application there are about 40 different Sort/Category codes and hundreds of downloaded records. 2. The other problem is that my code does not provide for a multi criterion selection. In other words in the neighbor cell contains “CAT” and (anywhere) “SIAMESE”, then the ActiveCell should read “CAT-EXOTIC”. If the neighbor cell contains “HORSE” and “KENTUCKY” then the ActiveCell should read “RACEHORSE”. And so on.
CODE HERE:
Sub FillSortCodePost()
Do While ActiveCell.Offset(0, 1).Value <> Empty
If InStr(1, ActiveCell.Offset(0, 1).Value, "CAT") <> 0 Then ActiveCell.Value = "FELINE"
If InStr(1, ActiveCell.Offset(0, 1).Value, "DOG") <> 0 Then ActiveCell.Value = "CANINE"
If InStr(1, ActiveCell.Offset(0, 1).Value, "HORSE") <> 0 Then ActiveCell.Value = "EQUINE"
ActiveCell.Offset(1, 0).Select
Loop
End Sub
END OF CODE HERE.
Thanks so much!
Matt