Macro Help
I realized last night when I've done this type of thing in the pasdt it
wasn't with Excel. I was using arrays. the code becomes messy with arays
because you have to push all the data down one location in an array when it
doesn't match. Excel automatically does the pushing of the Arrays (in this
case columns) by using the Insert Command. I like this code better.
Sub CompareStrings()
RowCount = 1
Do While Range("C" & RowCount) < "" Or _
Range("D" & RowCount) < ""
StringCompare = StrComp( _
Range("C" & RowCount), _
Range("D" & RowCount), _
vbTextCompare)
Select Case StringCompare
Case -1:
'Column C less than Column D
Range("D" & RowCount).Insert shift:=xlShiftDown
RowCount = RowCount + 1
Case 0:
'column Column C = Column D
RowCount = RowCount + 1
Case 1:
'Column D less than Column c
If Range("D" & RowCount) < "" Then
Range("C" & RowCount).Insert shift:=xlShiftDown
End If
RowCount = RowCount + 1
End Select
Loop
End Sub
"Stanley Braverman" wrote:
Redid Example
I have column C and column D that contains data. I need macro that will do
IF
THEN with a loop till done;
1st condition. If C1 = D1 then do nothing(but look at next cells C2 and D2
etc. Etc.
2nd condition. If C1 < D1 then move cells in column C down.
(one cell at a time until condition one meet again and continue loop)
3rd condition. If C1 D1 then move cells in column D down.
(one cell at a time until condition one meet again and continue loop)
Perhaps instead of a loop maybe an input for how many rows to do
By doing this cells that are being moved will leave a bank cell in it's
place and that is what is wanted.
Example: Before After
PS1495-15 PS1495-15 PS1495-15 PS1495-15
PS1495-16 PS1495-16 PS1495-16 PS1495-16
PS1495-17 PS1495-18 PS1495-17
PS1495-18 PS1495-19 PS1495-18 PS1495-18
PS1495-19 PSJT304-16 PS1495-19 PS1495-19
PSJT304-16 PSJT304-17 PSJT304-16 PSJT304-16
PSJT304-17 PSJT304-18 PSJT304-17 PSJT304-17
PSJT304-18 PSJT305-14 PSJT304-18
PSJT314-01 PSJT305-15 PS305-14
PSJT314-02 PSJT314-01 PS305-15
PSJT314-03 PSJT314-02 PSJT314-01 PS314-01
|