View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Trying to work with User-selected non-contiguous ranges

Hi Tristan,

No idea what your macro is doing but see if this makes sense

Sub test()
Dim Record_a As Variant
Dim rng As Range
Dim ar As Range
Dim rw As Long, cl As Long

Set rng = Selection
For Each ar In rng.Areas
If ar.Count = 1 Then
ReDim Record_a(1 To 1, 1 To 1)
Record_a(1, 1) = ar(1).Value
Else
Record_a = ar.Value
End If

For rw = 1 To UBound(Record_a)
For cl = 1 To UBound(Record_a, 2)
Debug.Print Record_a(rw, cl)
Next
Next
Next
End Sub

Regards,
Peter T

"Tristan" wrote in
message ...

Evening all,

As always what I thought would be a nice bit of mental exercise has
evolved into some monster that delights in my frustration and on-coming
headache:( .

What my little bit of code is trying to do is as follows:

1. The user selects the cells (using the standard Ctrl button and
mouse-click method) which are to be printed in a pre-constructed labels
document in Word

2. The code executes when a control button is activated and registers
the selected cells as an array using the selection object(?) and then
cycles through the array members adding each to its own label in the
document.

Does that make any sense?
Basically I'm trying to turn selected cells into an array and process
them that way.

Well, to my delight:) the code worked perfectly until I tried
selecting just one cell (i.e. one record) to print.
It seems that VBA won't recognise one-selected cell as an array .
Further testing proved that a non-contiguous selection of cells also
won't be recognised as an array .

I'm at a bit of a loss now and its not a facility I can do without.
Any help will be greatly appreciated.

Thanks Tris

Selection of code below:

Private Sub CommandButton1_Click()

Dim Outputs As Variant

Dim i As Integer

Record_a = Selection.Value

Dim Word As Word.Application
Set Word = New Word.Application

With Word
ScreenUpdating = False
Documents.Open Filename:="C:\Documents and
Settings\Label1.doc"
Visible = True

For i = LBound(Record_a) To UBound(Record_a)
With .Selection
TypeText Text:=Record_a(i, 1)
MoveRight unit:=wdCell, Count:=1
End With
Next i

ScreenUpdating = True

End With

Set Word = Nothing
End Sub


--
Tristan
------------------------------------------------------------------------
Tristan's Profile:

http://www.excelforum.com/member.php...o&userid=34061
View this thread: http://www.excelforum.com/showthread...hreadid=541967