简体   繁体   中英

Excel popup for cell calculation

First time poster here. I am a beginner when it comes to Excel VBA code. How do I create a PopUp window that shows up when I click a cell which then updates depending on what is entered in the PopUp Window? The cell contains a number I want to be updated.

Say I have a list of cells containing numbers, I click on one of the cells and in the window popup, I write +5. The original number now updates with 5 added to it. The same would go in reverse if i wrote -5 in the PopUp window.

This should get you started:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim targetRange As Range
    Dim activeRange As Range
    Dim origVal As Integer
    Dim addVal  As Integer
    Dim dialogResult As String


    Set targetRange = Target(1, 1)
    Set activeRange = Range("B2:B207")

    If Not Intersect(targetRange, activeRange) Is Nothing Then

        origVal = targetRange.Value

        dialogResult = InputBox("Enter number to add")

        If dialogResult <> "" Then

            If IsNumeric(dialogResult) Then
                addVal = dialogResult
                targetRange.Value = origVal + addVal
            Else
                MsgBox "Please enter a valid number!", vbCritical
            End If

        End If


    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM