- Joined
- Oct 27, 2009
- Messages
- 1
- Reaction score
- 0
Hi, I have problem how to send an output based on user selected location that they input on the input box. But, now the output always appear on the same worksheet when we first run the macro. (so when user choose worksheet 2, the result always appear on worksheet 1).
This is our vba macro:
Option Base 1
Sub LoanRepayment()
Dim r As Single, a As Single
Dim n As Single
Dim s As Single
Dim tests As Boolean
Dim LoanRepayment_array() As Double
Dim x As Long, y As Long
' Get Annual Interest Rate
Do
r = Application.InputBox("Enter the Annual Interest Rate (%):", "Annual Interest", , , , , , 1)
If r > 0 Then
tests = True
Else
MsgBox "Annual Interest rate must be entered", vbCritical, "Warning"
tests = False
End If
r = r / 1200
Loop Until tests
' Get Minimum Loan Amount
Do
a = Application.InputBox("Enter the Minimum Loan Amount", "Loan", , , , , , 1)
If a > 0 Then
tests = True
Else
MsgBox "Minimum Loan Amount must be entered", vbCritical, "Warning"
tests = False
End If
Loop Until tests
' Get Minimum Number of Payments
Do
n = Application.InputBox("Enter the Minimum Number of Payments", "Number of Payments", , , , , , 1)
If n > 0 Then
tests = True
Else
MsgBox "Minimum Number of Payments must be entered", vbCritical, "Warning"
tests = False
End If
Loop Until tests
' Get Number of steps
Do
s = Application.InputBox("Enter the Number of steps", "Number of steps", , , , , , 1)
If s > 0 Then
tests = True
Else
MsgBox "Number of steps must be entered", vbCritical, "Warning"
tests = False
End If
Loop Until tests
' Calculate Loan Repayment,formulae: LoanRepayment_array(1 + s, s) = (a + 10000 * (x - 1)) * (r * (1 + r) ^ (n + 10 * (y - 1))) / ((1 + r) ^ (n + 10 * (y - 1)) - 1)
Set outrange = Application.InputBox("Enter the output range", "Output Range" _
, "A1", , , , , 8)
outrange.Select
Do
ReDim LoanRepayment_array(1 + s, s) As Double
ReDim LoanRepayment_array2(s, s) As Double
Dim indexrow As Long
Dim IndexColumn As Long
Selection.Offset(0, ((s - 3) / 2)) = "Loan Repayments for an Interest Rate of" & (1200 * r) & "% p.a. (Compounded Monthly)"
For indexrow = 1 To UBound(LoanRepayment_array)
Selection.Offset(indexrow + 1, 0).Value = a + 10000 * (indexrow - 1)
Selection.Offset(indexrow + 1, 0).EntireColumn.ColumnWidth = 14
Next indexrow
For IndexColumn = 1 To UBound(LoanRepayment_array2)
Selection.Offset(1, IndexColumn).Value = n + 10 * (IndexColumn - 1)
Selection.Offset(1, IndexColumn).EntireColumn.ColumnWidth = 14
Next IndexColumn
For indexrow = 1 To UBound(LoanRepayment_array)
For IndexColumn = 1 To UBound(LoanRepayment_array2)
Selection.Offset(indexrow + 1, IndexColumn) = (a + 10000 * (indexrow - 1)) * (r * (1 + r) ^ (n + 10 * (IndexColumn - 1))) / ((1 + r) ^ (n + 10 * (IndexColumn - 1)) - 1)
Selection.Offset(indexrow + 1, IndexColumn).EntireColumn.ColumnWidth = 14
Selection.Offset(indexrow + 1, IndexColumn).NumberFormat = "$#,##0.00_);[red]($#,##0.00)"
Next IndexColumn
Next indexrow
Loop Until s
'send output to the printer
Dim Button As Integer
Button = MsgBox("Do you want to print the result ? ", vbQuestion + vbOKCancel, "Print")
If Button = vbOK Then Application.Dialogs(xlDialogPrint).Show
If Button = vbCancel Then Close
End Sub
thank you
This is our vba macro:
Option Base 1
Sub LoanRepayment()
Dim r As Single, a As Single
Dim n As Single
Dim s As Single
Dim tests As Boolean
Dim LoanRepayment_array() As Double
Dim x As Long, y As Long
' Get Annual Interest Rate
Do
r = Application.InputBox("Enter the Annual Interest Rate (%):", "Annual Interest", , , , , , 1)
If r > 0 Then
tests = True
Else
MsgBox "Annual Interest rate must be entered", vbCritical, "Warning"
tests = False
End If
r = r / 1200
Loop Until tests
' Get Minimum Loan Amount
Do
a = Application.InputBox("Enter the Minimum Loan Amount", "Loan", , , , , , 1)
If a > 0 Then
tests = True
Else
MsgBox "Minimum Loan Amount must be entered", vbCritical, "Warning"
tests = False
End If
Loop Until tests
' Get Minimum Number of Payments
Do
n = Application.InputBox("Enter the Minimum Number of Payments", "Number of Payments", , , , , , 1)
If n > 0 Then
tests = True
Else
MsgBox "Minimum Number of Payments must be entered", vbCritical, "Warning"
tests = False
End If
Loop Until tests
' Get Number of steps
Do
s = Application.InputBox("Enter the Number of steps", "Number of steps", , , , , , 1)
If s > 0 Then
tests = True
Else
MsgBox "Number of steps must be entered", vbCritical, "Warning"
tests = False
End If
Loop Until tests
' Calculate Loan Repayment,formulae: LoanRepayment_array(1 + s, s) = (a + 10000 * (x - 1)) * (r * (1 + r) ^ (n + 10 * (y - 1))) / ((1 + r) ^ (n + 10 * (y - 1)) - 1)
Set outrange = Application.InputBox("Enter the output range", "Output Range" _
, "A1", , , , , 8)
outrange.Select
Do
ReDim LoanRepayment_array(1 + s, s) As Double
ReDim LoanRepayment_array2(s, s) As Double
Dim indexrow As Long
Dim IndexColumn As Long
Selection.Offset(0, ((s - 3) / 2)) = "Loan Repayments for an Interest Rate of" & (1200 * r) & "% p.a. (Compounded Monthly)"
For indexrow = 1 To UBound(LoanRepayment_array)
Selection.Offset(indexrow + 1, 0).Value = a + 10000 * (indexrow - 1)
Selection.Offset(indexrow + 1, 0).EntireColumn.ColumnWidth = 14
Next indexrow
For IndexColumn = 1 To UBound(LoanRepayment_array2)
Selection.Offset(1, IndexColumn).Value = n + 10 * (IndexColumn - 1)
Selection.Offset(1, IndexColumn).EntireColumn.ColumnWidth = 14
Next IndexColumn
For indexrow = 1 To UBound(LoanRepayment_array)
For IndexColumn = 1 To UBound(LoanRepayment_array2)
Selection.Offset(indexrow + 1, IndexColumn) = (a + 10000 * (indexrow - 1)) * (r * (1 + r) ^ (n + 10 * (IndexColumn - 1))) / ((1 + r) ^ (n + 10 * (IndexColumn - 1)) - 1)
Selection.Offset(indexrow + 1, IndexColumn).EntireColumn.ColumnWidth = 14
Selection.Offset(indexrow + 1, IndexColumn).NumberFormat = "$#,##0.00_);[red]($#,##0.00)"
Next IndexColumn
Next indexrow
Loop Until s
'send output to the printer
Dim Button As Integer
Button = MsgBox("Do you want to print the result ? ", vbQuestion + vbOKCancel, "Print")
If Button = vbOK Then Application.Dialogs(xlDialogPrint).Show
If Button = vbCancel Then Close
End Sub
thank you