excel - 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
根据下面的屏幕截图,我只看到循环中的一个结果:
解决方案
推荐阅读
- javascript - 使用背景图像显示存储为数据 URI 的 SVG:为什么属性值无效?
- php - PHP 套接字在几分钟后过期
- .net - 对于 IBM.WMQ .Net 参考,为什么我的代码没有加载应用程序特定版本的 amqmdnet.dll?
- sql-server - 在 SQL Server 中获取“_”之后和之前的数据
- c - 为什么我不断收到无效的反馈?
- python - 搜索特定文本集的列,如果找到文本,则在新列中输入新的文本字符串 pandas
- java - org.springframework.web.client.HttpClientErrorException:405 null / 警告:不支持请求方法“POST”
- recursion - 如何跟踪长度为 3 的数组的 QuickSort() 调用
- r - 如何从一个文件夹中读取多个文件,将数据合并到一个文件中,并添加一个带有原始文件名称的变量?
- javascript - 如何使用嵌套数组查找两个数组之间的差异