craigexcel
Active Member
- Joined
- Jun 28, 2006
- Messages
- 298
- Office Version
- 2016
- Platform
- Windows
Hoping you can help troubleshoot what part of my code is causing it to hiccup. This procedure will be called from another, and is intended to make identical changes (i.e. add several Comment boxes) on various sheets within each workbook in the user-selected network folder.
It has run successfully (@ 1st pass) on a test collection of .xlsm workbooks (each of which has both an "Open" and "BeforeSave" macro). However, variously, running the code on subsequent instances causes it to fail at the line in red.
The calling macro requests the network folder via "Application.FileDialog(msoFileDialogFolderPicker)", and targets only .xlsm workbooks. Each of the target workbooks has an "Open" and "BeforeSave" macro, which also removes password protection (workbook and worksheets) only for defined users.
I thought the "ws.Range.." syntax was correct. Is that not true? Or is it possible that's not even the true source of the error that Excel is encountering, and it's really something else? Thoughts?
It has run successfully (@ 1st pass) on a test collection of .xlsm workbooks (each of which has both an "Open" and "BeforeSave" macro). However, variously, running the code on subsequent instances causes it to fail at the line in red.
The calling macro requests the network folder via "Application.FileDialog(msoFileDialogFolderPicker)", and targets only .xlsm workbooks. Each of the target workbooks has an "Open" and "BeforeSave" macro, which also removes password protection (workbook and worksheets) only for defined users.
I thought the "ws.Range.." syntax was correct. Is that not true? Or is it possible that's not even the true source of the error that Excel is encountering, and it's really something else? Thoughts?
Code:
Dim ws As Worksheet
For Each ws In Worksheets
If Len(ws.Name) = 5 Then
If ws.Name Like "[A-Z][A-Z][A-Z]##" Then 'if sheet name matches the pattern [AAA##], i.e 3 UPPER CASE alpha, followed by 2 numeric
Range("F1").ClearComments 'clear any existing Comment in cell
[COLOR=#ff0000]With ws.Range("F1").AddComment [/COLOR]'add new Comment
.Visible = False
.text Date & ": formula explanation: sadfkms slkdfm salkglsa kj glsakf gsalfkgsa lfkgjsl as dfkgjslakj gas;fkgj"
End With
With ws.Range("F1").Comment.Shape 'format Comment box size
Dim lArea As Long
lArea = .Width * .Height
.Width = 120
.Height = (lArea / .Width)
.TextFrame.AutoMargins = False
.TextFrame.MarginBottom = 1
.TextFrame.MarginTop = 1
.TextFrame.MarginLeft = 1
.TextFrame.MarginRight = 1
End With
Else
End If
Else
End If
Next ws
Last edited: