-->

我怎样才能保持这个变量的我的下一个子程序的价值?(How can I keep the value

2019-09-19 04:26发布

我学习Visual Basic的工作,我现在在做编程新手(无经验)。 我一直在读了一天左右,终于决定开始制作所需的程序!

但是,我遇到了一些问题。

现在我有两个子程序。 第一个子程序让用户输入多少个数据对他们有这样我可以创建一个表让他们填,这是所以他们的数据在正确的地方为我以后引用它。

有那么他们按他们完成输入数据开始不同的子程序,将做一些计算,他们进入后的数字按钮。 我的问题是,我需要的,说他们有多少数据对结转到第二个程序中的变量。

我继续之前,这里是我到目前为止的代码! (你必须在窗口中向下滚动),我也应该注意到,第二子程序是在一个单独的模块。

Option Explicit

Public Counter As Long


Sub TableCreation1()

    ActiveSheet.Shapes.Range(Array("Button 5")).Select

    Selection.Delete

    Counter = InputBox("How many pairs of data do you have? ")

    Range("A1") = "Time (days)"

    Range("B1") = "CFL (measured)"

    Range("A1:B1").Font.Bold = True

    Columns("A:B").EntireColumn.EntireColumn.AutoFit

    Range("A1").Select

    ActiveCell.Range("A1:B" & Counter + 1).Select

    Selection.Borders(xlDiagonalDown).LineStyle = xlNone

    Selection.Borders(xlDiagonalUp).LineStyle = xlNone

    With Selection.Borders(xlEdgeLeft)

       .LineStyle = xlContinuous

       .Weight = xlThin

    End With

    With Selection.Borders(xlEdgeTop)

        .LineStyle = xlContinuous

        .Weight = xlThin

    End With

    With Selection.Borders(xlEdgeBottom)

        .LineStyle = xlContinuous

        .Weight = xlThin

    End With

    With Selection.Borders(xlEdgeRight)

        .LineStyle = xlContinuous

        .Weight = xlThin

    End With

    With Selection.Borders(xlInsideVertical)

        .LineStyle = xlContinuous

        .Weight = xlThin

    End With

    With Selection.Borders(xlInsideHorizontal)

        .LineStyle = xlContinuous

        .Weight = xlThin

    End With

    Dim btn As Button

    Dim rng As Range

    With Worksheets("Sheet1")

        Set rng = .Range("A" & Counter + 2)

        Set btn = .Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height)

    With btn

        .Caption = "Click this button to begin calculations"

        .AutoSize = True

    End With

    End With

End Sub



Option Explicit

Dim IntermediateVariable As Long

Public Counter As Long

Sub FindCFLGuess()

IntermediateVariable = Worksheets("Sheet1").Range("B:B").Cells.SpecialCells(xlCellTypeConstants).Count

Counter = IntermediateVariable - 1

End Sub

为什么不是反携带到第二子程序的价值? 现在我有一个统计细胞在B列填写的金额,这给了我数的解决方法。 然而,这使得它让我不能就能够使用任何空间在B列的表的其余部分,我想用。

任何人都可以帮忙吗? 我认为,“公共”将使一个工作簿级的变量? 每当我做第二次显示计数器的值,它出现零。

很抱歉,如果我的代码是凌乱/低效。 我还在学习。 :)

Answer 1:

在你的代码的声明Public Counter As Long两次。 什么可能是怎么回事是每个的Sub块得到一个不同的Counter变量。 如果您删除第二个,他们应该都共享相同的变量。

此外,你应该只需要列出Option Explicit每个模块一次。 其中,现在,我看到您指定这些都是独立的模块,你做得很好。

编辑:试图阐明更多。

把它看成是一个分层,每个“范围”,也就是该层可以访问。 每一层都可以访问本身和所有的父母。 这里有一个简单的可视化:

( program 
    ( module
        ( sub )
    )
)

在你的子程序中应引用变量,因此该方案开始寻找向上。 假设你当然已经设置Option Explicit ,这意味着该变量必须手动定义,他们将永远不会被自动定义。

你想拥有什么是类似于下面的东西。 当变量是全球范围,以便它可以从同时运行的其它模块进行访问。

( program
    [global variable]
    ( module1
        ( sub )
    )
    ( module2 )
)


Answer 2:

我删除了一堆你无关的代码,你可能需要在生产,但在看到问题的办法是刚开。 我测试了以下内容,计数器保持了在所述第一子集的第一模块中的子在第二模块中它的值。

模块1:

Option Explicit

    Public Counter As Long

    Sub TableCreation1()
        Dim btn As Button
        Dim rng As Range

        Counter = InputBox("How many pairs of data do you have? ")
        Range("A1") = "Time (days)"
        Range("B1") = "CFL (measured)"
        Range("A1:B1").Font.Bold = True
        Columns("A:B").EntireColumn.EntireColumn.AutoFit
        Range("A1").Select
        ActiveCell.Range("A1:B" & Counter + 1).Select

        With Worksheets("Sheet1")
            Set rng = .Range("A" & Counter + 2)
            Set btn = .Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height)
            With btn
                .Caption = "Click this button to begin calculations"
                .AutoSize = True
            End With
        End With
    End Sub

模块2:

Option Explicit

Sub FindCFLGuess()
    MsgBox ("Counter = " & Counter)
End Sub

如果您创建了2个模块一个新的工作簿,并将代码粘贴到每个,你的计数器变量的值将显示在messagebox ,如果你运行TableCreation1然后FindCFLGuess顺序。

如果你有一个运行时错误,出于某种原因,或者打破你的代码的执行,显然计数器就会失去它的价值。 如果没有这些事情正在发生的事情,你应该添加一块手表,而你通过您的代码步跟踪变量的值。

要添加表,右键单击该变量,选择添加监视...,然后从模块下拉列表中选择(所有模块)。 您也可以在手表休息时表达式的值发生变化。



文章来源: How can I keep the value of this variable for my next subroutine?