我在Excel VBA中有一個宏給我一個錯誤。我在啓動Excel後第一次運行它,並且它運行完美,但是當我嘗試運行它時,它隨後出現錯誤nameRow = nameRange.Row
,給出錯誤Object not set
,我對此感到困惑。「對象未設置」錯誤
Sub AssignGroups()
Dim membership As Worksheet
Dim wb As Workbook
Dim groups As Worksheet
Dim nameRow As Long
Dim fullNameString As String
Dim nameRange As Range
Dim groupRange As Range
Dim nameRange2 As Range
Dim nameIndex As Long
Dim userNameString As String
Dim barIndex As Long
Set wb = ActiveWorkbook
Set membership = Sheets("User Group Membership")
Set groups = Sheets("User Assigned to Groups")
membership.Activate
Set nameRange = membership.Range("A:A").Find("user -name")
nameRow = nameRange.Row
fullNameString = membership.Cells(nameRow, "A").Value
nameIndex = InStr(fullNameString, "user -name")
barIndex = InStr(fullNameString, "|")
userNameString = Mid(fullNameString, nameIndex + 12, ((barIndex - 4) - (nameIndex + 12)))
groups.Activate
Set nameRange2 = groups.Range("A:CH").Find(userNameString)
nameColumn = nameRange2.Column
membership.Activate
membership.Cells(nameRow, "A").Activate
Do
ActiveCell.Offset(1).Activate
If Not IsEmpty(ActiveCell.Value) Then
cellValue = ActiveCell.Value
groups.Activate
Set groupRange = groups.Range("A:CH").Find(cellValue, , , lookat:=xlWhole)
groupRow = groupRange.Row
groups.Cells(groupRow, nameColumn).Activate
ActiveCell.Value = "X"
membership.Activate
End If
Loop Until IsEmpty(ActiveCell.Value)
End Sub
難道是因爲我引用ActiveCell
的方式嗎?
這可能是'.Find(...)'在前面的線方法沒有找到任何東西。在這種情況下,'nameRange'將被設置爲'Nothing'。你可以添加一些代碼來測試/處理它。 – xidgel
不是,這很可能是因爲你的'.Find'方法沒有找到結果並返回'Nothing',所以你有錯誤!在你的'Set nameRange','MsgBox membership.Range(「A:A」)。Find(「user -name」)是Nothing'之前加上這行,如果它顯示'True',find方法找不到任何東西,如果它是'假'這個問題在別的地方! ;) – R3uK