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 に答える