Hi,
I have a complex procedure I'd like to do in a macro, and I'm having trouble getting it to work.
I work with contract management for support contracts. When I download a customer's contract information, the output file looks horrible, so I'm working on a macro to clean it up. I've already got it where it moves columns around and adds a filter, but there is more I'd like it to do.
When the data comes out, it's not sorted very well. I want to have the data sorted via three columns.
Column J is the end date, Column X is a unique identifier for each component of a configuration, Column C shows whether that component is the major line, or a minor line.
I've found the code that should do it, but it's not doing any actual sorting:
I've already got LastRow defined elsewhere in the macro as the number of rows of data in the report.
But this is only step one. There may also be minor lines with different expiration dates, so I want to create a loop that will check each line to see if it is a major or minor line, if it's a minor line, I want it to check if the value in column X matches the value of the line above it, and if not, I;d like it to cut that line, find the correct value in column X, insert that cut line below the line it just found, then return to where it was and start again. I've started, but this just messes everything up:
Any help would be appreciated.
I have a complex procedure I'd like to do in a macro, and I'm having trouble getting it to work.
I work with contract management for support contracts. When I download a customer's contract information, the output file looks horrible, so I'm working on a macro to clean it up. I've already got it where it moves columns around and adds a filter, but there is more I'd like it to do.
When the data comes out, it's not sorted very well. I want to have the data sorted via three columns.
Column J is the end date, Column X is a unique identifier for each component of a configuration, Column C shows whether that component is the major line, or a minor line.
I've found the code that should do it, but it's not doing any actual sorting:
Code:
With ActiveSheet.Sort
With .SortFields
.Clear
.Add Key:=Range("J2:J" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=Range("X2:X" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
.Add Key:=Range("C2:C" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I've already got LastRow defined elsewhere in the macro as the number of rows of data in the report.
But this is only step one. There may also be minor lines with different expiration dates, so I want to create a loop that will check each line to see if it is a major or minor line, if it's a minor line, I want it to check if the value in column X matches the value of the line above it, and if not, I;d like it to cut that line, find the correct value in column X, insert that cut line below the line it just found, then return to where it was and start again. I've started, but this just messes everything up:
Code:
Dim Instance As String
For i = 2 To LastRow
If Range("C" & i).Value = "Minor" Then
If Range("X" & i).Value <> Range("X" & i + 1).Value Then
Instance = Range("X" & i).Value
Rows(ActiveCell.Row).Cut
Range("X2:X" & i).Find(What:=Instance).Select
Rows(ActiveCell.Row).Insert shift:=xlShiftUp
Application.CutCopyMode = False
End If
End If
Next i
Any help would be appreciated.