简体   繁体   English

将单元格值用作字符串VBA

[英]Use cell value as string VBA

Cell A2 contains value "25-86". 单元格A2包含值“25-86”。

I need cell B2 to have value "AJ 2586%" 我需要单元格B2的价值“AJ 2586%”

This code reveals Syntax error: 此代码显示语法错误:

Range("B2").Value = "AJ & Left(Range("A2"), 2) & Right(Range("A2"), 2) & %"

If I write it like that 如果我这样写的话

Range("B2").Value = "AJ & Left(A2, 2) & Right(A2, 2) & %"

The functions Left and Right treat "A2" as string. LeftRight功能将“A2”视为字符串。

How could I extract a part of text from the cell and enter it in another cell? 如何从单元格中提取部分文本并将其输入另一个单元格?

The issue is that everything between the " " defines a string. 问题是“”之间的所有内容都定义了一个字符串。 You can type your code like this: 您可以像这样输入代码:

Range("B2").Value = "AJ " & Left(Range("A2"), 2) & Right(Range("A2"), 2) & "%"

The following formula will return the value as a real percentage that you can use in calculations. 以下公式将值作为可在计算中使用的实际百分比返回。
It just removes the - from the figure. 它只是从图中删除了-

=SUBSTITUTE(A2,"-","")/100  

Initially it will display the value as 25.86 . 最初它将显示值为25.86

When you apply the custom number format of: 当您应用自定义数字格式

\AJ 0%  

It will then display as AJ 2586% . 然后它将显示为AJ 2586%

If that formula is in A1 then =A1/2 will return AJ 1293% . 如果该公式在A1中,那么=A1/2将返回AJ 1293%

Edit: 编辑:
Just realised you want it in VBA: 刚刚意识到你想要它在VBA中:

Range("B2") = Replace(Range("A2"), "-", "") / 100
Range("B2").NumberFormat = "\AJ 0%"

Or, if you just want it as text: 或者,如果您只想将其作为文本:

Range("B2") = "AJ " & Replace(Range("A2"), "-", "") & "%"
Range("B2").Value = "AJ " + Left(Range("A2"), 2) + Right(Range("A2"), 2) + "%"

缩进代码4空格,以使其格式正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM