Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

fixing code to increment number in cell

0

Hi

I  have  this  code

Option Explicit
Dim vT As String, vT2 As String

Sub OnePlus()
    vT = [G6]
    vT2 = Trim(Mid(vT, 7, Len(vT) - 1))
    [G6] = Left(vT, 7) & CLng(vT2) + 1
End Sub

in G6 I have A 2345678

what  I  expect to incerement  the  last number  like A2345679   but  will show this 

A 23456679  and this A 234566679 

so  how  can  i fix  it  guys?

Answer
Discuss

Answers

0
Selected Answer

hi Halk

If you need "A 23456680" putting back in G6, use RIGHT instead, so:

Option Explicit
Dim vT As String, vT2 As String

Sub OnePlus()
    vT = [G6]
    'vT2 = Trim(Mid(vT, 7, Len(vT) - 1))
    vT2 = Trim(Right(vT, 3))
    'write back to cell
    [G6] = Left(vT, 7) & CLng(vT2) + 1

End Sub
Discuss

Discussion

all of  things have fixed .
thanks , John .
Halk (rep: 30) Jun 5, '23 at 12:16 pm
Great. Thanks for selecting my Answer. Halk. 
John_Ru (rep: 6142) Jun 5, '23 at 2:58 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login