如何为下一个子例程保留此变量的值? [英] How can I keep the value of this variable for my next subroutine?

查看:114
本文介绍了如何为下一个子例程保留此变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名初级程序员(没有经验),正在为我现在正在从事的工作学习Visual Basic.我已经阅读了一天左右,终于决定开始制作所需的程序!

I'm a beginning programmer (no experience) learning Visual Basic for a job I'm doing right now. I've been reading for a day or so and have finally decided to start making the required program!

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

However, I'm running into some problems.

现在我有两个子例程.第一个子例程让用户输入他们有多少对数据,以便我可以创建一个表供他们填写.这样,他们的数据就在正确的位置,供我以后参考.

Right now I have two subroutines. The first subroutine lets the user input how many data pairs they have so that I can create a table for them to fill in. This is so their data is in the right place for me to reference it later.

输入完数据后,他们会按下一个按钮以启动另一个子例程,该子例程将对输入的数字进行一些计算.我的问题是我需要一个变量,该变量说明必须将多少个数据对传递给第二个例程.

There is then a button they press after they finish entering the data to start a different subroutine which will do some calculations to the numbers they entered. My issue is that I need the variable that says how many data pairs they have to carry over to the second routine.

在继续之前,这是到目前为止的代码! (您必须在窗口中向下滚动)我还要注意第二个子例程在单独的模块中.

Before I continue, here is my code so far! (You'll have to scroll down in the window) I should also note that the second subroutine is in a separate module.

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

为什么Counter的值不继承到第二个子例程?现在,我有一种解决方法,可以计算出B列中填写的单元格数量,该数量可以提供给我.但是,这样做使我无法将B列中的任何空间用于我想使用的其余工作表.

Why isn't the value for Counter carrying over to the second subroutine? Right now I have a workaround that counts the amount of cells filled out in column B, which gives me the number. However, that makes it so that I wouldn't be able to use any of the space in column B for the rest of the sheet, which I want to use.

任何人都可以帮忙吗?我以为公共"会使其成为工作簿级别的变量?每当我使第二个子窗口显示Counter的值时,它就会显示为零.

Can anyone help? I thought the "Public" would make it a Workbook-level variable? Whenever I make the second sub display the value of Counter, it comes up as zero.

对不起,如果我的代码混乱/效率低下.我还在学习. :)

Sorry if my code is messy/inefficient. I'm still learning. :)

推荐答案

在您的代码中,您两次声明Public Counter As Long.可能发生的情况是,每个Sub块都获得了不同的Counter变量.如果您删除第二个变量,则它们应该共享相同的变量.

In your code you are declaring Public Counter As Long twice. What's probably going on is that each of your Sub blocks are getting a different Counter variable. If you remove the second one they should both share the same variable.

此外,每个模块只需列出一次Option Explicit.现在,我看到您将这些指定为独立的模块,就可以了.

Also, you should only need to list Option Explicit once per module. Which, now that I see you specify these are separate modules, you are doing fine.

编辑:尝试阐明更多内容.

Trying to elucidate more.

将其视为分层,并且每个范围"都是该层可以访问的内容.每一层都可以访问自己和所有父级.这是简化的可视化效果:

Think of it as a layering and each "scope" is what the layer can access. Each layer has access to itself and all parents. Here's a simplified visualization:

( program 
    ( module
        ( sub )
    )
)

在子例程中引用变量,因此程序开始向上看.假设您已经设置了Option Explicit,这意味着必须手动定义变量,而永远不会自动定义它们.

In your subroutine you reference the variable, so the program starts looking upwards. Assuming of course you've set Option Explicit which means that the variables must be defined manually and they will never be defined automatically.

您想要的东西如下所示.该变量的作用域是全局的,以便可以从同时运行的其他模块中访问它.

What you want to have is something like the following. Where the variable is global in scope so that it can be accessed from other modules running concurrently.

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

这篇关于如何为下一个子例程保留此变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