Reading array from Spreadsheet Data VBA - arrays

I'm sending a range into an array in VBA. But, when I try to reference parts of the array, I get a "Subscript Out of Range" error. I know that the range is successfully being transferred, because I can then send the array back into a different range.
Dim LastClmn() As Variant 'The last column of brake data
Set RangeSet = ws.Range("RJ2:RJ" & ii)
LastClmn() = RangeSet
Msgbox LastClmn(4)
Referencing a piece of the array is what causes the error

Try LastClmn(4,1)
I found it by using the Locals Window under View in the menu bar.

Related

Can't copy a value from one worksheet over to an array in another worksheet

In the same workbook, I've got two worksheets: Model and Results.
My goal is to copy the value of a cell in Model (for e.g., F8) over to a cell in an array (c4 to I23) in Results called ResultsArray (see code below).
When I run my module, no error appears, but the code doesnt seem to work either (the value of F8 doesnt get copied over to the specified cell in ResultsArray).
Appreciate any help.
Tried running different variations of the code below
Sub CopyTest()
Dim ResultsArray As Variant
ResultsArray = Worksheets("Results").Range("C4:I23")
ResultsArray(1, 1) = Worksheets("Model").Range("F8").Value
End Sub
I'm using ResultsArray(1,1) because I am hoping to introduce a loop into the code to populate cells in the array based on the loop counter, e.g., ResultsArray(loopcounter,1)
So turns out I just needed to add "Set" in the 2nd line before "ResultsArray" when assigning the range from the worksheet "Model" to it:
Sub CopyTest()
Dim ResultsArray As Variant
Set ResultsArray = Worksheets("Results").Range("C4:I23")
ResultsArray(1, 1) = Worksheets("Model").Range("F8").Value
End Sub
I've tested this addition and it works

application defined or object defined error whey copying data into array

I am used to assigning ranges to arrays. However, for some reason right now I am constantly getting an application or object defined error (the first code line below). The second line below works fine. It is identical to the first line except I just copy the range showing all the variables exist.
I have checked in the watch window, arrActuals is Variant/Variant(). Adding .Value at the end of the first line did not solve the error either. Any ideas on why this is happening?
arrActuals = wkbOVHFile.Sheets(szAOPPage).Range(Cells(iStartCopy, IntActOVHCol), Cells(iEndCopy, IntActOVHCol))
wkbOVHFile.Sheets(szAOPPage).Range(Cells(iStartCopy, IntActOVHCol), Cells(iEndCopy, IntActOVHCol)).Copy
Cells without a qualifying worksheet object defaults to the active sheet, so your code fails when wkbOVHFile.Sheets(szAOPPage) is not the active sheet.
More robust like this:
Dim rng As Range
With wkbOVHFile.Sheets(szAOPPage)
Set rng = .Range(.Cells(iStartCopy, IntActOVHCol), _
.Cells(iEndCopy, IntActOVHCol))
End With
arrActuals = rng.Value
rng.Copy

Visio VBA - How can I distribute shapes with a known, fixed distance

