Variable not increasing

grabrail

Board Regular
Joined
Sep 6, 2010
Messages
128
Office Version
  1. 365
Platform
  1. Windows
Hi

I have a worksheet where I am using some code to moitor a number of cells for a value. If this value is entered, I want a variable to increase by one. The variable starts at zero. When the value of the variable reaches 14 I want another cell to display a different value. The code I currently have is as follows:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim imref8 As Integer

imref8 = 0

    ElseIf Not Intersect(Target, Range("M43:M48, M51:M54, M56:m59")) Is Nothing Then
        
                If Target.Value = "DF" Then
                    
                   tyres.Show
                    
                End If            
            
                If Target.Value = "NDF" Then
                    
                   imref8 = imref8 + 1
                   MsgBox (imref8)
                    
                End If
            
                If Target.Value = "N/A" Then
                                      
                   imref8 = imref8 + 1
                   MsgBox (imref8)
                    
                End If

end sub

The first section with value DF works fine and my user form appears. Its the next 2 sections I am having issues with. So basically if one of the cells is DNF or N/A I want the variable imref8 to increase by 1. However I change one of the cells to DNF, and msgbox shows me the variable is one. which is correct, however if i enter DNF in the next cell, msgbox shows me the variable is still one. How come the variable isnt incrementing with each DNF or N/A entry?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
sorry yes, that is correct, typo in my question, so my question on here should read "If I enter NDF"
 
Upvote 0
If you want it to retain its value between changes, then you should replace this:

Code:
Dim imref8 As Integer

imref8 = 0

with:

Code:
Static imref8 as Integer

or write the value out to a cell/name if you need it not to reset to 0 when you close and reopen the workbook.
 
Upvote 1
Solution
If you want it to retain its value between changes, then you should replace this:

Code:
Dim imref8 As Integer

imref8 = 0

with:

Code:
Static imref8 as Integer

or write the value out to a cell/name if you need it not to reset to 0 when you close and reopen the workbook.
Perfect thank you, the Static imref8 as integer does exactly what I need, thank you
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top