Getting to Know VBA
New Member
- Joined
- Dec 20, 2013
- Messages
- 29
Hi,
I've set about identifying VBA code to achieve the following two objectives:
1) Auto-protect all worksheets in a workbook upon close of file; and
2) Insertion of rows.
Having located VBA code on the forum for both, and created macros, I find that while both macros work fine independent of one another, they unfortunately do not operate together as required. More specifically, when I attempt to insert a row (I have created a command button to execute the macro for insertion of rows) I receive the following error message "Run-time error ' 1004': Insert method of Range class failed".
The VBA code I am using for the auto-protect function is:
The VBA code I am using for the insert rows function is:
Your assistance in resolving this problem is much appreciated.</markhill@charm.net.nospam>
I've set about identifying VBA code to achieve the following two objectives:
1) Auto-protect all worksheets in a workbook upon close of file; and
2) Insertion of rows.
Having located VBA code on the forum for both, and created macros, I find that while both macros work fine independent of one another, they unfortunately do not operate together as required. More specifically, when I attempt to insert a row (I have created a command button to execute the macro for insertion of rows) I receive the following error message "Run-time error ' 1004': Insert method of Range class failed".
The VBA code I am using for the auto-protect function is:
Code:
Option Explicit
' Change the password "pass" to your password (include the apostrophes)
Sub Auto_Close()
Dim w As Worksheet
For Each w In ThisWorkbook.Worksheets
w.Protect Password:="pass"
Next
End Sub
The VBA code I am using for the insert rows function is:
Code:
Option Explicit
Sub InsertRowsAndFillFormulas_caller()
'-- this macro shows on Tools, Macro..., Macros (Alt+F8) dialog
Call InsertRowsAndFillFormulas
End Sub
Sub InsertRowsAndFillFormulas(Optional vRows As Long = 0)
' Documented: http://www.mvps.org/dmcritchie/excel/insrtrow.htm
' Re: Insert Rows -- 1997/09/24 Mark Hill <markhill@charm.net.nospam>
' row selection based on active cell -- rev. 2000-09-02 David McRitchie
Dim x As Long
ActiveCell.EntireRow.Select 'So you do not have to preselect entire row
If vRows = 0 Then
vRows = Application.InputBox(prompt:= _
"How many rows do you want to add?", Title:="Add Rows", _
Default:=1, Type:=1) 'Default for 1 row, type 1 is number
If vRows = False Then Exit Sub
End If
'if you just want to add cells and not entire rows
'then delete ".EntireRow" in the following line
'rev. 2001-01-17 Gary L. Brown, programming, Grouped sheets
Dim sht As Worksheet, shts() As String, i As Long
ReDim shts(1 To Worksheets.Application.ActiveWorkbook. _
Windows(1).SelectedSheets.Count)
i = 0
For Each sht In _
Application.ActiveWorkbook.Windows(1).SelectedSheets
Sheets(sht.Name).Select
i = i + 1
shts(i) = sht.Name
x = Sheets(sht.Name).UsedRange.Rows.Count 'lastcell fixup
Selection.Resize(rowsize:=2).Rows(2).EntireRow. _
Resize(rowsize:=vRows).Insert Shift:=xlDown
Selection.AutoFill Selection.Resize( _
rowsize:=vRows + 1), xlFillDefault
On Error Resume Next 'to handle no constants in range -- John McKee 2000/02/01
' to remove the non-formulas -- 1998/03/11 Bill Manville
Selection.Offset(1).Resize(vRows).EntireRow. _
SpecialCells(xlConstants).ClearContents
Next sht
Worksheets(shts).Select
End Sub
Your assistance in resolving this problem is much appreciated.</markhill@charm.net.nospam>