Forum Discussion
Copying Macro to new sheet with-in same workbook
Hi all,
I have a Macro button that calculates some math after entering an input in one cell located on the first sheet. It takes my input --> do some calculations using constants from other sheets -->then return an output on a cell in the first sheet. I would like to copy the same exact sheet where it does the math to a new sheet with in the same workbook. So basically I would like it to do the same thing on a second sheet with-in the same workbook.
Please help, Thank you
code:
Public Function Contains(col As Collection, Key As Variant) As Boolean
Dim obj As Variant
On Error GoTo err
Contains = True
obj = col(Key)
Exit Function
err:
Contains = False
End Function
Sub SCADA_Tool()
'
' SCADA_Tool Macro
'
Dim memoryUsage As Double
Dim productionCount As Double
Dim Machines As New Collection
Dim cellString1 As String
Dim cellString2 As String
Dim cellString3 As String
Dim Key As String
Dim Data As Double
Dim Memory As String
Set linetbl = Worksheets("SCADA Sizing Tool").ListObjects("machineTable")
Set machinetbl = Worksheets("SCADA Constants").ListObjects("machineTypesTable")
With linetbl.DataBodyRange
lineRows = .Rows.Count
lineCols = .Columns.Count
End With
With machinetbl.DataBodyRange
machineRows = .Rows.Count
machineCols = .Columns.Count
End With
memoryUsage = Worksheets("SCADA Constants").Range("B103")
productionCount = Worksheets("SCADA Sizing Tool").Range("B2")
For i = 3 To machineRows + 2
cellString1 = "K" + CStr(i)
cellString2 = "F" + CStr(i)
Data = Worksheets("SCADA Constants").Range(cellString1)
Key = Worksheets("SCADA Constants").Range(cellString2)
If Not Contains(Machines, Key) Then Machines.Add Data, Key
Next i
'
For i = 5 To lineRows + 4
cellString3 = "A" + CStr(i)
Memory = Worksheets("SCADA Sizing Tool").Range(cellString3)
memoryUsage = memoryUsage + (Machines(Memory) * productionCount)
Next i
Range("F18").Value = memoryUsage
End Sub