vba 在excel中将值从厘米转换为英寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15704617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:19:54  来源:igfitidea点击:

Convert a value from cm to inch in excel

excelexcel-vbavba

提问by Adige72

A number in centimeter format can be converted to inch using this function:

可以使用此函数将厘米格式的数字转换为英寸:

=CONVERT(A1,"cm","in")

What i want is that instead of replacing the number, putting the converted number next to original one. Actually it's a more complicated situation:

我想要的是,不是替换数字,而是将转换后的数字放在原始数字旁边。其实这是一个更复杂的情况:

Original cell:

原始单元格:

35 - 45 cm

After convertion (all in one cell):

转换后(全部在一个单元格中):

35 - 45 cm
(14 - 18 inch)

So, how to do this in excel in an automated way since there are many cells in this format, maybe using a macro?

那么,由于这种格式有很多单元格,可能使用宏,因此如何以自动化方式在 excel 中执行此操作?

采纳答案by user2140261

I will start with this but I Suggest providing me more information so I can better Solve your problem:

我将从这个开始,但我建议为我提供更多信息,以便我可以更好地解决您的问题:

Sub AddConversionToCell()

    FirstNumberAsIn = WorksheetFunction.Convert(Left(Range("A1").Value, 2), "cm", "in")
    SecondNumberAsIn = WorksheetFunction.Convert(Mid(Range("A1").Value, 6, 2), "cm", "in")
    Range("A1").Value = Range("A1").Value & " ( " & FirstNumberAsIn & " - " & SecondNumberAsIn & " inch)"

End Sub

回答by Tim Williams

Formula approach - if your values start in A1 then place this in B1 and fill down.

公式方法 - 如果您的值从 A1 开始,则将其放在 B1 中并向下填充。

= A1 & CHAR(10) & "(" & ROUND(CONVERT(
VALUE(LEFT(A1,SEARCH("-",A1)-1)),"cm","in"),1) & " - " &
ROUND(CONVERT(
VALUE(MID(SUBSTITUTE(A1,"cm",""), SEARCH("-",A1)+1,10)),"cm","in"),1) & "in)"