PDA

View Full Version : need help for an array macro in word



crazyfisher
09-08-2007, 04:43 AM
Dear all,
I am a beginner of VBA programming and I have some problems with an array macro,plz help !!!

the array stores student ID and student Name and the macro will enable me to enter the student ID and student Mark in a form, and it will show up " student *** has a mark of ***".
this is my script:

Public arStuID(8) As String
Public arStudMark(8) As Integer

Sub startit()

arStuID(1) = "Tim"
arStuID(2) = "Jim"
arStuID(3) = "Dim"
arStuID(4) = "Tam"
arStuID(5) = "Tom"
arStuID(6) = "Tum"
arStuID(7) = "Lim"
arStuID(8) = "Nee"

UserForm1.Show


End Sub

Sub PrintAllMarks()

ActiveDocument.Bookmarks("Start").Select


For x = 1 To 8
If x = arStuID(x) Then
Selection.TypeText "Student " & arStuID(x) _
& " has a mark of " & arStudMark(x) & "." & vbCrLf
End If
Next x


End Sub


but the problem is even if I didn't supply a certain student ID and his mark, the document still show a line" student has a mark of 0"
how should I code to show only the inputed student ID and mark ??

thanks !!!

mdmackillop
09-08-2007, 06:06 AM
To get you started. Hard coding names and values is not a good way to input data. How would you anticipate entering names and marks?
Option Explicit

Public arStuID(8) As String
Public arStudMark(8) As Integer

Sub startit()

arStuID(1) = "Tim"
arStuID(2) = "Jim"
arStuID(3) = "Dim"
arStuID(4) = "Tam"
arStuID(5) = "Tom"
arStuID(6) = "Tum"
arStuID(7) = "Lim"
arStuID(8) = "Nee"

arStudMark(1) = 50
arStudMark(2) = 49
arStudMark(3) = 48
arStudMark(4) = 47
arStudMark(5) = 46
arStudMark(6) = 45
arStudMark(7) = 44
arStudMark(8) = 43


'UserForm1.Show


End Sub

Sub PrintAllMarks()
Dim x As Long
startit

ActiveDocument.Bookmarks("Start").Select


For x = 1 To 8
'If x = arStuID(x) Then
Selection.TypeText "Student " & arStuID(x) _
& " has a mark of " & arStudMark(x) & "." & vbCrLf
'End If
Next x


End Sub

fumei
09-08-2007, 02:52 PM
Two questions.

1. Do you have it set for using Base 1? By default Public arStuID(8) As String means nine items in the array. 0 - 8 is NINE items. Arrays by default are 0-based.

2. Are you in fact using Option Explicit? I suspect not, because:
If x = arStuID(x) Then should return a syntax error. No, not should.....

it WOULD stop the code and refuse to budge, because it IS an error.

This is why we use Option Explicit.
For x = 1 To 8
If x = arStuID(x) ThenLook at it. x is a number and arStud(x) is a string.

This is a mis-match error. x will NEVER = arStud(x). It is impossible.


the document still show a line" student has a mark of 0"It would not if you used Option Explicit, as the code would not run. It is an error.

crazyfisher
09-09-2007, 05:17 AM
sorry, that is a stupid error.. i was writing this ....

Public arStuID(8) As String
Public arStudMark(8) As Integer

Sub startit()

arStuID(1) = "Tim"
arStuID(2) = "Jim"
arStuID(3) = "Dim"
arStuID(4) = "Tam"
arStuID(5) = "Tom"
arStuID(6) = "Tum"
arStuID(7) = "Lim"
arStuID(8) = "Nee"

UserForm1.Show


End Sub

Sub PrintAllMarks()

ActiveDocument.Bookmarks("Start").Select


For x = 1 To 8

Selection.TypeText "Student " & arStuID(x) _
& " has a mark of " & arStudMark(x) & "." & vbCrLf

Next x


End Sub



so could any one tell me how to make it work without inputing all the items in the array ? thx!!

TonyJollans
09-09-2007, 05:29 AM
Simply ...


For x = 1 to 8
If arStuID(x)<> "" Then
Selection.TypeText etc
End If
Next x

mdmackillop
09-09-2007, 06:38 AM
This uses a text file to store names and marks. It could be done in Word, Excel, Access etc.