Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Changing a named ranged based on array size on a form

I've created a form with a list box that is populated from a named
ranged on a spreadsheet. I am allowing the users to add/delete/reorder
the values in the listbox.

My problem is when I write the listbox values back to the named range.
If my listbox has fewer items than the original range then the old
values that were not overwritten are still in the range. The ideal
solution would be to clear the range then re-define the range limits
before I write back the values. I can't seem to find a way to change
named range's cell range definition via VB.

Here's what I've attempted

Form load - I load the named range to the list box on the form. Assume
7 items were loaded

Private sub UserForm_Initialize()
For each e in Range("Setup!Current_State_Column_Names")
if e.value < "" then
setup_form.cs_columns_list.AddItem e.value
end if
end for
End sub

After the user completes their edits (all working) I attempt to write
back/redefine the named range

Dim CellsDown as Integer
Dim i as long, j as integer, x as integer, itemnum as integer
Dim TempArray() as string
Dim Current_State_Column_Names as Range

If cs_columns_list.listIndex = cs_columns_list.ListCount -1 then exit
sub

CellsDown = cs_columns_list.listcount ' 5 items are in the list box
for example

set Current_State_Column_Names = Range(Cells(1,1), Cells(CellsDown, 1))

' Fill array with listbox items

x = 0
for i = 1 to CellsDown
for j = 1 to 1
TempArray(i,j) = cs_columns_list(x)
x = x + 1
next j
next i

' Now attempt to write the array back to the named ranged

Current_state_column_names.Value = TempArray

' The works except the two extra values in the original named range are
still there. Obviously I could write a loop to clear the values from
the names range before I write the new values but it seems like it
would be easier to be able to redefine the named range in the
spreadsheet based on the number of items in my new list. I currently
have the range extended a lot longer than I need simply to accomodate
the fact I can't seem to redefine the range.

  #2   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Changing a named ranged based on array size on a form

Try changing the RefersTo property of your named range. I played around with
it a little, and maybe this will give you some ideas. Using a range object
and a name object, you'll note they refer to the same range at the beginning
of the macro, but different ranges at the end of the macro by changing what
the named range refers to.

Sub test()
Dim rngTest As Range
Dim nmeTest As Name

With ThisWorkbook
Set nmeTest = .Names("MyRange")
With nmeTest
Set rngTest = .RefersToRange
End With
End With

MsgBox rngTest.Address
MsgBox nmeTest.RefersTo

nmeTest.RefersTo = Sheet1.Range("A1:A5")
MsgBox rngTest.Address
MsgBox nmeTest.RefersTo

End Sub


"Wanderer" wrote:

I've created a form with a list box that is populated from a named
ranged on a spreadsheet. I am allowing the users to add/delete/reorder
the values in the listbox.

My problem is when I write the listbox values back to the named range.
If my listbox has fewer items than the original range then the old
values that were not overwritten are still in the range. The ideal
solution would be to clear the range then re-define the range limits
before I write back the values. I can't seem to find a way to change
named range's cell range definition via VB.

Here's what I've attempted

Form load - I load the named range to the list box on the form. Assume
7 items were loaded

Private sub UserForm_Initialize()
For each e in Range("Setup!Current_State_Column_Names")
if e.value < "" then
setup_form.cs_columns_list.AddItem e.value
end if
end for
End sub

After the user completes their edits (all working) I attempt to write
back/redefine the named range

Dim CellsDown as Integer
Dim i as long, j as integer, x as integer, itemnum as integer
Dim TempArray() as string
Dim Current_State_Column_Names as Range

If cs_columns_list.listIndex = cs_columns_list.ListCount -1 then exit
sub

CellsDown = cs_columns_list.listcount ' 5 items are in the list box
for example

set Current_State_Column_Names = Range(Cells(1,1), Cells(CellsDown, 1))

' Fill array with listbox items

x = 0
for i = 1 to CellsDown
for j = 1 to 1
TempArray(i,j) = cs_columns_list(x)
x = x + 1
next j
next i

' Now attempt to write the array back to the named ranged

Current_state_column_names.Value = TempArray

' The works except the two extra values in the original named range are
still there. Obviously I could write a loop to clear the values from
the names range before I write the new values but it seems like it
would be easier to be able to redefine the named range in the
spreadsheet based on the number of items in my new list. I currently
have the range extended a lot longer than I need simply to accomodate
the fact I can't seem to redefine the range.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Changing a named ranged based on array size on a form

That worked like a charm...thank you very much!

JMB wrote:
Try changing the RefersTo property of your named range. I played around with
it a little, and maybe this will give you some ideas. Using a range object
and a name object, you'll note they refer to the same range at the beginning
of the macro, but different ranges at the end of the macro by changing what
the named range refers to.

Sub test()
Dim rngTest As Range
Dim nmeTest As Name

With ThisWorkbook
Set nmeTest = .Names("MyRange")
With nmeTest
Set rngTest = .RefersToRange
End With
End With

MsgBox rngTest.Address
MsgBox nmeTest.RefersTo

nmeTest.RefersTo = Sheet1.Range("A1:A5")
MsgBox rngTest.Address
MsgBox nmeTest.RefersTo

End Sub


"Wanderer" wrote:

I've created a form with a list box that is populated from a named
ranged on a spreadsheet. I am allowing the users to add/delete/reorder
the values in the listbox.

My problem is when I write the listbox values back to the named range.
If my listbox has fewer items than the original range then the old
values that were not overwritten are still in the range. The ideal
solution would be to clear the range then re-define the range limits
before I write back the values. I can't seem to find a way to change
named range's cell range definition via VB.

Here's what I've attempted

Form load - I load the named range to the list box on the form. Assume
7 items were loaded

Private sub UserForm_Initialize()
For each e in Range("Setup!Current_State_Column_Names")
if e.value < "" then
setup_form.cs_columns_list.AddItem e.value
end if
end for
End sub

After the user completes their edits (all working) I attempt to write
back/redefine the named range

Dim CellsDown as Integer
Dim i as long, j as integer, x as integer, itemnum as integer
Dim TempArray() as string
Dim Current_State_Column_Names as Range

If cs_columns_list.listIndex = cs_columns_list.ListCount -1 then exit
sub

CellsDown = cs_columns_list.listcount ' 5 items are in the list box
for example

set Current_State_Column_Names = Range(Cells(1,1), Cells(CellsDown, 1))

' Fill array with listbox items

x = 0
for i = 1 to CellsDown
for j = 1 to 1
TempArray(i,j) = cs_columns_list(x)
x = x + 1
next j
next i

' Now attempt to write the array back to the named ranged

Current_state_column_names.Value = TempArray

' The works except the two extra values in the original named range are
still there. Obviously I could write a loop to clear the values from
the names range before I write the new values but it seems like it
would be easier to be able to redefine the named range in the
spreadsheet based on the number of items in my new list. I currently
have the range extended a lot longer than I need simply to accomodate
the fact I can't seem to redefine the range.



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
Automatically Adding Time based on a name ranged Cathy Excel Discussion (Misc queries) 1 February 27th 08 02:13 AM
Named ranged selection problem Martin B Excel Worksheet Functions 3 January 4th 08 09:32 PM
Vlookup - Using a named ranged for col_index_num [email protected] Excel Discussion (Misc queries) 2 June 1st 05 05:38 PM
How to run code based on a dropdown form changing? PropKid[_2_] Excel Programming 1 October 22nd 04 11:15 PM
How to run code based on a dropdown form changing? PropKid Excel Programming 1 October 21st 04 11:49 PM


All times are GMT +1. The time now is 05:59 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"