View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default How does this code know which sheet to act upon?

If you're going to use the with/end with structure, then you don't want this:

With Sheets("217")
For Each myCell In Worksheets("217").Range("A2:AB2")
If myCell.Locked = False Then
myCell.ClearContents
End If
Next myCell
End With

You'd want:

With Sheets("217")
For Each myCell In .Range("A2:AB2")
If myCell.Locked = False Then
myCell.ClearContents
End If
Next myCell
End With

Notice the dot in front of the .range("A2:AB2"). This means that this belongs
to the object in the previous With statement (sheets("217") in your example).

You could have skipped the with/end with, too and used:

For Each myCell In Worksheets("217").Range("A2:AB2")
If myCell.Locked = False Then
myCell.ClearContents
End If
Next myCell

You've got that range("A2:AB2") qualified with the preceding Worksheets("217").

ps. If you have problems with those merged cells, you may want to change:

mycell.clearcontents
to
mycell.value = ""




dan dungan wrote:

I used the following approach which seems to be working.
Private Sub cmdNextPartNum_Click()
'Clears the unlocked cells in Sheets "217" and "A&E,
'range A2:AB2,including merged cells to prepare for next quote
Dim myCell As Range
Application.EnableEvents = False

With Sheets("217")
For Each myCell In Worksheets("217").Range("A2:AB2")
If myCell.Locked = False Then
myCell.ClearContents
End If
Next myCell
End With

With Sheets("A&E")
For Each myCell In Worksheets("A&E").Range("A2:AB2")
If myCell.Locked = False Then
myCell.ClearContents
End If
Next myCell
End With

Worksheets("QuotedPart").Activate
Selection.End(xlUp).Select
CmdNextPartNum.Visible = False
cmdGetPrice.Visible = True
Range("A2:C2").Select
Range("A2:C2").ClearContents

Application.EnableEvents = True
End Sub


--

Dave Peterson