I'd like to place all currently selected shapes into an array. I'd then like to sort that array so I can find either the top most or left most shape in the array. I'd then like to use that shape as my starting point, and then from there align the other shapes a fixed, known distance apart. I've tried to place the shapes into an array like so:
Dim numShapes As Integer, i As Integer
Dim arrShapes As Visio.Selection
numShapes = Visio.ActiveWindow.Selection.Count
For i = 1 To numShapes
arrShapes(i) = Visio.ActiveWindow.Selection(i)
Next i
I have tried to create the array with no type specification, specifying as variant, and as in this example as selection. I don't know if I can put them into a list of some kind either? Obviously I can't get to the point of sorting the array and then distributing my shapes until I can get the array to populate. I'm placing a break point in the code and I have the "Locals" window open and I can see that the array is not being populated.
Update:
Why does this work,
Dim Sel As Visio.Selection
Dim Shp As Visio.Shape
Set Sel = Visio.ActiveWindow.Selection
For Each Shp in Sel
Debug.Print Shp.Name
Next
And this does not?
Dim i As Integer
Dim Shp As Visio.Shape
For i = 1 To Visio.ActiveWindow.Selection.Count
Set Shp = Visio.ActiveWindow.Selection(i)
Debug.Print Shp.Name
Next i
Regards,
Scott
There was a couple of problems in your code - fixing only one would not have got you any further in understanding if you had actually fixed anything.
Your arrShapes is declared as a general object - the Selection
Object is one of those objects that is the Jack of all trades, and
master of none.
You didn't "Set" when assigning to the array.
I don't have Visio on this machine, so cannot directly test the code below. I am also assuming that all items selected are shapes (usually a safe assumption in Visio).
Dim numShapes As Integer, i As Integer
Dim arrShapes() As Shape ' Set this up as an array of shape
If Visio.ActiveWindow.Selection.Count > 0 then ' don't want to cause a problem by setting the array to 0!
ReDim arrShapes(Visio.ActiveWindow.Selection.Count)
numShapes = Visio.ActiveWindow.Selection.Count ' while not really necessary it does help explain the code.
For i = 1 To numShapes
' must Set as we want the reference to the shape, not the default value of the shape.
Set arrShapes(i) = Visio.ActiveWindow.Selection(i)
Next i
Else
MsgBox "No shapes selected. Nothing done." ' soft fail
End If

Creating pie chart with array

I am trying to create a Pie chart based on an array (rather than a range). The array is [11,10,1] (I have other code that populates the array).
Dim type_chart As Chart
Dim type_array(2) As Integer
Set type_chart = Charts.Add
type_chart.ChartType = xlPie
type_chart.SeriesCollection(1).Values = type_array
On the last line of the code above, I receive an 'Invalid Parameter' error.
Also, it doesn't have to use an array, but it cannot use a Range.
Your chart needs to work from a range. Find a blank area you can use. Try something like the following:
my_temp_range = "A10:C10"
ActiveSheet.Range(my_temp_range) = type_array
type_chart.SeriesCollection(1).Values = ActiveSheet.Range(my_temp_range)
Once you're working with a temporary range you may not even need the last line (so long as the size of the array doesn't change). You could just set up the chart in advance instead.

Array already dimensioned - Class module

For certain, technical, reasons, we cannot use styles in word. In an effort to speed up applying global properties over and over, I've created a class that can read from a simple xml style-sheet. The sheet contains different "paragraphs." Each paragraph simply stores the paragraph properties that we use the most.
I'm used to C++ where I can use dynamic memory and I'm trying to replicate the behavior of a dynamically allocated array. However, when I attempt to re-dim I get the error message "Array arleady dimensioned."
My research on the MSDN suggests that in order to ReDim the array has to be Global or in the "general declaration context" This makes me think it might simply not be possible to do it in a class.
Excerpt from MSDN:
"You can use ReDim only at procedure level. Therefore, the declaration
context for the variable must be a procedure; it can't be a source
file, a namespace, an interface, a class, a structure, a module, or a
block."
I have attempted to search stack overflow for "Word VBA Array already dimensioned" and went through all 3 pages of results with no avail.
private type pStyle 'Definition removed because it's not needed
private Paragraphs(0) As pStyle 'Initially an empty array of paragraphs
later I have the following function
Public Function AddEmpty()
'Create space
count = count + 1
ReDim Preserve Paragraphs(count)
AddEmpty = count
End Function
Please let me know if any ideas. I would prefer to not have to "estimate" the number of paragraph styles we will need for each style sheet as every file is different.
Private Paragraphs(0) As ...
This is not an empty array, rather it is a fixed length array with 1 element.
For a dynamic array - one you will later redimension - just declare it as:
Private Paragraphs() As ...
Dim numbers(10) As Integer
MsgBox (UBound(numbers))
ReDim numbers(4)
MsgBox (UBound(numbers))
Above code will throw array-already-dimensioned. we can do like
ReDim numbers(10) As Integer
MsgBox (UBound(numbers))
ReDim numbers(4)
MsgBox (UBound(numbers))

Resources