+ Reply to Thread
Results 1 to 6 of 6

Macro Opening Wrong File - Code Attached

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    29

    Macro Opening Wrong File - Code Attached

    Hello,

    I have a macro that keeps wanting to open version 4 of a file (the one I created it in), even when I have version 5 open.

    If you could take a quick peak at my code and tell me what's going on, I would appreciate it!

    See attached.

    Thanks in advance!
    Justin
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    06-17-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    29

    Re: Macro Opening Wrong File - Code Attached

    Hey I'm new to this macro thing. Should I post the code here rather than the file so that no one is afraid of me sending a virus or something through the code?

  3. #3
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Re: Macro Opening Wrong File - Code Attached

    Hi,
    Your file ThisWorkbook part includes that code;
    Sub dashboard()
    
    Dim lr1 As Integer, lr2 As Integer, lr3 As Integer, x As Integer, r As Integer, c As Integer, p As Integer, m As Integer, rg As Integer
    
    Application.ScreenUpdating = False
    
    'clear contents on sheet(2)
    lr1 = Sheets(2).Range("b5000").End(xlUp).Row + 1
    Sheets(2).Range("A2:I" & lr1).ClearContents
    Sheets(2).Range("J2").End(xlDown).ClearContents
    
    'detemine how many months of info to collect
    x = InputBox("How many months of data?")
    
    'plants
    For r = 6 To 27
        lr3 = Sheets(2).Cells(5000, 1).End(xlUp).Offset(1, 0).Row
        For rg = lr3 To lr3 + x - 1
            Sheets(2).Cells(rg, 1) = Sheets(1).Cells(r, 1).Value
        Next rg
    Next r
    
    For r = 6 To 27
        lr2 = Sheets(2).Cells(5000, 2).End(xlUp).Offset(1, 0).Row
        For p = lr2 To lr2 + x - 1
            Sheets(2).Cells(p, 2) = Sheets(1).Cells(r, 2).Value
        Next p
    Next r
        
    
    For r = 6 To 27
            
            'months
            For m = 4 To (x * 6) + (4 - 6) Step 6
                lr = Sheets(2).Cells(5000, 3).End(xlUp).Offset(1, 0).Row
                Sheets(2).Cells(lr, 3) = Sheets(1).Cells(r, m).Offset((r * -1) + 2, 0).Value
            Next m
            
            'pounds loss/gained
            For f1 = 4 To (x * 6) + (4 - 6) Step 6
                lr = Sheets(2).Cells(5000, 4).End(xlUp).Offset(1, 0).Row
                Sheets(2).Cells(lr, 4) = Sheets(1).Cells(r, f1).Value
            Next f1
        
            'monthly usage
            For f2 = 5 To (x * 6) + (5 - 6) Step 6
                lr = Sheets(2).Cells(5000, 5).End(xlUp).Offset(1, 0).Row
                Sheets(2).Cells(lr, 5) = Sheets(1).Cells(r, f2).Value
            Next f2
            
            'scrap rate
            For f3 = 6 To (x * 6) + (6 - 6) Step 6
                lr = Sheets(2).Cells(5000, 6).End(xlUp).Offset(1, 0).Row
                Sheets(2).Cells(lr, 6) = Sheets(1).Cells(r, f3).Value * -1
            Next f3
            
            'pounds scrap sold
            'For f4 = 7 To (x * 6) + (f4 - 5) Step 6
                'lr = Sheets(2).Cells(5000, 7).End(xlUp).Offset(1, 0).Row
               ' Sheets(2).Cells(lr, 7) = Sheets(1).Cells(r, f4).Value
            'Next f4
            
            'avg std cost
            For f5 = 8 To (x * 6) + (8 - 6) Step 6
                lr = Sheets(2).Cells(5000, 8).End(xlUp).Offset(1, 0).Row
                Sheets(2).Cells(lr, 8) = Sheets(1).Cells(r, f5).Value
            Next f5
            
            '$ amount
            For f6 = 9 To (x * 6) + (9 - 6) Step 6
                lr = Sheets(2).Cells(5000, 9).End(xlUp).Offset(1, 0).Row
                Sheets(2).Cells(lr, 9) = Sheets(1).Cells(r, f6).Value
            Next f6
              
    Next r
    
    For Each pivot In Sheets(2).PivotTables
        pivot.RefreshTable
        pivot.Update
    Next
    
    
    Sheets(2).Range("J2").Copy Sheets(2).Range("J3:J" & lr1)
    
    Application.ScreenUpdating = True
            
    End Sub
    Even you delete that code your pivottable and pivotcharts works properly...

    What is your question clearly ?
    Sub DontForgetThese()
         If Your thread includes any code Then Please use code tags...
         If Your thread has been solved Then Please mark as solved...
         If Anybody has helped to you Then Please add reputation...
    End Sub

  4. #4
    Registered User
    Join Date
    06-17-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    29

    Re: Macro Opening Wrong File - Code Attached

    Hi Herry,

    Thanks for the response!

    For whatever reason, when I open the file I attached earlier in the thread, the macro automatically opens another file on my hard drive and runs the macro in that file....and I can't figure out why its doing that.
    Any suggestions?

    Thanks!
    Justin

  5. #5
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Re: Macro Opening Wrong File - Code Attached

    I cant find any reason why it is doing that...
    Are you sure attached file includes everything?
    Last edited by HerryMarkowitz; 09-18-2014 at 06:36 PM.

  6. #6
    Registered User
    Join Date
    06-17-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    29

    Re: Macro Opening Wrong File - Code Attached

    Hey I just discovered the problem! When I run it from the vba window with f5, it works. I had been trying to run it from the "CKS Macros" tab in the ribbon. That's when it wants to open the other file...odd.

    Maybe if you could help me with that, that would be great. Otherwise, no problem...I'll mark it solved.

    Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Autofilter Macro Failing - Run-time error 91 - full macro code is attached
    By evamarie in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-07-2013, 02:36 PM
  2. code error while selecting a range-file attached
    By SMILE in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-26-2011, 06:02 AM
  3. macro code to change file-opening password
    By arunjoshi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-16-2005, 03:49 AM
  4. Hyperlink; wrong file opening
    By Optiker in forum Excel General
    Replies: 0
    Last Post: 05-10-2005, 12:29 PM
  5. Code to select 'Disable Macro' when opening an EXTERNAL FILE
    By G rumpy O ld D uffer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-22-2005, 02:06 PM

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