0
Private Sub CommandButton2_Click()

Dim TempVar As Integer

TempVar = NumNodes
NumNodes = NumNodes + 1
TempVar = NumNodes
Debug.Print "NumNodes + 1"

Call Node_Button_Duplication
Call Channel_Selection_Duplication

NumNodes = TempVar

Debug.Print "NumNodes = " & NumNodes 'Debug
Debug.Print "TempVar = " & NumNodes 'Debug
End Sub

Public Sub Channel_Selection_Duplication()

    Range("Q8:S8").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    Range("Q8:S8").Select
    ActiveCell.FormulaR1C1 = "Channel Usage Selection"
    Range("Q8:S52").Select
    Range("Q52").Activate
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
Range("Q8:S8").Select
Selection.Interior.ColorIndex = 36

End Sub

Public Sub Node_Button_Duplication()


Worksheets("Topology").Shapes("CommandButton1").Select
Selection.Copy
Worksheets("Topology").Paste
Selection.ShapeRange.IncrementLeft 339#
Selection.ShapeRange.IncrementTop -12.75

End Sub

我试图在调用 2 个子例程(Node_Button_Duplication 和 Channel_Selection_Duplication)之前保存 NumNodes 的值(一个全局变量),第一个子例程称为复制并粘贴电子表格中的命令按钮。我相信,这会重新编译 VBA 项目并重置(全部?)全局变量。

我试图写入一个单元格并从单元格中读回值,但这不起作用(基本上与使用临时变量的想法相同)。

上面的代码在运行时会导致每次运行时 TempVar 和 NumNodes 都重置为 1。我想知道保存变量不被重置的最好方法是什么?

4

1 回答 1

0

试试这个

Option Explicit

Private Sub CommandButton2_Click()
    Dim NumNodes as Long

    NumNodes = Sheets("Temp").Range("A1").Value

    NumNodes = NumNodes + 1

    Sheets("Temp").Range("A1").Value = NumNodes

    MsgBox "NumNodes = " & NumNodes

    Call Node_Button_Duplication
    Call Channel_Selection_Duplication
End Sub

确保您有一张名为“Temp”的工作表

现在试试看。

于 2012-05-15T20:53:50.387 回答