我正在努力寻找一个人'另一个Range.Find中的名称,但我一直得到运行时错误91 - 对象变量或未设置块变量。 发生了一些事情" rngFound"在" getPaid"。
Sub EmailClick()
Dim lastSeasonRow As Double
lastSeasonRow = Worksheets("Season 2014-2015").Range("A" & Worksheets("Season 2014-2015").Rows.Count).End(xlUp).Row
Dim lastSeasonEmailRow1 As Double
lastSeasonEmailRow1 = Worksheets("Email").Range("A" & Worksheets("Email").Rows.Count).End(xlUp).Row
Dim rng As Range
Dim rngFound As Range
Dim getPaid As Range
Dim ErrorEmail As String
Dim colMyCol As New Collection 'Our collection
For j = 2 To lastSeasonRow
Set rng = Worksheets("Email").Range("A2:A" & lastSeasonEmailRow1)
Set rngFound = rng.Find(Worksheets("Season 2014-2015").Cells(j, 1).Value)
If Not rngFound Is Nothing Then
' If its Found
If DoesItemExist(colMyCol, rngFound.Offset(0, 1).Value) = False Then
'Check If Already completed swimmer's family
Dim CountSwimmers As String
CountSwimmers = Application.CountIf(Worksheets("Email").Range("C2:C" & lastSeasonEmailRow1), rngFound.Offset(0, 2).Value)
If CountSwimmers > 1 Then
For s = 1 To CountSwimmers
If s = 1 Then
'If first swimmer
Set rng = Worksheets("Email").Range("C2:C" & lastSeasonEmailRow1)
Set rngFound = rng.Find(rngFound.Offset(0, 2).Value)
Debug.Print rngFound.Offset(0, -2).Value
Set rngBFound = rngFound
Else
'Next swimmer in family
Set rngFound = rng.FindNext(rngFound)
Debug.Print rngFound.Offset(0, -2).Value
********************** When Debugging, above line is Highlighted.
End If
********************************' TODO: Grab Worksheet's Name with persons' name and get Money column**
Set getPaid = Worksheets("Season 2014-2015").Range("A2:A" & lastSeasonRow).Find(rngFound.Offset(0, -2).Value)
If Not getPaid Is Nothing Then
'If its found
If getPaid.Offset(0, 14).Value <> "" Then
'If they do owe money
Debug.Print getPaid.Offset(0, 14).Value
Else
End If
End If
Next s
'write name to list, if name in array skip it, when lastSeasonRow, remove array.
colMyCol.Add (rngFound.Offset(0, -1).Value)
'TODO: change values below to strings that will correspond with aboves combined values
If rngFound.Value = "Michael" Then
Call Send_Email_Using_VBA(rngFound.Offset(0, 2).Value, rngFound.Offset(0, 1).Value, rngFound.Value, Worksheets("Season 2014-2015").Cells(j, 15).Value)
End If
Else
Debug.Print rngFound.Value
If Worksheets("Season 2014-2015").Cells(j, 15).Value <> "" Then
'If they do owe money
If rngFound.Offset(0, 3).Value <> "" Then
'if multiple emails (primary and cc)
If rngFound.Value = "Michael" Then
Call Send_Email_Using_VBA(rngFound.Offset(0, 2).Value, rngFound.Offset(0, 1).Value, rngFound.Value, Worksheets("Season 2014-2015").Cells(j, 15).Value, rngFound.Offset(0, 3).Value)
End If
Else
If rngFound.Value = "Michael" Then
Call Send_Email_Using_VBA(rngFound.Offset(0, 2).Value, rngFound.Offset(0, 1).Value, rngFound.Value, Worksheets("Season 2014-2015").Cells(j, 15).Value)
End If
End If
End If
End If
End If
Else
ErrorEmail = ErrorEmail + Worksheets("Season 2014-2015").Cells(j, 1).Value + vbNewLine
End If
Next j
If ErrorEmail <> "" Then
MsgBox ("No Email Found For: " & vbNewLine & ErrorEmail)
End If
End Sub
谢谢
编辑:添加图像以供数据参考:
电子邮件工作表
2014-2015季工作表
答案 0 :(得分:1)
对于此建议的解决方案,您需要更改 getPaid 变量以键入 long 并添加类型的变量(例如 gotPaid )双
Dim getPaid As Long, gotPaid As Double
更改以下代码部分。
Set getPaid = Worksheets("Season 2014-2015").Range("A2:A" & lastSeasonRow).Find(rngFound.Offset(0, -2).Value)
If Not getPaid Is Nothing Then
'If its found
If getPaid.Offset(0, 14).Value <> "" Then
'If they do owe money
Debug.Print getPaid.Offset(0, 14).Value
Else
End If
End If
对此。
With Worksheets("Season 2014-2015")
gotPaid = Application.SumIfs(.Columns("O"), .Columns("A"), rngFound.Offset(0, -2).Value)
getPaid = Application.CountIfs(.Columns("A"), rngFound.Offset(0, -2).Value)
If CBool(getPaid) Then
'If its found
If CBool(gotPaid) Then
'If they do owe money
Debug.Print rngFound.Offset(0, -2).Value & ": " & gotPaid
Else
End If
End If
End With
通过拖拽第二个查找操作,您不会重新定义第一个查找操作,.FindNext
应该继续操作,直到您遇到 CountSwimmers 编号。或者你可以通过不重用相同的变量来做到这一点,但工作表函数应该在这里工作得很好。
由于你的专业助手功能如 DoesItemExist ,这无法测试,但它确实可以编译。