我试图通过每一行的一个范围中的第一列具有的值(式中,文字,数字等,以搜索)为一个字符串。该字符串是通过从下拉列表中选择生成的。选择的格式类似于“桌面,戴尔,790 - 4GB”,我只关注第一个逗号前面的文本字符串(本例中为AKA“桌面”)。我使用Split()方法来获取第一个逗号前的单词,然后尝试使用Case语句将字符串插入到同一行中的另一个单元格中。
二期
我在用的是在第一个下拉列表中选择来填充第二个下拉以预先确定的值列表。
的问题
的程序被投掷运行时错误 '1004':Application_defined或对象定义的错误。我不知道从哪里开始。
原始代码
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoops
Application.EnableEvents = False
Call splitter
If Not Intersect(Target, Range("B:B")) Is Nothing Then
If Not Target.HasFormula Then Target.Value = UCase(Target.Value)
End If
Whoops:
Application.EnableEvents = True
End Sub
Sub splitter()
Dim line() As String
Dim rng, row As Range
Dim lRow, lCol As Long
lRow = Cells.Find("*", Range("A1"), xlFormulas, , xlByRows, xlPrevious).row
lCol = Cells.Find("*", Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column
Set rng = Cells(lRow, lCol)
For Each row In rng.Rows
If row.Value <> "" Then
line = Split(Range(row, 1), ",")
Select Case line(0)
Case "Desktop"
Range(row, 8).Value = "Desktop"
Case "Laptop"
Range(row, 8).Value = "Laptop"
Case "Server"
Range(row, 8).Value = "Server"
Case Else
Range(row, 8).Value = "N/A"
End Select
End If
Next
End Sub
修改过的代码
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoops
Application.EnableEvents = False
Call splitter
If Not Intersect(Target, Range("B:B")) Is Nothing Then
If Not Target.HasFormula Then Target.Value = UCase(Target.Value)
End If
Whoops:
Application.EnableEvents = True
End Sub
Sub splitter()
Dim line() As String
Dim rng As Range, row As Range
Dim lRow As Long
lRow = Cells.Find("*", Range("A1"), xlFormulas, , xlByRows, xlPrevious).row
Set rng = Cells("A1:N" & lRow)
For Each row In rng.Rows
If row.Value <> "" Then
line = Split(Cells(row, 1), ",")
Select Case line(0)
Case "Desktop"
Cells(row, 8).Value = "Desktop"
Case "Laptop"
Cells(row, 8).Value = "Laptop"
Case "Server"
Cells(row, 8).Value = "Server"
Case Else
Cells(row, 8).Value = "N/A"
End Select
End If
Next
End Sub
3件事1)'Dim rng,row As Range'将它改为'Dim rng As Range,row As Range' else否则'rng'将被声明为变体。 2)'Range(row,8).Value'应该是'cells(row,8).Value'也适用于其他人。3)关于'For Each Row in rng.Rows','rng '只是1格,它不会有行。你究竟想要做什么? – 2012-07-12 21:55:17
我想我明白你在做什么......一会儿...... – 2012-07-12 22:03:39
好吧,我修改了代码。见OP。现在我的错误是:无效的过程调用或参数 – 2012-07-12 22:06:10