我試圖從一組工作簿和工作表中提取所有標題字段及其註釋。我試圖找到所有鎖定的單元格,而不是空的,並沒有計算。我已經把這段代碼放在一起,但是它在cell.comment.text
行上拋出一個錯誤。它回來與錯誤:循環遍歷所有打開的工作簿和工作表中的單元格,並獲得註釋
Run-time error '91': Object Variable or With block variable not set
Sub extract()
Dim WB As Workbook
Dim ws As Worksheet: Dim Db As Worksheet
Dim NoRow As Integer: Dim i As Integer: Dim j As Integer
Dim cell
' On Error GoTo extract_Error
Set Db = ThisWorkbook.Sheets("Data")
With Application
.ScreenUpdating = False
End With
For Each WB In Application.Workbooks
If Not WB.Name = ThisWorkbook.Name Then
For Each ws In WB.Sheets
i = Db.Cells(Db.Rows.Count, 1).End(xlUp).Row
For Each cell In ws.UsedRange.Cells
If cell.Locked = True And IsEmpty(cell) = False And cell.HasFormula = False Then
i = i + 1
Db.Cells(i, 1) = WB.Name
Db.Cells(i, 2) = ws.Name
Db.Cells(i, 3) = cell.Value
Db.Cells(i, 4) = cell.Comment.Text
End If
Next cell
Next ws
End If
Next WB
With Application
.ScreenUpdating = True
End With
On Error GoTo 0
Exit Sub
extract_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure extract of Module Module1"
End Sub
擁有它應該返回一個空字符串值
試過了。它運行但不提取註釋 – Tom
和'Db.Cells(i,4)'不爲空? 「Debug.Print cell.Comment.Text」返回什麼? –
我剛剛通過在單元格中添加註釋('SHIFT' +'F2')並下降到立即窗口範圍(「A1」)進行測試。Comment.Text'正常工作... –