visual basic increment on cell update

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


visual basic increment on cell update



Currently trying to take each cell update and store in a cell that is basically A55 plus one row each update. So start with A55 then next update A56 then A57 so on forever on each update. Current code is stuck at cell 55 though:


Private Sub Worksheet_Calculate()
Dim target As Range
Set target = Range("U13")
Dim Counter As Integer
Counter = 55
If Not Intersect(target, Range("U13")) Is Nothing Then
Cells(Counter, "A").value = target
End If
Counter = Counter + 1
End Sub





Each time this is run, it will start with Counter=55 and the increment, Counter=Counter+1 will not carry over to the next execution.
– Rey Juna
5 hours ago




Counter=55


Counter=Counter+1





You could create a helper cell to store your counter value and increment and update that cell's value before you exit. If you use a named range for the cell, such as MyCounter, then Counter=Range("MyCounter").Value and Set Range("MyCounter").Value=Counter+1
– Rey Juna
5 hours ago


MyCounter


Counter=Range("MyCounter").Value


Set Range("MyCounter").Value=Counter+1





Would I still use Counter inside the target?
– James Shelton
5 hours ago





Do you mind making an answer so I can vote for it and see it in full please
– James Shelton
4 hours ago





If Counter were to be declared at a global level, and initialized at 55. Then you could increment it within this sub.
– JosephC
4 hours ago




3 Answers
3



Just count the rows in column A


Private Sub Worksheet_Calculate()
Dim target As Range, LstRw As Long

Set target = Range("U13")
LstRw = Cells(Rows.Count, "A").End(xlUp).Row + 1

If LstRw < 55 Then LstRw = 55

If Not Intersect(target, Range("U13")) Is Nothing Then
Cells(LstRw, "A").Value = target
End If

End Sub





this did not work does x1up need to be defined?
– James Shelton
4 hours ago





@James xlUp - the letter l, not the number 1
– BigBen
4 hours ago


xlUp


l


1





Okay this still did not work. nothing happens. At least in my code a55 updated.
– James Shelton
4 hours ago





Check if you have other cells filled in below A55, maybe there are some way down.
– Davesexcel
4 hours ago





That worked! How can I delete all on a button click clear for example, or delete all when it is loaded.
– James Shelton
4 hours ago



Not sure when you want to run init(), you'll have to pick an event. But each time you run Worksheet_Calculate() after that it will increment.


Public Counter As Integer

Sub Init()
Counter = 55
End Sub

Private Sub Worksheet_Calculate()
Dim target As Range
Set target = Range("U13")

If Not Intersect(target, Range("U13")) Is Nothing Then
Sheet1.Cells(Counter, "A").Value = target
End If
Counter = Counter + 1
End Sub





how will I run something, new to vb
– James Shelton
4 hours ago





I am not sure how to get this working in context of my code.
– James Shelton
4 hours ago



Create a helper cell to store your counter value and increment and update that cell's value before you exit.



If you use a named range for the cell, such as MyCounter, then modify your code to this:


MyCounter


Private Sub Worksheet_Change(ByVal Target As Range)
Dim Counter As Integer

Counter = Range("MyCounter").Value

If Not Intersect(Target, Range("U13")) Is Nothing Then
Cells(Counter, 1).Value = Target
Range("MyCounter").Value = Counter + 1
End If

End Sub



I changed the event type to Worksheet_Change. Worksheet_Calculate doesn't allow you to pass the Target as Range.


Worksheet_Change


Worksheet_Calculate



Here is a screen shot



Increment on cell update using helper cell



Cell Y1 is named "MyCounter" and you would initialize it to 55. Then each time you change the value of cell U13, it will put that value into the row that MyCounter is pointing to and then increment MyCounter.



I would recommend that you put your helper cell on another sheet instead of the way that I did it in the example, but if you do then update the code to include the sheet name in the range reference, such as Counter = Sheets("HelperSheet").Range("MyCounter").Value. Hope that helps.


Counter = Sheets("HelperSheet").Range("MyCounter").Value






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

CRM reporting Extension - SSRS instance is blank

Keycloak server returning user_not_found error when user is already imported with LDAP