Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default optimize coding on named range

is there a better way to do this?

Range("second_column").Select
Do While Not ActiveCell.Borders(xlEdgeBottom).Weight = xlMedium And Not
ActiveCell.Borders(xlEdgeBottom).LineStyle = xlContinuous
ActiveCell.Offset(1, 0).Select
Loop
Range(ActiveCell, Range("second_column")).Name= "second_column"

please help how to do this better..
in my named range I don't know how to get its column to check
for a specific cell outside(below) of the range

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default optimize coding on named range

One way -

Sub test()
Dim bGotCel As Boolean
Dim rwFirst As Long, rwLast As Long
Dim r As Long, c As Long
With Range("second_column")
rwFirst = .Row
c = .Column
End With

rwLast = Cells.SpecialCells(xlLastCell).Row

For r = rwFirst To rwLast
With Cells(r, c).Borders(xlEdgeBottom)
If .Weight = xlMedium And .LineStyle = xlContinuous Then
bGotCel = True
Exit For
End If
End With
Next

If bGotCel Then
Range(Cells(r, c), Range("second_column")).Name = "second_column"
Else
MsgBox "xlEdgeBottom xlMedium xlContinuous not found"
End If

Range("second_column").Select ' only if sure on active sheet

End Sub

Assumes "second_column" starts as a single named cell and hopefully a cell
with the border formats you are looking for exists somewhere below in same
column.

Regards

"Dantz" wrote in message
ups.com...
is there a better way to do this?

Range("second_column").Select
Do While Not ActiveCell.Borders(xlEdgeBottom).Weight = xlMedium And Not
ActiveCell.Borders(xlEdgeBottom).LineStyle = xlContinuous
ActiveCell.Offset(1, 0).Select
Loop
Range(ActiveCell, Range("second_column")).Name= "second_column"

please help how to do this better..
in my named range I don't know how to get its column to check
for a specific cell outside(below) of the range



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default optimize coding on named range

thnks a lot...but what if i don't know the specific column...
let's say my "second_column" has a range from A5:A10,D5:D10,F5:F10..

is there a way I can loop on its columns?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default optimize coding on named range

You have substantially changed what you originally asked for but the below
should cater for your example even with specified border formats in
different rows.

I have to say I hesitate posting this. I think the idea is flawed, prone to
things going wrong. But as long as you are aware both of what you are doing
as regards the name and border formats and exactly what the code does.

If all you want to do is expand the named range down all you need to do is
ensure you insert new rows within the named range. The named range will
automatically expand by itself.

Sub test2()
Dim bGotCel As Boolean
Dim rwFirst As Long, rwLast As Long
Dim r As Long, c As Long
Dim ra As Range
Dim rng As Range
Dim rNew As Range

'uncomment next line after testing.
'Format cells below row10 with xlEdgeBottom with xlContinuous/xlMedium
Range("A5:A10,D5:D10,F5:F10").Name = "second_column"

Set rng = Range("second_column")

For Each ra In rng.Areas

With ra
rwFirst = .Row
c = .Column
End With

rwLast = Cells.SpecialCells(xlLastCell).Row
bGotCel = False
For r = rwFirst To rwLast
With Cells(r, c).Borders(xlEdgeBottom)
If .Weight = xlMedium And .LineStyle = xlContinuous Then
bGotCel = True
Exit For
End If
End With
Next

If bGotCel Then
If rNew Is Nothing Then
Set rNew = Range(Cells(rwFirst, c), Cells(r, c))
Else
Set rNew = Union(rNew, Range(Cells(r, c), _
Range(Cells(rwFirst, c), Cells(r, c))))
End If

Else
MsgBox "xlEdgeBottom xlMedium xlContinuous not found"
Set rNew = Nothing
Exit For
End If
Next

If Not rNew Is Nothing Then
rNew.Name = "second_column"
rNew.Select
End If

End Sub

Regards,
Peter T

"Dantz" wrote in message
oups.com...
thnks a lot...but what if i don't know the specific column...
let's say my "second_column" has a range from A5:A10,D5:D10,F5:F10..

is there a way I can loop on its columns?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default optimize coding on named range

thanks a lot..the first code that you gave worked out just fine
and the second one gave me another idea...

by the way...
this is out of the title but...is there a way how can i detect if the
user presses
Enter key in the cell? I want to save the input of the user after
pressing the
enter key...



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default optimize coding on named range

Not directly but look into the Worksheet_Change event, zillions of examples
in this ng

Rt-click a sheet tab, view code and paste following into the worksheet
module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox Target(1).Value
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target(1).Value
End Sub

Regards,
Peter T

"Dantz" wrote in message
oups.com...
thanks a lot..the first code that you gave worked out just fine
and the second one gave me another idea...

by the way...
this is out of the title but...is there a way how can i detect if the
user presses
Enter key in the cell? I want to save the input of the user after
pressing the
enter key...



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default optimize coding on named range

instead of

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox Target(1).Value
End Sub


I meant

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox Target.address
End Sub

but probably just the Worksheet_Change event is all you need.

Regards,
Peter T

"Peter T" <peter_t@discussions wrote in message
...
Not directly but look into the Worksheet_Change event, zillions of

examples
in this ng

Rt-click a sheet tab, view code and paste following into the worksheet
module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox Target(1).Value
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target(1).Value
End Sub

Regards,
Peter T

"Dantz" wrote in message
oups.com...
thanks a lot..the first code that you gave worked out just fine
and the second one gave me another idea...

by the way...
this is out of the title but...is there a way how can i detect if the
user presses
Enter key in the cell? I want to save the input of the user after
pressing the
enter key...





  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default optimize coding on named range

thanks a lot Peter..

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to optimize? large non-contig cell range for data validation l ker_01 Excel Discussion (Misc queries) 5 May 1st 09 11:05 AM
Referencing a named range based upon Range name entry in cell Barb Reinhardt Excel Worksheet Functions 14 June 20th 07 07:19 PM
Array as a "named range" - formula ok in cells, but error as "named range" tskogstrom Excel Discussion (Misc queries) 11 December 28th 06 04:44 PM
inserting a named range into new cells based on a named cell Peter S. Excel Discussion (Misc queries) 1 June 4th 06 03:53 AM
If any cell in named range = 8 then shade named range JJ[_8_] Excel Programming 3 August 26th 05 11:09 PM


All times are GMT +1. The time now is 02:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"