【发布时间】:2012-07-13 08:10:32
【问题描述】:
第一阶段
我正在尝试搜索具有字符串值(公式、文本、数字等)的范围内每一行的第一列。该字符串是通过从下拉列表中选择生成的。选择的格式类似于“Desktop,Dell,790 - 4GB”,我只关心第一个逗号之前的文本字符串(在本例中也称为“Desktop”。)我正在使用 Split() 方法来获取第一个逗号之前的单词,然后尝试使用 Case 语句将字符串插入同一行的另一个单元格中。
第二阶段
我正在使用在第一个下拉列表中选择的内容来填充具有预定值的第二个下拉列表。
问题
程序抛出运行时错误“1004”:应用程序定义或对象定义错误。我不知道从哪里开始。
原始代码
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
【问题讨论】:
-
三件事 1)
Dim rng, row As Range改为Dim rng As Range, row As Range否则rng将被声明为变体。也为其他人更改 2)Range(row, 8).Value应该为其他人更改为cells(row, 8).Value3)同样关于For Each row In rng.Rows,rng只是 1 个单元格,它不会有行。你到底想做什么? -
我想我明白你要做什么了……等一下……
-
好的,我修改了代码。见 OP。现在我的错误是:无效的过程调用或参数
-
所有的下拉菜单都在 Col B 还是 Col A 中?...我几乎完成了对代码的更改...。
-
第一个下拉菜单在 Col A,第二个下拉菜单将在 Col H(第 2 阶段)
标签: excel excel-2010 vba