How to remove run-time error '457' in my PP macro?

Posted on 2011-10-20
Last Modified: 2012-05-12
Hi experts,
I need your help to eliminate errors in a macro that I use. My macro reports an error when running on 29 row my code:
If ITM <> "" Then dict.Add ITM, ITM
For better orientation, attach a printscreen error, my project where I need to generate random numbers without repetition.
Thank you in advance for any help,
Run-time Error test-random-number.ppt
Function getnum2() As Long
Dim lngLow As Long
Dim lngHigh As Long
Dim lngNum As Long
Static dict As Object
Dim fso As Object
Dim readFile As Object
Dim strFileText As String
Dim arr As Variant
Dim itm As Variant
Const strFileName As String = "c:\deleteme\somefile4.txt"

    Randomize Now()
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FileExists(strFileName) Then
        fso.createTextFile strFileName, False
    End If
    If dict Is Nothing Then
        Set dict = CreateObject("scripting.dictionary")
        Set readFile = fso.OpenTextFile(strFileName, 1, False)
        If readFile.atendofstream Then
            strFileText = readFile.ReadAll
            arr = Split(strFileText, vbCrLf)
            For Each itm In arr
                If itm <> "" Then dict.Add itm, itm
        End If
    End If
    lngLow = 200
    lngHigh = 45000000
    lngNum = CLng((lngHigh - lngLow + 1) * Rnd() + lngLow)
    Do While dict.exists(lngNum)
        lngNum = CLng((lngHigh - lngLow + 1) * Rnd() + lngLow)
    dict.Add lngNum, lngNum
    Set readFile = fso.OpenTextFile(strFileName, 8, False)
    readFile.WriteLine (lngNum)
    getnum2 = lngNum

End Function

Open in new window

Question by:Thomas_Meyer
    LVL 4

    Accepted Solution

    Hi Thomas,

    I think you are experiencing this error because the value are writing into your somefile4.txt is not actually random. So when you run the script again and it gets to line 29 you are entering the same item into the dict dictionary twice and this is not allowed.

    I am not sure if this will work for you, but try adding the following code and seeing if your script completes.

    For Each itm In arr
    	If itm <> "" Then
    		Count = Count + 1
    		dict.Add Count, itm
    	End If

    Open in new window


    Author Closing Comment

    Perfect, the error seems to be removed.
    Thank you for your help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
    Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
    This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
    This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now