PDA

View Full Version : Java Code to VBA



EML
03-17-2012, 09:29 AM
Hey guys, I'm new here.
I got a big school project in Excel ahead of me, so I'll be here quite often :whistle:
I'm planning to contribute, but first .. I need a help :help

I'm trying to convert this code written in java to VBA:
package jobshop;
import java.util.ArrayList;
import java.util.List;

public class JobShop {
final int N=3, M=3;
final int MAXINT=100000;
int[ ][ ] ProcessingTimes;
int[ ][ ] Order;
int[ ][ ] Solution;
int[] MachineReady = new int[M];
int[] JobReady = new int[N];

private class Phase {
int job;
int machine;
int order;
public Phase(int job, int machine, int order){
this.job = job; this.machine = machine; this.order= order;
}
}

private int readNfromExcel(){
return N;
}
private int readMfromExcel(){
return M;
}

private int[][] loadPhaseOrderFromExcel(){
int [][] A= new int[N][M]; //Job - Machine
A[0][0]=0; A[0][1]=1; A[0][2]=2;
A[1][0]=2; A[1][1]=0; A[1][2]=1;
A[2][0]=0; A[2][1]=1; A[2][2]=2;
return A;
}
It's just small part of the code, I think I can do the rest If you can help me with the start.

My main problem is that I don't know how to represent few things in VBA and what are their substitutions. Is everything supposed to be in one single module?

Last part should not be inputed manually in code, but from excel worksheets. But I'm gonna deal with it later after verification that it works properly. Also those arrays will be much bigger.

Here's what I got so far.

Dim N As Integer: N = 3
Dim M As Integer: M = 3
Dim MAXINT As Integer: MAXINT = 100000
Dim ProcessingTimes()
Dim order()
Dim Solution()
Dim MachineReady(M) As Integer
Dim JobReady(N) As Integer

Private Sub Phase()
Dim job As Integer
Dim machine As Integer
Dim order As Integer
Public Sub Phase(job, machine, order)
Me.job = job
Me.machine = machine
Me.order = order
End Sub

Private Function readNfromExcel() As Integer
Return N
End Function

I'm getting compile error in that "return N" line. I don't know if the rest is ok.

So .. can anybody help me, please? : pray2:

Kenneth Hobs
03-17-2012, 03:19 PM
Welcome to the forum! Most forums have an FAQ about homework help. http://www.vbaexpress.com/forum/faq.php?faq=psting_faq_item#faq_hom_faq_item

You will have lots of work if you expect to use the routines contained within the import code. Your routine is for a Class.

Let me show you a couple of things to help you get started. First, let's look at a function. Replace Return with the Function name equals. e.g. in a Module:
Option Explicit

Dim N As Integer

Sub AssignN()
N = 3
End Sub

Sub Test_readNfromExcel()
AssignN
MsgBox readNfromExcel, vbInformation, readNfromExcel + 1
End Sub

Function readNfromExcel() As Integer
readNfromExcel = N
End Function
It is not uncommon to make Functions in a Class to be private. If they are Public then they are available to other routines and macros that can be played.

Keep in mind that java is the programming language as is Visual Basic but Visual Basic for Applications has the Application object built-in without the need for setting it.

Try to ask small concept questions when you get stuck. The KB and other forum threads are goods ways to learn VBA.

I am not very good with classes but there are some examples in this forum. e.g. http://www.vbaexpress.com/forum/showthread.php?t=39194

EML
03-18-2012, 02:14 AM
Thanks for your reply.
About that homework issue, I'm sorry. I thought it will help you to understand my problem. And it's not even close to the whole code, but one more time, sorry.

I'm not sure If I see what you're trying to say. Do I need to completely change my thinking in VBA? The way I'm trying to convert it line by line is incorrect? Or just uncommon?
Should I substitute it all with procedures and functions only?