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

VBAifelse在excel中循环返回#VALUE-VBAifelseloopinexcelreturning#VALUE

HiAllinnewtousingVBAinexcelandwastryingtocreateafunctionthatwouldlookatanumber

Hi All in new to using VBA in excel and was trying to create a function that would look at a number and return it as a six digit number.

嗨所有新的在excel中使用VBA并尝试创建一个函数,它将查看一个数字并将其作为六位数返回。

The function I wrote to try and accomplish this is below but when I use the command =Res(A1) in a cell I just get a #VALUE! as the answer.

我写的试图完成这个的功能如下,但是当我在单元格中使用命令= Res(A1)时,我得到一个#VALUE!作为答案。

The value in cell one at the moment is 30508.

目前单元格1中的值为30508。

Any help anyone could offer to resolve this would be greatly appreciated. Thanks Guys.

任何人都可以提供解决方案的任何帮助将不胜感激。多谢你们。

Function Res(myval As Integer) As Integer

Res = 0

If ((myval > 0) And (myval <10)) Then    
    Res = myval * 100000

ElseIf ((myval > 9) And (myval <100)) Then
    Res = myval * 10000

ElseIf ((myval > 99) And (myval <1000)) Then 
    Res = myval * 1000

ElseIf ((myval > 999) And (myval <10000)) Then  
    Res = myval * 100

ElseIf ((myval > 9999) And (myval <100000)) Then
    Res = myval * 10

ElseIf ((myval > 999999) And (myval <10000000)) Then
    Res = myval / 10

Else
    Res = myval

End If

End Function

3 个解决方案

#1


9  

Change Function Res(myval As Integer) As Integer to:

将函数Res(myval As Integer)更改为Integer:

Function Res(myval As Long) As Long

You're hitting the integer maximum.

你正在达到整数最大值。

Long stands for long integer, and you want to use it anytime your number could go above 30k.

Long代表长整数,你想在任何时候你的数字超过30k就可以使用它。

#2


2  

You need to use long instead of integer

你需要使用long而不是整数

integer only covers from -32,768 to 32,767

整数仅涵盖-32,768至32,767

long covers -2,147,483,648 to 2,147,483,647

长封面-2,147,483,648至2,147,483,647

    Function Res(myval As Long) As Long

    Res = 0

    If ((myval > 0) And (myval <10)) Then    
       Res = myval * 100000

    ElseIf ((myval > 9) And (myval <100)) Then
        Res = myval * 10000

    ElseIf ((myval > 99) And (myval <1000)) Then 
        Res = myval * 1000

    ElseIf ((myval > 999) And (myval <10000)) Then  
        Res = myval * 100

    ElseIf ((myval > 9999) And (myval <100000)) Then
        Res = myval * 10

    ElseIf ((myval > 999999) And (myval <10000000)) Then
        Res = myval / 10

    Else
        Res = myval

    End If

    End Function

#3


1  

Function Res(myVal as long) As Long
    Res = myVal * 10 ^ ( 6 - Len(Cstr(myVal)) )
End Function

Note - this still suffers from the same problem as the original approach:

注意 - 这仍然遇到与原始方法相同的问题:

If myVal is 9999999 (7 digits) then myVal/10 is 999999.9 and Clng(999999.9) is 10000000 (still seven digits)

如果myVal是9999999(7位),那么myVal / 10是999999.9而Clng(999999.9)是10000000(仍然是7位数)


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