Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 将excel对象发送到子例程_Vb.net_Visual Studio_Visual Studio 2010 - Fatal编程技术网

Vb.net 将excel对象发送到子例程

Vb.net 将excel对象发送到子例程,vb.net,visual-studio,visual-studio-2010,Vb.net,Visual Studio,Visual Studio 2010,就目前而言,我有一段类似这样的代码(稍作解释,但我相信你明白了) 这在几个不同的地方又发生了3次。。。因此,现在我正试图重构代码,使其更简洁 我想做的是: If ComboBox1.SelectedIndex = 1 Then swEV2.Stop() If ComboBox3.SelectedIndex = 0 Then Excelupdate(xlWorkSheet203, AT) ElseIf ComboBox3.

就目前而言,我有一段类似这样的代码(稍作解释,但我相信你明白了)

这在几个不同的地方又发生了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表格仍然没有更新新的信息。我还缺少什么吗?

我会检查/更改一些东西:

1) 将函数中的ByRefs更改为ByVal。您不需要更新对工作表的引用或修改字符串,因此不需要ByRef

2) 确定更新参数的数据类型。您正在混合操作和类型,这可能导致单元格引用不正确

如果单元格引用的目标是:

worksheet.Cells(("A2"), 3)
worksheet.Cells((12), 3)
然后,您应该将代码更改为:

worksheet.Cells((update & "2"), 3)
如果单元格引用的目标是:

worksheet.Cells(("A2"), 3)
worksheet.Cells((12), 3)
然后您应该更改更新参数类型:

update as integer

byval和byref有什么区别?我什么时候会使用byref?目标是“12”,所以我将尝试将其改为整型,将参数ByRef作为整型,这意味着过程可以修改传入的变量。对于对象,这意味着您可以将值设置为Nothing或创建新实例。对于字符串,这意味着您可以更改字符串的值,它将返回给调用者。ByVal表示按值传递对象。对于对象,可以更改或访问对象内的属性,但不能更改或访问对象引用本身。对于字符串,这意味着您可以在过程中随心所欲地更改字符串,但这不会影响调用方范围内的变量。我在上面的问题框中发布了对原始代码的编辑,我认为在上面编写代码比在注释框中编写代码更容易。好吧,我想出来了,我列出了错误的组合框:P谢谢!