0% found this document useful (0 votes)
7 views3 pages

Orga's assignment

This document contains a VBA macro that generates a mass balance table in Excel. It iterates 20 times to calculate values related to feed, recycle, and reactor outputs, writing the results into specified cells. The macro also formats the table and displays a success message upon completion.

Uploaded by

presamjay10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Orga's assignment

This document contains a VBA macro that generates a mass balance table in Excel. It iterates 20 times to calculate values related to feed, recycle, and reactor outputs, writing the results into specified cells. The macro also formats the table and displays a success message upon completion.

Uploaded by

presamjay10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sub GenerateMassBalanceTable()

Dim ws As Worksheet

Dim I As Integer

Dim feed As Double

Dim recycle_guess As Double

Dim into_reactor As Double

Dim out_reactor As Double

Dim recycle_end As Double

‘ Set the worksheet (modify “Sheet1” to your target sheet name)

Set ws = ThisWorkbook.Sheets(“Sheet1”)

‘ Clear the area where the table will be written (optional, adjust range as
needed)

Ws.Range(“A1:F21”).Clear

‘ Write table headers

Ws.Range(“A1”).Value = “Iteration Number”

Ws.Range(“B1”).Value = “Feed”

Ws.Range(“C1”).Value = “Recycle (guess)”

Ws.Range(“D1”).Value = “Into Reactor”

Ws.Range(“E1”).Value = “Out of Reactor”

Ws.Range(“F1”).Value = “Recycle (at end of iteration)”

‘ Initialize constants

Feed = 1 ‘ Feed is constant at 1 mol/hr

Recycle_guess = 0 ‘ Initial guess for recycle in iteration 1


‘ Loop through 20 iterations

For I = 1 To 20

‘ Calculate values for the current iteration

Into_reactor = feed + recycle_guess

Out_reactor = 0.6 * into_reactor ‘ 60% of input remains (40% reacts)

Recycle_end = out_reactor ‘ Recycle at end of iteration

‘ Write values to the table

Ws.Cells(I + 1, 1).Value = I ‘ Iteration Number

Ws.Cells(I + 1, 2).Value = feed ‘ Feed

Ws.Cells(I + 1, 3).Value = recycle_guess ‘ Recycle (guess)

Ws.Cells(I + 1, 4).Value = into_reactor ‘ Into Reactor

Ws.Cells(I + 1, 5).Value = out_reactor ‘ Out of Reactor

Ws.Cells(I + 1, 6).Value = recycle_end ‘ Recycle (at end of iteration)

‘ Update recycle_guess for the next iteration

Recycle_guess = recycle_end

Next i

‘ Format the table (optional)

Ws.Range(“A1:F21”).NumberFormat = “0.0000000” ‘ Set precision to


match table

Ws.Range(“A1:F1”).Font.Bold = True ‘ Bold headers

Ws.Range(“A1:F21”).Borders.LineStyle = xlContinuous ‘ Add borders

Ws.Columns(“A:F”).AutoFit ‘ Adjust column widths


MsgBox “Table generated successfully!”, vbInformation

End Sub

You might also like