View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Doug Glancy Doug Glancy is offline
external usenet poster
 
Posts: 770
Default Improve code that checks a range of names - currently using Activate

I have a spreadsheet with some hidden helper columns. One of these columns
is titled "Delete Row Allowed" and contains TRUE or a blank. True means
that the yes, the row can be deleted. In the spreadsheet, I've defined a
name, "boolDeleteItemAllowed" which refers to "=MySheet!$G1". Thus, the
name always tells whether the currently selected row can be deleted.

In my code I've got the following function, which checks whether row
deletion is allowed. It checks all the rows in the current selection. It
activates one cell in each row to do this. I haven't used Activate in code
for some time, but I'm not sure how to get the relative reference in each
row without it.

Function DeleteRowAllowed() As Boolean
Dim cell As Range
Dim rngActiveCell As Range

DeleteRowAllowed = True
With Workbooks("MyBook.xls").ActiveSheet
'need to set this back at end
Set rngActiveCell = ActiveCell
'check one cell in each row
For Each cell In Selection.Columns(1).Cells
cell.Activate
If CBool(.Names("boolDeleteRowAllowed").RefersToRange .Value) = False
Then
DeleteRowAllowed = False
Exit Function
End If
Next cell
'set back to original ActiveCell
rngActiveCell.Activate
End With
End Function

I don't think it's a big deal in terms of efficiency. As soon as it hits a
blank row it exits and I'll add ScreenUpdating = False. But I am curious if
there's a way to do it without Activate and still keeping the defined names
in the worksheet.

Thanks in advance,

Doug