您好我在excel中有一个列表,其中包含以下值:CTP315-07-01,CTP315-07-51,CTP315-07-220。 Excel没有正确地对值进行排序,所以我试图创建一个执行该操作的脚本。我的代码运行,但由于某种原因它没有做任何事情。这里是。有谁知道我会如何排序?
Sub test()
Dim minValue As Integer
Dim minIndex As Integer
For i = 238 To 401
tmp = i
For j = i + 1 To 401
Dim w As Integer
Dim k As Integer
x = Right(Cells(j, 45).Value, 3)
y = Right(Cells(tmp, 45).Value, 3)
If Left(x, 1) = "-" Then
x = Right(x, 2)
If Left(x, 1) = "0" Then
x = Right(x, 1)
End If
End If
If Left(y, 1) = "-" Then
y = Right(y, 2)
If Left(y, 1) = "0" Then
y = Right(y, 1)
End If
End If
w = ConvertToInteger(x) 'this is the first value (j, 45)
k = ConvertToInteger(y) 'this is the 2nd value (tmp,45)
If w < k Then
tmp = j
End If
Next j
temp = Cells(i, 45)
Cells(i, 45) = Cells(tmp, 45)
Cells(tmp, 45) = temp
Next i
End Sub
Function ConvertToInteger(v1 As Variant) As Integer
On Error GoTo 100:
ConvertInteger = CInt(v1)
Exit Function
100:
MsgBox "Failed to convert """ & v1 & """ to an integer.", , "Aborting - Failed Conversion"
End
End Function
答案 0 :(得分:0)
如何进行以下操作,它将取值 - 并将其转换为整数,将该整数添加到相邻列,在这种情况下为AT,然后对该列进行排序:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, 45).End(xlUp).Row
'get the last row with data on Column B
For i = 2 To LastRow 'loop from row 2 to last
TrimText = ws.Cells(i, 45) 'get the value from cell to be trimmed
NewValue = Left(TrimText, 6) 'get the first 6 characters
ws.Cells(i, 46) = NewValue 'add that text to adjecent column AT
SecondValue = NewValue & "-" 'get the mid values
SecondValue = Trim(Replace(TrimText, SecondValue, "")) 'remove the first value from string
newpos = InStrRev(SecondValue, "-") 'find the second - dash
SecondValue = Left(SecondValue, newpos - 1) 'remove anything after the dash
ws.Cells(i, 47) = CInt(SecondValue) 'convert to Integer
pos = InStrRev(TrimText, "-") 'find the last - in text
If pos > 0 Then 'if found
NewValue = Right(TrimText, Len(TrimText) - pos) 'remove anything before dash -
ws.Cells(i, 48) = CInt(NewValue) 'convert value to integer, add that value to adjecent column AT
End If
Next i
ws.Cells.AutoFilter 'add filter
'below sort by column AT = 46
ws.AutoFilter.Sort.SortFields.Add Key:=Range("AT1:AT" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ws.AutoFilter.Sort.SortFields.Add Key:=Range("AU1:AU" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ws.AutoFilter.Sort.SortFields.Add Key:=Range("AV1:AV" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ws.Columns("AT:AV").Delete Shift:=xlToLeft 'delete the columns used to sort
End Sub
答案 1 :(得分:0)
您的代码无法正常工作的原因是因为您有错字。
如果您不知道它,则会有一行代码:Option Explicit
如果插入模块顶部,则需要定义所有变量。这对于捕获变量名称中的拼写错误非常有用。
还有一些调试技巧。打开代码编辑器(在Windows上)的快捷方式是Alt+F11
。在那里,您可以使用F8
键一次一行地执行代码。您还可以通过单击要中断(或暂停)的代码行的左侧来设置代码中的断点。
通常,在可能的情况下,人们首选确定发生问题的代码行。
当我逐步完成代码时,我能够将测试模块中有问题的行识别为:
w = ConvertToInteger(x) 'this is the first value (j, 45)
k = ConvertToInteger(y) 'this is the 2nd value (tmp,45)
因为w
和k
在这些行之后始终都是0
- 即使他们不应该这样做。
这使我得出结论:ConvertToInteger
函数实际上没有返回任何内容。
我建议仔细查看以下代码行,并检查拼写错误:
ConvertInteger = CInt(v1)
<强>澄清:强>
对于返回变量的函数,需要为其赋值。
Function plusTwo ()
plusTwo = 2 + 2 'plusTwo needs to match the name of the function otherwise it will be interpreted as a new variable
End Function