last day (15 days later) » 

7:04 AM
1
A: Adding multiple columns of of multiple row values into an array of custom type

Tim WilliamsSomething like this should work: Public CEEStudent() As Student '***** MyType as an array Public Sub fillCEEStudent() Dim j As Long, i As Long ReDim CEEStudent(20) With ThisWorkbook.Sheets("Question1") j = 0 For i = 3 To 22 'might need to tweak the col...

 
I used your code, with some minor adjustments (See above) and it compiles and my styling effects work, but it is not returning any values for the corresponding student. It is returning a 0 for score and for gpa...
 
You did not use my code really - there are a number of changes in your posted update. Please take a few minutes to read up on using With
 
that is where I was focusing too, the WITH changes everything, especially in a scope block that refers to more than one object, I posted above to take them out and code it Explicit until its working correctly. Example: ThisWorkbook.Worksheets("Question2").Cells(i,"A") = ThisWorkbook.Worksheets("Question1").Cells(i,"A").Value as one instance you can use the address, the value, the string, etc, etc Just get it working and decide if those WITH are really your friend and if you have multiple workbooks, watch your scope (function scope, etc.)
 
The With scope only applies where an object is given a leading period (.), so you have complete control over how that scope is applied.
 
I am now getting the values returned for everything correctly. I just cant get them to pass the info back to the userform, where the gpa and final score needs to be displayed.... When I am in debug mode , i set the cursor right after the End Sub statement and it tells me that the value of j is 21 and the value of i is 23, both of those values are 1 higher than i need and when i hover over the variables and stuff, i get an out of range error.
It appears to be an issue with i and j not stopping when they should. I tried adding a do while loop directly after the i = 3 to 22 line and excel did not like that at all... crashed and would not reopen that workbook... had to repaste everything and remove it first....
Ok, trying to clean this up a bit... confusing otherwise.... The current code in the original post is the most up to date... when i enter debug mode and select "Run to Cursor" i place the cursor in front of the stement: "Next j". When I go up and hover over j in the statement: "For j = 0 To 20", the value of j shows as 21. I hover over the i on the vey next line and it is 23. IWhen i hover over the 4 values i am trying to get, it says that it is out of range.
So, i exit debug mode, place the cursor 1 line up so that it is directly before the statement: "Next i". When i go up and hover over j again, it shows "0", which is correct. i shows 23, which is one higher than it should be. However, when I hover over the 4 values, they re there and reflect what is in row 22, the very last row...
When I run the project, the UserForm1 pops up in Sheets("Question2") as it should. I enter 5 in the student id field and click the command button, which then runs the macro above. It returns 0 for gpa and 0 for test scores... changes the background color to red, as it should... but i am not sure how to find out what values it was using, other than the number i typed in or how to print the array to see wht, if anything was stored
And to address the "With" statement.. without it, i got a compile error because the object CEEStudent() needed to be declared or set.... I could not assign a value to it because it is an array and could not st it to Nothing because it is an object... so, yeah... confucing as all $#&!
And I ended up getting it to work the same way... by being explicit with everything, which is fine for this project. I can even just leave it that way... If i can get the correct values to come back and get them to display in the userform....
 
7:04 AM
so you got it to work or you didn't? This is an age old topic that you are talking about, not in your original question: How to pass variables and objects to a userform - you really can't. There are ways to access items outside the scope of the userform . . . . . that is a different topic . . .
 
@Wookies-Will-Code - "How to pass variables and objects to a userform - you really can't" That's not true at all.
 
What I am say is you can't do it like this show UserForm1(my Class, my object, my variables). Data of course gets passed in and put of scope, but it is something that needs to be mindful or you wind up in a position of not being able to access it. I simply write my own functions that do have parameters, and they are wrapped in the standard functions we are given, you call the userform function, my udf is called, using the userform functions as a pseudo interface
if you can pass parameters into and out of userform (a Class really), I am ready to learn. I follow M. Guidon and make the user form part of the data type as well.
 
A userform is just an object - any class can contain exposed methods (or even public variables if you want) which allow for communication between that class and other code. You can call other code from within your userform, and other code can call methods in your userform.
 
Ok, well I have updated the code once again.. I did away with the with statement in the beginning and solved my out of range error with some if else logic... it runs fine... Until i try to get the data displayed in the userform... It just sets everything to 0. where i try to assign the values , the opposite side is showing correct values, but it is not passing and being applied to the textboxes... However, the userform is using the values to meet the formatting/styling conditions perfectly
I GOT IT... IT WORKS!!! 100% WORKING!!!
I could not find anything at all on actually getting the textbox text set to any value i wanted... right along with what @Wookies-Will-Code was saying... But I did find out how to assign text values to the label captions! I simply changed the two text boxes to labels and the code to .labelName.caption = and it works!
 
Good to see you got it working. FYI within a userform you can use Me to refer to the form, istead of VBAProject.UserForm1 so for example Me.TextBox2.Text. That means if you rename your userform all the code inside will not break.
 
7:04 AM
Ok, so its all working great if i define the user input in the code. but it is not iterating through the array.. anyways, i am happy its working this much... how is an array structured exactly, in this scenario... if it was in a json type format, would it look like what I added to the bottom of the original post? Or no? I am trying to figure out how to write the for loop to search for the user input, which is StudentID
 
Time to open a new question if you're having a different issue. This one is done I think. Add a link back to this one if you need to give some background.
 
@Gene if you want to get to how to set and modify text box input, this answer was created for a different reason, but if you go through it you can likely find how to capture and/or change the textbox.text stackoverflow.com/a/48086180/8716187
 

last day (15 days later) »