热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将数字保存为文本,如何自动化-Savingnumberastext,howtoautomateit

Ihavea7702216772numberinsideacell.IfIputabeforethefistdigitandclickEnterExcelt

I have a 7702216772 number inside a cell. If I put a ' before the fist digit and click Enter Excel transforms the number to a text and puts a green triangle at the left top of the cell:

我在一个单元格中有一个7702216772号码。如果我在第一个数字之前输入一个并单击Enter Excel将数字转换为文本并在单元格的左上角放置一个绿色三角形:

enter image description here

I have many rows of similar numbers all of which need to be transformed into text. However clicking each and adding ' before the first symbol and clicking Enter would take a lot of time. Is there any way to do it programatically?

我有许多相似数字的行,所有这些都需要转换成文本。但是,单击每个并在第一个符号之前添加'并单击Enter将花费大量时间。有没有办法以编程方式进行?

I tried using formula: ="'"&H4 but it doesn't do what's expected - the green triangle never appears on the result cell.

我尝试使用公式:=“'”&H4但它没有做到预期的结果 - 绿色三角形永远不会出现在结果单元格中。

I also tried setting cell format to Text, but the green triangle doesn't appear in that case too.

我也尝试将单元格格式设置为Text,但在这种情况下也不会出现绿色三角形。

I need the green triangle to appear at the upper left corner, just like at the picture!

我需要绿色三角形出现在左上角,就像在图片中一样!

4 个解决方案

#1


2  

If all your number are in a single column, the following code will do it:

如果您的所有号码都在一列中,则以下代码将执行此操作:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A

For i = 1 To LastRow 'loop from row 1 to last
    ws.Cells(i, "A").Value = "'" & ws.Cells(i, "A").Value 'add the ' before the number
Next i
End Sub

Change the "A" to whichever column you are using.

将“A”更改为您使用的任何列。

#2


1  

Non VBA answer; I'm using Column G in this answer but it depends on where your numbers are. You'll have to change the cell but I think you will be ok with this.

非VBA答案;我在这个答案中使用了G列,但这取决于你的数字在哪里。你必须改变细胞,但我想你会对此感到满意。

  • In an empty cell, enter formula: ="'"&G4
  • 在空单元格中,输入公式:=“'”&G4

  • Use the fill handle or Ctrl+D to fill it down to the length of Column G's values.
  • 使用填充柄或Ctrl + D将其填充到列G值的长度。

  • Select the whole of Column G's values and copy them to the clipboard
  • 选择整个Column G的值并将它们复制到剪贴板

  • Select the same range in Column G, right-click, select Paste Special and choose Values
  • 在G列中选择相同的范围,右键单击,选择“选择性粘贴”并选择“值”

#3


0  

Just Select the cells you wish to process and run this short macro:

只需选择您要处理的单元格并运行此短宏:

Sub Textify()
    Dim rng As Range, r As Range
    Set rng = Selection.Cells.SpecialCells(2, 1)
    For Each r In rng
        r.Value = "'" & r.Value
    Next r
End Sub

#4


0  

I have tested it now for several times and it worked always

我现在已经多次测试它并且它始终有效

Cells(xx, xx).FormulaR1C1 = "'" & Cells(xx, xx).Value

Same would work for ActiveCell or whatever you like.

同样适用于ActiveCell或任何你喜欢的。


推荐阅读
author-avatar
比熊--悉尼
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有