E
E
Evloshevsky Nikolay2019-01-29 16:04:56
Visual Basic
Evloshevsky Nikolay, 2019-01-29 16:04:56

How to create an array from selected cells in VB?

There are 2 columns in which the coordinates of the points are located - X and Y. How to write a macro that translates the selected range into a string: X1, Y1; X2, Y2; The number of coordinates is not constant. I did a check for the closure of the contour, the coincidence of the coordinates of the first and last points.
I think, in a cycle, fill a one-dimensional array and, depending on the serial number of the next element, put "," or ";" when concatenating.
Help how to fill an array in VB from selected cells
5c504f8689d00636044187.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pychev Anatoly, 2019-01-29
@n1k_crimea

or so
select the range and run the macro, you can assign it to the button
, it will ask where to put the result

Sub test()
   Dim rng As Range
   Dim v, vr, vc As Variant
   Dim i&, j As Long
   Dim sRes As String
   
   Set rng = Selection
   
   v = rng
   
   ReDim vc(LBound(v) To UBound(v))
   For i = LBound(v) To UBound(v)
      ReDim vr(LBound(v, 2) To UBound(v, 2))
      For j = LBound(v, 2) To UBound(v, 2)
         vr(j) = v(i, j)
      Next j
      vc(i) = Join(vr, ",")
   Next i
   
   sRes = Join(vc, ";")
   
   Set rng = Application.InputBox("Выберите ячейку", "Object", Type:=8)

   rng = sRes
   
End Sub

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question