professorj
New Member
- Joined
- May 9, 2015
- Messages
- 1
Hi I have some code that is working as I intended with one minor exception. Upon clicking the userform button the code runs and adds a validation list to a specific column on the worksheets. It also locks all rows on beginning at a specified row down to the last row with data. It locks the desired rows except that it is locking an extra row past the last actual row with data. Any ideas or clues as to the why it is locking that extra row even though the cell with the condition attached to is blank? Below is the code
Private Sub OkButton_Click()
Application.ScreenUpdating = False
Dim Termset As Worksheet
Dim LastRow As Long
LastRow = 5
Dim I As Long
Dim ScheduleA As Workbook
Dim Wksht As Worksheet
Set Wksht = Workbooks(Me.ComboBox1.Value).Worksheets(1)
Dim newsheet As Worksheet
Workbooks(Me.ComboBox1.Value).Activate
Set ScheduleA = Workbooks(Me.ComboBox1.Value)
Set newsheet = Sheets.Add(After:=Sheets(Worksheets.Count), Count:=1, Type:=xlWorksheet)
newsheet.Name = "ActionCodes"
Worksheets("ActionCodes").Range("A1").Value = "A"
Worksheets("ActionCodes").Range("A2").Value = "M"
Worksheets("ActionCodes").Range("A3").Value = "D"
Worksheets("ActionCodes").Range("C1").Value = "A"
Worksheets("ActionCodes").Range("C2").Value = "M"
Worksheets("ActionCodes").Range("C3").Value = "I"
Worksheets("ActionCodes").Range("C4").Value = "D"
Worksheets("ActionCodes").Range("C5").Value = "R"
For Each Termset In ActiveWorkbook.Worksheets
If Termset.Name = "Manufacturer_Catalogue" Then
With Termset.Range("C4:C5000").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=ActionCodes!$C$1:$C$5"
.IgnoreBlank = True
.InCellDropdown = True
End With
ElseIf Termset.Name <> "ActionCodes" Then
With Termset.Range("H6:H5000").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=ActionCodes!$A$1:$A$3"
.IgnoreBlank = True
.InCellDropdown = True
End With
With Termset.Range("N6:N5000").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=ActionCodes!$A$1:$A$3"
.IgnoreBlank = True
.InCellDropdown = True
End With
End If
Next Termset
newsheet.Visible = xlSheetVeryHidden
For Each Termset In ScheduleA.Worksheets
If Termset.Name <> "Manufacturer_Catalogue" And Termset.Name <> "ActionCodes" Then
For I = 5 To LastRow - 1
Next I
With Termset
Do While 1
If Termset.Range("A" & LastRow).Value <> "" Then
LastRow = LastRow + 1
Else
Exit Do
End If
' If last_row = 2000 Then
' MsgBox ("Error in copying rebates")
' End Sub
' End If
Loop
.Cells.Locked = False
.Cells.FormulaHidden = False
.Range("A6:G" & LastRow).Locked = True
.Range("A6:G" & LastRow).FormulaHidden = False
.Range("L6:M" & LastRow).Locked = True
.Range("L6:M" & LastRow).FormulaHidden = False
.Range("Q6:AD" & LastRow).Locked = True
.Range("Q6:AD" & LastRow).FormulaHidden = False
Termset.Protect "567", DrawingObjects:=True, Contents:=True, _
Scenarios:=True, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowSorting:=True, UserInterfaceOnly:=True
End With
ElseIf Termset.Name = "Manufacturer_Catalogue" Then
With Wksht
Do While 1
If Termset.Range("A" & LastRow).Value <> "" Then
LastRow = LastRow + 1
Else
Exit Do
End If
' If last_row = 2000 Then
' MsgBox ("Error in copying rebates")
' End Sub
' End If
Loop
.Cells.Locked = False
.Cells.FormulaHidden = False
.Range("A4:A" & LastRow).Locked = True
.Range("A4:A" & LastRow).FormulaHidden = False
Termset.Protect "567", DrawingObjects:=True, Contents:=True, _
Scenarios:=True, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowSorting:=True, UserInterfaceOnly:=True
End With
End If
Next Termset
Application.ScreenUpdating = True
Unload Me
MsgBox ("Termsets Locked")
End Sub
Private Sub OkButton_Click()
Application.ScreenUpdating = False
Dim Termset As Worksheet
Dim LastRow As Long
LastRow = 5
Dim I As Long
Dim ScheduleA As Workbook
Dim Wksht As Worksheet
Set Wksht = Workbooks(Me.ComboBox1.Value).Worksheets(1)
Dim newsheet As Worksheet
Workbooks(Me.ComboBox1.Value).Activate
Set ScheduleA = Workbooks(Me.ComboBox1.Value)
Set newsheet = Sheets.Add(After:=Sheets(Worksheets.Count), Count:=1, Type:=xlWorksheet)
newsheet.Name = "ActionCodes"
Worksheets("ActionCodes").Range("A1").Value = "A"
Worksheets("ActionCodes").Range("A2").Value = "M"
Worksheets("ActionCodes").Range("A3").Value = "D"
Worksheets("ActionCodes").Range("C1").Value = "A"
Worksheets("ActionCodes").Range("C2").Value = "M"
Worksheets("ActionCodes").Range("C3").Value = "I"
Worksheets("ActionCodes").Range("C4").Value = "D"
Worksheets("ActionCodes").Range("C5").Value = "R"
For Each Termset In ActiveWorkbook.Worksheets
If Termset.Name = "Manufacturer_Catalogue" Then
With Termset.Range("C4:C5000").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=ActionCodes!$C$1:$C$5"
.IgnoreBlank = True
.InCellDropdown = True
End With
ElseIf Termset.Name <> "ActionCodes" Then
With Termset.Range("H6:H5000").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=ActionCodes!$A$1:$A$3"
.IgnoreBlank = True
.InCellDropdown = True
End With
With Termset.Range("N6:N5000").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=ActionCodes!$A$1:$A$3"
.IgnoreBlank = True
.InCellDropdown = True
End With
End If
Next Termset
newsheet.Visible = xlSheetVeryHidden
For Each Termset In ScheduleA.Worksheets
If Termset.Name <> "Manufacturer_Catalogue" And Termset.Name <> "ActionCodes" Then
For I = 5 To LastRow - 1
Next I
With Termset
Do While 1
If Termset.Range("A" & LastRow).Value <> "" Then
LastRow = LastRow + 1
Else
Exit Do
End If
' If last_row = 2000 Then
' MsgBox ("Error in copying rebates")
' End Sub
' End If
Loop
.Cells.Locked = False
.Cells.FormulaHidden = False
.Range("A6:G" & LastRow).Locked = True
.Range("A6:G" & LastRow).FormulaHidden = False
.Range("L6:M" & LastRow).Locked = True
.Range("L6:M" & LastRow).FormulaHidden = False
.Range("Q6:AD" & LastRow).Locked = True
.Range("Q6:AD" & LastRow).FormulaHidden = False
Termset.Protect "567", DrawingObjects:=True, Contents:=True, _
Scenarios:=True, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowSorting:=True, UserInterfaceOnly:=True
End With
ElseIf Termset.Name = "Manufacturer_Catalogue" Then
With Wksht
Do While 1
If Termset.Range("A" & LastRow).Value <> "" Then
LastRow = LastRow + 1
Else
Exit Do
End If
' If last_row = 2000 Then
' MsgBox ("Error in copying rebates")
' End Sub
' End If
Loop
.Cells.Locked = False
.Cells.FormulaHidden = False
.Range("A4:A" & LastRow).Locked = True
.Range("A4:A" & LastRow).FormulaHidden = False
Termset.Protect "567", DrawingObjects:=True, Contents:=True, _
Scenarios:=True, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowSorting:=True, UserInterfaceOnly:=True
End With
End If
Next Termset
Application.ScreenUpdating = True
Unload Me
MsgBox ("Termsets Locked")
End Sub