As it is right now I have a bit of code that kind of looks like this (a little paraphrased but Im sure you get the idea)
If ComboBox1.SelectedIndex = 1 Then
swEV2.Stop()
If ComboBox3.SelectedIndex = 0 Then
xlWorkSheet202.Activate()
xlWorkSheet202.Cells((AT + 2), 3) = TextBox1.Text
xlWorkSheet202.Cells((AT + 3), 2) = "PSS (kBs)"
xlWorkSheet202.Cells((AT + 3), 3) = "USS (kBs)"
xlWorkSheet202.Cells((AT + 3), 4) = "User %"
xlWorkSheet202.Cells((AT + 3), 5) = "Kernel %"
xlWorkSheet202.Cells((AT + 3), 6) = "Total %"
xlWorkSheet202.Cells((AT + 4), 1) = "Min:"
xlWorkSheet202.Cells((AT + 5), 1) = "Max:"
xlWorkSheet202.Cells((AT + 6), 1) = "Average:"
xlWorkSheet202.Cells((AT + 7), 1) = "Median:"
xlWorkSheet202.Cells((AT + 8), 1) = "Stan Dev:"
ElseIf ComboBox3.SelectedIndex = 2 Then
xlWorkSheet204.Cells((WT + 2), 3) = TextBox1.Text
xlWorkSheet204.Cells((WT + 3), 2) = "PSS (kBs)"
xlWorkSheet204.Cells((WT + 3), 3) = "USS (kBs)"
xlWorkSheet204.Cells((WT + 3), 4) = "User %"
xlWorkSheet204.Cells((WT + 3), 5) = "Kernel %"
xlWorkSheet204.Cells((WT + 3), 6) = "Total %"
xlWorkSheet204.Cells((WT + 4), 1) = "Min:"
xlWorkSheet204.Cells((WT + 5), 1) = "Max:"
xlWorkSheet204.Cells((WT + 6), 1) = "Average:"
xlWorkSheet204.Cells((WT + 7), 1) = "Median:"
xlWorkSheet204.Cells((WT + 8), 1) = "Stan Dev:"
This goes on 3 more times in a few different places… So now I am trying to refactor the code to make it cleaner and shorter.
What I would like to do is this:
If ComboBox1.SelectedIndex = 1 Then
swEV2.Stop()
If ComboBox3.SelectedIndex = 0 Then
Excelupdate(xlWorkSheet203, AT)
ElseIf ComboBox3.SelectedIndex = 2 Then
Excelupdate(xlWorkSheet204, WT)
Private sub ExcelUpdate(byref worksheet as object, byref update as string)
worksheet.Activate()
worksheet.Cells((update + 2), 3) = TextBox1.Text
worksheet.Cells((update + 3), 2) = "PSS (kBs)"
worksheet.Cells((update + 3), 3) = "USS (kBs)"
worksheet.Cells((update + 3), 4) = "User %"
worksheet.Cells((update + 3), 5) = "Kernel %"
worksheet.Cells((update + 3), 6) = "Total %"
worksheet.Cells((update + 4), 1) = "Min:"
worksheet.Cells((update + 5), 1) = "Max:"
worksheet.Cells((update + 6), 1) = "Average:"
worksheet.Cells((update + 7), 1) = "Median:"
worksheet.Cells((update + 8), 1) = "Stan Dev:"
end sub
I thought for sure the above would work but it still seems that I am missing something, when I open the excel sheet nothing was printed. This would cut down the lines of code that I have in half easily, so I would love to find a solution for this
Thanks Guys
……………………………………………….
Edit (Sorry those comment boxes are terrible for writing anything)
……………………………………………….
alright I tried changing these lines of code:
If ComboBox2.SelectedIndex = 1 Then
If ComboBox3.SelectedIndex = 0 Then
ExcelUpdate(xlWorkSheet202, AT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
ElseIf ComboBox3.SelectedIndex = 1 Then
ExcelUpdate(xlWorkSheet203, GT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
ElseIf ComboBox3.SelectedIndex = 2 Then
ExcelUpdate(xlWorkSheet204, WT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
ElseIf ComboBox3.SelectedIndex = 3 Then
ExcelUpdate(xlWorkSheet205, OT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
End If
End If
Private Sub ExcelUpdate(ByVal Sheet As Object, ByVal update As Integer, ByVal pval As Double, ByVal uval As Double, ByVal user As Double, ByVal kernel As Double)
update = update + 1
Sheet.cells(update, 1) = timenow
Sheet.cells(update, 2) = pval
Sheet.cells(update, 3) = uval
Sheet.cells(update, 4) = user
Sheet.cells(update, 5) = kernel
Sheet.cells(update, 6) = cdbl(kernel + User)
end sub
But the excel sheets still do not update with the new information. Is there anything else im missing?
I would check/change a couple of things:
1) Change the ByRefs in the function to ByVal. You don’t need to update the reference to the worksheet or modify the string, so ByRef is not need.
2) Determine the data type of the update parameter. You are mixing operation and types, which could result in an incorrect cells reference.
If the goal of the cell reference is:
then you should change your code to:
If the goal of the cell reference is:
then you should change the update parameter type: