就像现在一样,我有一些代码看起来像这样(有点解释但我相信你明白了)
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:"
这在几个不同的地方再发生3次......所以现在我正在尝试重构代码以使其更清洁和更短。
我想做的是:
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
我确信上面的内容会起作用,但似乎我仍然缺少一些东西,当我打开excel表时没有打印出来。这样可以轻松减少我的代码行,所以我很乐意为此找到解决方案
谢谢大家
............................................... ........
编辑(对不起那些评论框很难写任何东西)
............................................... ........
好吧我试过更改这些代码行:
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
但是excel表仍然没有更新新信息。还有其他什么遗失吗?
答案 0 :(得分:0)
我会检查/改变一些事情:
1)将函数中的ByRef更改为ByVal。您不需要更新对工作表的引用或修改字符串,因此不需要ByRef。
2)确定更新参数的数据类型。您正在混合操作和类型,这可能导致不正确的单元格引用。
如果单元格引用的目标是:
worksheet.Cells(("A2"), 3)
然后您应该将代码更改为:
worksheet.Cells((update & "2"), 3)
如果单元格引用的目标是:
worksheet.Cells((12), 3)
然后你应该更改更新参数类型:
update as integer