excel - 如何解决 VBA Excel 代码运行时错误 91
问题描述
我是宏的初学者。当我尝试执行代码时,我收到一条运行时错误 91 消息
Sub Worksheet_Functions()
Dim HighestScore As Integer
Dim LowestScore As Integer
Dim TopScore As Range
Dim BottomScore As Range
LowestScore = WorksheetFunction.Min(Range("B1", "B5"))
HighestScore = WorksheetFunction.Max(Range("B1", "B5"))
Set TopScore = Range("B1,B5").Find(What:=HighestScore)
Set BottomScore = Range("B1,B5").Find(What:=Lowesscore)
Range("A7").Value = "lowest score is "
Range("A8").Value = "Highest score is"
Range("B7").Value = BottomScore.Offset(-1, 0).Value
Range("B8").Value = TopScore.Offset(-1, 0).Value
End Sub
解决方案
我修复了你的代码。请看评论
Option Explicit
Sub Worksheet_Functions()
Dim HighestScore As Integer
Dim LowestScore As Integer
Dim TopScore As Range
Dim BottomScore As Range
LowestScore = WorksheetFunction.Min(Range("B1", "B5"))
HighestScore = WorksheetFunction.Max(Range("B1", "B5"))
Set TopScore = Range("B1,B5").Find(What:=HighestScore)
' In the posted code was a typo.
' Use Option Explicit to avoid these kind of errors.
Set BottomScore = Range("B1,B5").Find(What:=LowestScore)
Range("A7").Value = "lowest score is "
Range("A8").Value = "Highest score is"
' Find method returned a result only if BottomScore is not nothing
If Not BottomScore Is Nothing Then
' In case the result is in the first row you can't go one row above
If BottomScore.Row > 1 Then
Range("B7").Value = BottomScore.Offset(-1, 0).Value
End If
End If
' The same if conditions as with Bottomscore
If Not TopScore Is Nothing Then
If TopScore.Row > 1 Then
Range("B8").Value = TopScore.Offset(-1, 0).Value
End If
End If
End Sub
推荐阅读
- javascript - 在 GraphQL Resolvers 文件中定义一个函数
- javascript - 如何将输入文件中的数据保存到字典数组jQuery中
- github-actions - github 计划操作永远不会被触发
- java - 使用错误代码在枚举中搜索状态代码
- c# - 从用 C# 编写的 Windows 服务调用 Powershell 脚本
- c# - 连续单击时不显示列表视图上下文菜单
- python-3.x - Python 脚本处理并生成 html 格式的结果。我希望它使用 WSGI 在网络上发布
- c# - EF Core IdentityUser 与业务模型存在多对多关系,如何使用 .Include().ThenInclude()?
- java - 如何从二维数组(java)中打印出奇数索引元素?
- python - Scrapy 仅返回字母列表中每个字母的第一项