Excel VBA Array question

supercrewed

Board Regular
Joined
Apr 9, 2010
Messages
86
Below is my code to take a range and place onto another sheet. What i'd like to do is to put the range in an array, and then do the calculations in the code, rather than just copying the range from one range to another. The code below works, but i'm relying on the calculations from the worksheet, and i don't want to do this.
My data range is (A1:A175), from the Data sheet. I'm having trouble loading the array, and the accessing it to do the calculations.

Any help to get started would be great, Thanks in advance.

Code:
Option Explicit

Sub DATA_24288368()

Dim i As Integer
Dim r As Range
Dim j As Integer
Dim MinMax, JobNumber, SerialNumber As String
Dim LastRow As Long

Sheets("Inspection_Data").Activate

LastRow = Cells(Rows.Count, "I").End(xlUp).Row + 1
ActiveSheet.Cells(LastRow, 9).Select

Set r = Range("I" & LastRow)

Do
JobNumber = InputBox("Enter Job Number!", "Input Required")
If StrPtr(JobNumber) = 0 Then Exit Sub
Loop While Len(JobNumber) = 0 Or IsNumeric(JobNumber) = False
r.Offset(0, -2) = JobNumber

Do
SerialNumber = InputBox("Enter Serial Number!", "Input Required")
If StrPtr(SerialNumber) = 0 Then Exit Sub
Loop While Len(SerialNumber) = 0 Or IsNumeric(SerialNumber) = False
r.Offset(0, -6) = SerialNumber

For i = 0 To 33
r.Offset(0, i + j) = Sheets("Data").Range("B" & i + 1)
If i = 26 Then j = 4
If i = 28 Then j = 5
If i = 29 Then j = 6
Next

r.Offset(0, 43) = Sheets("Data").Range("B35")
'**************************************************************************************
MsgBox ("Data will be deleted from Data Sheet"), vbCritical, "Data Being Deleted"

Sheets("Data").Activate
Range("A:A").ClearContents


End Sub
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You can easily place a range of cells into an variant array and loop through it to conduct any sort of analysis/parsing you need. You have to keep in mind 2 key points here.
1. They array variable has to be a variant
2. the cell values are stored into a 2-dimensional array. If you try to reference it as a 1-dimensional array, you will get err.number = 9 (Subscript out of range)
Here is a code example of storing range A1:A175 into a variant array and then looping through it.

Code:
Option Explicit


Sub holding()
'Array variable must be a variant data type
Dim vArr() As Variant
Dim i As Long


    'All you need to do is simply set the array to the desired range
    vArr = ActiveSheet.Range("A1:A175")
    
    'The array is 2-dimensional. Thing of the 2nd dimension
    ' as the column number. E.g. vArr(1,1) = cell A1.
    ' If I were to set vArr = ActiveSheet.Range("B2:C175"), then
    ' vArr(1,2) would = the value of range C3.
    For i = LBound(vArr) To UBound(vArr)
        Debug.Print vArr(i, 1)
    Next i
End Sub

Below is my code to take a range and place onto another sheet. What i'd like to do is to put the range in an array, and then do the calculations in the code, rather than just copying the range from one range to another. The code below works, but i'm relying on the calculations from the worksheet, and i don't want to do this.
My data range is (A1:A175), from the Data sheet. I'm having trouble loading the array, and the accessing it to do the calculations.

Any help to get started would be great, Thanks in advance.

Code:
Option Explicit

Sub DATA_24288368()

Dim i As Integer
Dim r As Range
Dim j As Integer
Dim MinMax, JobNumber, SerialNumber As String
Dim LastRow As Long

Sheets("Inspection_Data").Activate

LastRow = Cells(Rows.Count, "I").End(xlUp).Row + 1
ActiveSheet.Cells(LastRow, 9).Select

Set r = Range("I" & LastRow)

Do
JobNumber = InputBox("Enter Job Number!", "Input Required")
If StrPtr(JobNumber) = 0 Then Exit Sub
Loop While Len(JobNumber) = 0 Or IsNumeric(JobNumber) = False
r.Offset(0, -2) = JobNumber

Do
SerialNumber = InputBox("Enter Serial Number!", "Input Required")
If StrPtr(SerialNumber) = 0 Then Exit Sub
Loop While Len(SerialNumber) = 0 Or IsNumeric(SerialNumber) = False
r.Offset(0, -6) = SerialNumber

For i = 0 To 33
r.Offset(0, i + j) = Sheets("Data").Range("B" & i + 1)
If i = 26 Then j = 4
If i = 28 Then j = 5
If i = 29 Then j = 6
Next

r.Offset(0, 43) = Sheets("Data").Range("B35")
'**************************************************************************************
MsgBox ("Data will be deleted from Data Sheet"), vbCritical, "Data Being Deleted"

Sheets("Data").Activate
Range("A:A").ClearContents


End Sub
 
Upvote 0
Thanks for the quick reply, and yes i saw this on the boards. But if i wanted to get the min, and max of two ranges within the range A1:A175, how would i do that, this is the bigger problem for me. Is it possible, just to pull the values into a one dimensional array, and work it the way, but i guess it's the same.
Let's say i need the min, and max of ranges A21:A27 and A142:A148. I'm not sure how to extract the values, combine them, and then get the min, and max from within the array.
I'm thinking

Code:
 r.Offset(0,33)=Application.WorksheetFunction.Min("A21:A27", "A142:A148")

But, as you see above, i feel the only way would be to loop throught arr, somehow to get the values

Your thoughts????
 
Upvote 0
Is this what you're after

Code:
[COLOR=#333333]r.Offset(0,33) = [/COLOR]Application.WorksheetFunction.Min(Range("A21:A27", "A142:A148"))
 
Upvote 0
This will do it, but is there a better way???

Code:
r.offset(0,33) = Application.WorksheetFunction.Min((Arr(21, 1)), Arr(28, 1))
 
Upvote 0
I don't understand, why you don't understand. I want the Min value placed in cell offest(0,33), from a starting cell. Let's not get wrapped up in the part that works, and this does too. I'm looking for a shorter way to pull the values from the array, everything else is good.

if you look at the original post, i know you'll see what i doing.
 
Last edited:
Upvote 0
take a look at these samples, which do what i want!! The million doallr question is, do i have to list out all of the array indexes in order to get the values, or is there a better way???

Code:
answer = WSFunction.Min(([COLOR="#FF0000"]Arr(21, 1)), Arr(28, 1)[/COLOR]) & " - " & WSFunction.Max([COLOR="#FF0000"](Arr(21, 1)), Arr(28, 1)[/COLOR])
 
 answer = WSFunction.Average(([COLOR="#FF0000"]Arr(1, 1)), Arr(2, 1)[/COLOR])
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top