Set range to value without re-formatting cell

2019-07-14 04:18发布

I have a range of numbers with some / in it that I am trying to copy to another range:

...
152/7163
152/7163
153/7163
153/7163
48/7164
332/7138
332/7138
332/7138
371/7166
5/7161
5/7161
5/7161
5/7161
115/7163
116/7163
121/7163
519/7132
154/7163
...

However, when I do so, some of those get formatted to a date (i.e. 5/7161 becomes May-61. Note that in the transfer to my importWS, Excel added 5/1/7161 on its own...then turned that into May-61. If I change it to Number then the cell changes its value completely, to 1921663.00. How do I prevent that from happening?

I'm copying from ws and copying to importWS:

    importWS.range(importWS.cells(2,4),importWS.cells(lastRow,4)).Value = _
ws.Range(ws.cells(2,7),ws.cells(lastRow,7)).Value

I've tried using .Value2 and .Text and neither work. I know I could loop through each cell in the range, and do like importWS.cells(i,4).Value = "'" & ws.cells(i,7).Value but would rather not, since I'm using the above range method to do this for multiple columns.

1条回答
smile是对你的礼貌
2楼-- · 2019-07-14 04:54

Preset the Range.NumberFormat property to Text then read the Range.Value2 property from the source.

with importWS
    .range(.cells(2, 4), .cells(lastRow, 4)).NumberFormat = "@"
    .range(.cells(2, 4), .cells(lastRow, 4)) = _
        ws.Range(ws.cells(2, 7), ws.cells(lastRow, 7)).value2
end with
查看更多
登录 后发表回答