我是一名初级程序员(没有经验),正在为我现在正在做的工作学习 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
为什么 Counter 的值不能延续到第二个子例程?现在我有一个解决方法来计算 B 列中填写的单元格数量,这给了我数字。但是,这使得我无法将 B 列中的任何空间用于我想要使用的工作表的其余部分。
任何人都可以帮忙吗?我认为“公共”会使其成为工作簿级别的变量?每当我让第二个子显示 Counter 的值时,它就会显示为零。
抱歉,如果我的代码混乱/效率低下。我还在学习。:)