2012-07-12 109 views
1

阶段1VBA Excel-应用程序或对象定义的错误

我试图通过每一行的一个范围中的第一列具有的值(式中,文字,数字等,以搜索)为一个字符串。该字符串是通过从下拉列表中选择生成的。选择的格式类似于“桌面,戴尔,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 
+1

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

+0

我想我明白你在做什么......一会儿...... – 2012-07-12 22:03:39

+0

好吧,我修改了代码。见OP。现在我的错误是:无效的过程调用或参数 – 2012-07-12 22:06:10

回答

2

我有你的两个代码合并成1

PH ASE 1

这是你正在尝试的吗?

Private Sub Worksheet_Change(ByVal Target As Range)   
    On Error GoTo Whoa 

    Application.EnableEvents = False 

    If Not Intersect(Target, Columns(1)) Is Nothing Then 
     If Target.Cells.Count > 1 Then GoTo LetsContinue 

     If Target.Value <> "" And InStr(1, Target.Value, ",") Then 
      Select Case Split(Target.Value, ",")(0) 
       Case "Desktop": Range("H" & Target.row).Value = "Desktop" 
       Case "Laptop": Range("H" & Target.row).Value = "Laptop" 
       Case "Server": Range("H" & Target.row).Value = "Server" 
       Case Else:  Range("H" & Target.row).Value = "N/A" 
      End Select 
     End If 
    ElseIf Not Intersect(Target, Columns(2)) Is Nothing Then 
     If Target.Cells.Count > 1 Then GoTo LetsContinue 
     If Not Target.HasFormula Then Target.Value = UCase(Target.Value) 
    End If 

LetsContinue: 
    Application.EnableEvents = True 
    Exit Sub 
Whoa: 
    MsgBox Err.Description 
    Resume LetsContinue 
End Sub 
+0

好!这完美地解决了我的问题!只有一个问题。 Worksheet_Change子例程中的代码用于将Col B中的所有文本大写。在此代码中,我失去了该功能。 – 2012-07-12 22:21:34

+0

补充一点:) ..一会儿 – 2012-07-12 22:22:21

+0

更新了帖子。 – 2012-07-12 22:23:32

相关问题