+ Reply to Thread
Results 1 to 5 of 5

MACRO In a never ending loop

Hybrid View

madbrit MACRO In a never ending loop 03-18-2012, 05:41 PM
Richard Buttrey Re: MACRO In a never ending... 03-18-2012, 06:32 PM
royUK Re: MACRO In a never ending... 03-19-2012, 02:37 AM
madbrit Re: MACRO In a never ending... 03-19-2012, 09:38 AM
royUK Re: MACRO In a never ending... 03-19-2012, 09:44 AM
  1. #1
    Registered User
    Join Date
    03-05-2012
    Location
    Bath UK
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    20

    MACRO In a never ending loop

    Someone very kindly gave me this macro
    It works great for what I want, but if i enter a job number that does not exsist in the directory it goes into a never ending loop

    Can someoone help, I need to error trap this macro so if i enter a number that does not exsist in the directory it will put up a message box telling me, and exit the Macro, or ask me to put a correct number in instead of going into a loop looking for a non exsisting file.

    Thanks

    MadBRiT


    Sub Replace_Job_Number()
    
        Dim rngOld As Range, rngNew As Range
        
        Set rngOld = Range("A1")   'old job number
        Set rngNew = Range("A2")   'new job number
        
        If rngOld = "" Or rngNew = "" Then
        
            MsgBox "Missing job number. ", , "Cannot Update Links"
    
        Else
    
            Cells.Replace What:=rngOld.Value & ".xls", _
                          Replacement:=rngNew.Value & ".xls", _
                          LookAt:=xlPart, _
                          SearchOrder:=xlByRows, _
                          MatchCase:=False
        
            rngOld.Value = rngNew.Value
            rngNew.ClearContents
            
        End If
    
    End Sub
    Last edited by madbrit; 03-18-2012 at 06:56 PM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: MACRO In a never ending loop HELP

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: MACRO In a never ending loop

    Give more details on what you are oing
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    03-05-2012
    Location
    Bath UK
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    20

    Re: MACRO In a never ending loop

    I have 3 people going out getting jobs for me
    when they come back they have an excel sheet with the quote data in cells B14:L39

    I save these Excel workbooks as a job No's like.... 1111

    At the moment I cut and paste B14:L39 into my Workboox sheet, it then calculates the sizes i have to cut things to, what length of material I have to order, and various other variables.

    What I am trying to do is one of 2 things

    First a MACRO, that when i start it up it askes me for the job No, it then finds it and places the data I want from that workbook (B14:L39)
    into my Worksheet.

    The other thing I have been toying with is opening the quote sheet and it Auto places the quote data ( B14:L39)
    into my sheet, I have taken one line of this formula and pasted it below.

    =SUM('[1111.xls]QUOTE SHEET'!$B$14)

    This works when I open the workbook No 1111 it transfers all data I want, BUT...

    How do I make the No 1111 variable, so If i put 2222 in cell A1 all the variables formulas will now look for 2222
    =SUM('[variable(A1).xls]QUOTE SHEET'!$B$14)

    Someone came up with this, and it works great, unless you know of a better way?

    Sub Replace_Job_Number()
    
        Dim rngOld As Range, rngNew As Range, strPath As String
        
        Set rngOld = Range("A1")    ' old job number
        Set rngNew = Range("A2")    ' new job number
        strPath = "C:\Test\"        ' Path where files are located
        
        If rngOld = "" Or rngNew = "" Then
        
            MsgBox "Missing job number. ", , "Cannot Update Links"
            
        Else
        
            If Len(Dir(strPath & rngNew & ".xls")) Then
    
                Cells.Replace What:=rngOld.Value & ".xls", _
                              Replacement:=rngNew.Value & ".xls", _
                              LookAt:=xlPart, _
                              SearchOrder:=xlByRows, _
                              MatchCase:=False
            
                rngOld.Value = rngNew.Value
                rngNew.ClearContents
            
            Else
                MsgBox "Cannot locate file: " & vbCr & vbCr & _
                strPath & rngNew.Value & ".xls", , "File Not Found"
            End If
        End If
    
    End Sub
    Thanks

    G

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: MACRO In a never ending loop

    I think an example workbook would help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1