首页 > 解决方案 > VBA Excel在范围内查找字符串不适用于CALL语句

问题描述

我想从另一个宏触发我的宏。

我的第一个宏如下所示:

 Sub Basic()
 Dim wr As Worksheet
 Dim myDataObject As DataObject
 Set myDataObject = New DataObject
 Set wr = ThisWorkbook.Sheets("Data")

 myDataObject.GetFromClipboard
 If myDataObject.GetFormat(1) = True Then
 wr.Range("A1").PasteSpecial
 Else
 MsgBox ("Please provide the data")
 Exit Sub
 End If
 If wr.Range("A1").Value Like "*Ephemeris*" Then
 Call General
 ElseIf wr.Range("A1").Value Like "*Event*" Then
 Call Almanach
 ElseIf wr.Range("A1").Value Like "*Standstil*" Then
 Call Others
 Else
 MsgBox ("Please provide correct data")
 End If

我想从哪里调用另一个宏 - Almanach。

宏一般工作,但我不明白,为什么下面的代码完全省略:

   For Each Cell In work.Range("A5:A" & LastrouA)
   If InStr(1, Cell.Value, "FULL MOON") > 0 Then
   Cell.Interior.ColorIndex = 37
   End If
   Next Cell

在此处输入图像描述

而当我直接通过控制台运行这个宏时它可以工作(虽然很慢)。我试图通过创建按钮来解决它,但结果完全一样!该功能是否有其他选择,InStr或者我忘记了什么?

除了这些行之外,整个代码都有效。

 For Each Cell In work.Range("A3:A" & LastrouA)
 If InStr(1, Cell.Value, "FULL MOON") > 0 Then
    Cell.Interior.ColorIndex = 37
ElseIf InStr(1, Cell.Value, "0.") > 0 Then
        Cell.Interior.ColorIndex = 17
ElseIf InStr(1, Cell.Value, "Elong") > 0 Then
        Cell.Interior.ColorIndex = 36
ElseIf InStr(1, Cell.Value, "Shower") > 0 Then
        Cell.Interior.ColorIndex = 40
End If
If InStr(1, Cell.Value, "Eclipse") > 0 Then
        Cell.Interior.ColorIndex = 38
End If
Next Cell
For Each Cell In work.Range("B3:B" & LastrouA)
If InStr(1, Cell.Value, "FULL MOON") > 0 Then
        Cell.Interior.ColorIndex = 37
ElseIf InStr(1, Cell.Value, "0.") > 0 Then
        Cell.Interior.ColorIndex = 17
ElseIf InStr(1, Cell.Value, "Elong") > 0 Then
        Cell.Interior.ColorIndex = 36
ElseIf InStr(1, Cell.Value, "Shower") > 0 Then
        Cell.Interior.ColorIndex = 40
End If
If InStr(1, Cell.Value, "Eclipse") > 0 Then
        Cell.Interior.ColorIndex = 38
End If
 Next Cell

 For Each Cell In work.Range("A3:A" & LastrouA)
 If InStr(1, Cell.Value, "Jan") > 0 Then
        Cell.Offset(-1, 0).Value = "January"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 ElseIf InStr(1, Cell.Value, "Feb") > 0 Then
        Cell.Offset(-1, 0).Value = "February"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 ElseIf InStr(1, Cell.Value, "Mar ") > 0 Then
        Cell.Offset(-1, 0).Value = "March"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 ElseIf InStr(1, Cell.Value, "Apr") > 0 Then
        Cell.Offset(-1, 0).Value = "April"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 ElseIf InStr(1, Cell.Value, "May") > 0 Then
        Cell.Offset(-1, 0).Value = "May"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 ElseIf InStr(1, Cell.Value, "Jun") > 0 Then
        Cell.Offset(-1, 0).Value = "June"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 End If
 Next Cell

 For Each Cell In work.Range("B3:B" & LastrouA)
 If InStr(1, Cell.Value, "Jul") > 0 Then
        Cell.Offset(-1, 0).Value = "July"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 ElseIf InStr(1, Cell.Value, "Aug") > 0 Then
        Cell.Offset(-1, 0).Value = "August"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 ElseIf InStr(1, Cell.Value, "Sep ") > 0 Then
        Cell.Offset(-1, 0).Value = "September"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 ElseIf InStr(1, Cell.Value, "Oct") > 0 Then
        Cell.Offset(-1, 0).Value = "October"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 ElseIf InStr(1, Cell.Value, "Nov") > 0 Then
        Cell.Offset(-1, 0).Value = "November"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 ElseIf InStr(1, Cell.Value, "Dec") > 0 Then
        Cell.Offset(-1, 0).Value = "December"
        Cell.Offset(-1, 0).Font.Bold = True
        Cell.Offset(-1, 0).HorizontalAlignment = xlCenter
 End If
 Next Cell

根据下面的屏幕截图,我只看到循环中的一个结果:

在此处输入图像描述

标签: excelvba

解决方案


推荐阅读