VBA Dropping Leading Zero

Tsmith25

New Member
Joined
Dec 9, 2019
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
I have a string of test that I am trying to just copy out item numbers and such. Each item number has differing lengths and some of the item numbers have leading zeros. When I run the macro it is dropping some of the leading zeros off some of the items. I am sure it is the way I have written the macro, see blow.
VBA Code:
Sub Button1_Click()



For x = 1 To 60000

A = Sheet2.Cells(1048576, 1).Value

If Sheet1.Cells(x, 2).Value > 1 And Sheet1.Cells(x, 3).Value > 1 And Sheet1.Cells(x, 4).Value > 1 And Sheet1.Cells(x, 2).Value <> "PART" And Sheet1.Cells(x, 2).Value <> "NO.#" Then


Sheet2.Cells(A + 1, 1).Value = Sheet1.Cells(x, 2).Value
Sheet2.Cells(A + 1, 2).Value = Sheet1.Cells(x, 3).Value
Sheet2.Cells(A + 1, 3).Value = Sheet1.Cells(x, 4).Value
Sheet2.Cells(A + 1, 4).Value = Sheet1.Cells(x, 5).Value

End If

Next x


End Sub
 
Last edited by a moderator:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Welcome to the Board!

If you want to copy it over, and not drop the leading zeroes, don't convert it to Value when copying, i.e.
change all references like:
Code:
Sheet2.Cells(A + 1, 1).Value = Sheet1.Cells(x, 2).Value
to
Code:
Sheet2.Cells(A + 1, 1) = Sheet1.Cells(x, 2)
 
Upvote 0
You are welcome.
Glad I was able to help.
The ".Value" coerces the text value to a numeric one, dropping the leading zeroes.
 
Upvote 0
The ".Value" coerces the text value to a numeric one, dropping the leading zeroes.

Apparently so. But that highlights an idiosyncracy of VBA and an error in VBA documentation.

According to VBA documentation, the default member of a Range object is the Value property. So we wouldn't expect a difference.

But obviously, VBA does indeed treat them differently in some contexts.

(Not a complete surprise. I've noticed differences before. I just didn't expect __this__ particular difference.)
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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