I got an excel file with an autoincrement on 1 cell, where the numberformat is "000".
Worksheets("TEST").Range("M17").Value = Worksheets("TEST").Range("M17").Value + 1
Worksheets("TEST").Range("M17").NumberFormat = "000"
This works fine, so my first number is: 001 instead of 1.
Now I need to merge this cell to another cell M16 (which displays an input for example: 2020)
=M16&"/"&M17
But for some reason it is losing his NumberFormat, and excel is showing: 2020/1 instead of 2020/001.
I have tried several things, like formatting my cell M18
Worksheets("TEST").Range("M18").NumberFormat = "0000/000"
Or by trying to solve this directly in vba
Worksheets("TEST").Range("M18").Value = Worksheets("FACTUUR").Range("M16").Value & Worksheets("TEST").Range("M17").Value
Worksheets("TEST").Range("M18").NumberFormat = "0000/000"
Or even by changing the format from within Excel itself, but then I get an error message directly (don't know why...)
Any idea on how to solve this?
You are on an excel application configured to work with decimal comma instead of decimal point.
Therefore the formula you type in the cell should have a semicolon
=M16&"/"&TEXT(M17;"000")
Note that the same formula can be assigned to the cell in VBA with a comma:
Range("M18").Formula="=M16&""/""&TEXT(M17,""000"")"
:(
The first part of your code only changes the way the number is displayed to the user. If you click into cell M17 after running your code, you will see that the actual value is simply 1 and not 001. You need to use format$ as BigBen suggested earlier and then concatenate as per the code below:
Worksheets("test").range("M17").value = Worksheets("test").range("M17").value + 1
Worksheets("test").range("M17").NumberFormat = "000"
Dim firstValue As String: firstValue = Worksheets("test").range("M16").value
Dim secondValue As String: secondValue =
Format$(Worksheets("test").range("M17").value, "000")
Worksheets("test").range("M18").value = firstValue & "/" & secondValue
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.