VBA: moving an array but keeping the format unchanged

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
Office Version
  1. 365
Platform
  1. Windows
I have a list of suppliers codes, most of them are in text format like

ABC, DEF, XYZ

however I have a few that are just numbers,

12456
6789
00056

This last one is causing me problems as the leading zeros are lost

I am moving the data, trying to avoid a copy paste:

Code:
Sheets("List").Range("A3:K1000").Value2 = Sheets("Query").Range("A3:K1000").Value2

what could be the best way of keeping the leading zeros?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try
Code:
Sheets("List").Range("A3:K1000").Value2 = Sheets("Query").Range("A3:K1000").[COLOR=#0000ff]Text[/COLOR]
 
Upvote 0
I tried a few variations of .value .value2 & .text

.text does not move any data across
 
Upvote 0
a bit of further testing

Code:
Sheets("List").Range("A4").Value2 = Sheets("Query").Range("A4").Text

works as expected, but

Code:
Sheets("List").Range("A4:K4").Value2 = Sheets("Query").Range("A4:K4").Text

doesn't move any data
 
Upvote 0
is anyone able to offer any further help on this one?
 
Upvote 0
You can't use Text on a multi-cell range (unless they all happen to contain the same displayed value).

Assuming the source cells actually contain the leading 0s rather than just being formatted to show them, you could set the target range number format to text:

Code:
With Sheets("List").Range("A3:K1000")
.Numberformat = "@"
.Value2 = Sheets("Query").Range("A3:K1000").Value2
end with
 
Upvote 0
Solution
Thank you very much - Worked perfectly.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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