Problem with Array

clynch28

New Member
Joined
Sep 21, 2017
Messages
19
I'm having a problem with using a loop to assign cell values to an array variable. The values are only running for the first i and the msg box is only saying the first two instead of the four that it should find. Any help would be greatly appreciated.

Type Plants
Name As String
Capacity As Integer
End Type


Type Warehouses
Name As String
Demand As Integer
End Type


Sub plantInfo()


Dim Plant() As Plants
Dim nPlants As Integer
Dim i As Integer
Dim msg As String


With Range("A3")
nPlants = Range(.Offset(1, 0), .End(xlDown).Offset(-1, 0)).Rows.Count
ReDim Plant(1 To nPlants)
For i = 1 To nPlants
Plant(i).Name = .Offset(i, 0).Value
Plant(i).Capacity = .Offset(i, 6).Value
msg = msg & "production from " & Plant(i).Name & " cannot exceed its capacity of " & Plant(1).Capacity & "." & vbLf
Next i
End With


MsgBox msg




End Sub



 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
It works for me (Ref columns "A & G"), except The line below has a Plant(1). instead of an Plant(i). in it
Code:
 msg = msg & "production from " & Plant(i).Name & " cannot exceed _
 its capacity of " & [COLOR=#ff0000][SIZE=5]Plant(i).[/SIZE][/COLOR]Capacity & "." & vbLf
 
Last edited:
Upvote 0
The issue now though is that it only counts two out of the four rows in the table. Any ideas as to why?
 
Upvote 0
Your code line for "nplants" will only count the rows down to the first blank row.
Perhaps the line below would be better!!!
Code:
nPlants = Range("A" & Rows.Count).End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
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