将英寸转换为公制值

时间:2015-11-24 08:50:25

标签: excel vba

我尝试创建一个将英寸值转换为指标值的函数。

在细胞中有例如" 1/12英寸"。

我的功能到现在为止如下:

Public Function ConInToMe(inp As String) As Integer

    If InStr("inp, inch", ",") = 4 Then

        inp = Left(inp, Len(inp) - 4)
        inp = CInt(inp)
        ConInToMe = WorksheetFunction.MRound(inp / 25.4, 1 / 16)
        ActiveCell.NumberFormat = "# ??/??"
    Else
        MsgBox ("no inch value detected")
    End If

End Function

我删除子串" inch"转换价值。我总是在单元格中获得#VALUE。我做错了什么?有什么想法吗?

提前打招呼, yab86

3 个答案:

答案 0 :(得分:1)

如果您使用0.5 inch代替1/2 inch,则可以使用此功能。我想/会让转换加倍。

Public Function ConInToMe(inp As String) As Double

    Dim holder As Double

    If InStr(inp, "inch") > 0 Then
        inp = Left(inp, Len(inp) - 5)
        holder = inp
        ConInToMe = WorksheetFunction.MRound(holder * 25.4, 1 / 16)
        'Debug.Print ConInToMe
        'ActiveCell.NumberFormat = "# ??/??"
    Else
        MsgBox ("no inch value detected")
    End If

End Function

编辑:

它很难看,但适用于/

Public Function ConInToMe(inp As String) As Double

    Dim holder As Double

    Dim con1 As Double, con2 As Double

    If InStr(inp, "inch") > 0 Then

        If InStr(inp, "/") > 0 Then

        con1 = CDbl(Left(inp, InStr(inp, "/") - 1))
        con2 = CDbl(Mid(inp, InStr(inp, "/") + 1, InStr(inp, " ") - InStr(inp, "/")))

        holder = con1 / con2
        ConInToMe = WorksheetFunction.MRound(holder * 25.4, 1 / 16)

        Else

        inp = Left(inp, Len(inp) - 5)
        holder = inp
        ConInToMe = WorksheetFunction.MRound(holder * 25.4, 1 / 16)

        End If
    Else
        MsgBox ("no inch value detected")
    End If

End Function

答案 1 :(得分:0)

错误发生在inp = CInt(inp)

inp被声明为字符串,因此无法为其分配整数。

也许工作的新变量应该起作用

答案 2 :(得分:-1)

package test3;

import java.util.Scanner;

public class inchtometer {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("This Is Inch TO Meter Converter");

        float a;
        double b;
        Scanner mtr=new Scanner(System.in);
        System.out.println("Enter Inch Valuse You Can Covert Meter");

        a=mtr.nextFloat();
        mtr.close();
        b=25.4;
        double c=a*b;
        System.out.println("Meter Values Is ="+c);

    }

}

    enter code here