Solved

vba mid function weird error

Posted on 2011-09-06
2
371 Views
Last Modified: 2012-05-12
Hi,

So I'm trying to get the last 9 numbers from an integer by using the mid function.
In this example It's to calculate a French TVA number:

Private Sub TextBoxBtw_Change()
    CommandButtonBtw.Enabled = False
    
    Dim TrimmedResult As String
    Dim IsResultNumeric As Boolean
    
    TrimmedResult = Replace(TextBoxBtw.Text, " ", "")
    TrimmedResult = Replace(UCase(TrimmedResult), "FR", "")
    IsResultNumeric = IsNumeric(TrimmedResult)
    If Len(TrimmedResult) = 11 And IsResultNumeric = True Then
        Dim controlNumber As Integer
        Dim sirenNumber As Integer
        
        controlNumber = Mid(TrimmedResult, 1, 2)
        sirenNumber = Mid(TrimmedResult, 3)
        If controlNumber = (12 + 3 * (sirenNumber Mod 97)) Mod 97 Then
            CommandButtonBtw.Enabled = True
        End If
    End If
End Sub

Open in new window


But for some reason it gives me a "loop error?" on sirenNumber = Mid(TrimmedResult, 3)

The weird part is when I type 6 instead of 3 it works and give me the last 5 numbers.
BUT when I type for example Mid(TrimmedResult, 3, 5) which would give me also 5 numbers it doesnt work... only 4 in length work when I indicate the length.

Why oh why does vba hate me?
Any ideas?
0
Comment
Question by:Mutsop
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 36487466
The number is too large for an integer. Change this line:

Dim sirenNumber As Integer

to this:

Dim sirenNumber As Long

0
 
LVL 3

Author Closing Comment

by:Mutsop
ID: 36487490
Offcourse around 65000 max for an integer.... Forgot about that.
Sigh :D

Thanks alot
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now