+ Reply to Thread
Results 1 to 14 of 14

Excel update causing Macro problem

  1. #1
    Registered User
    Join Date
    10-03-2023
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    5

    Excel update causing Macro problem

    Hi guys,

    A recent update to our Microsoft office from 2016 to 2021 has caused an issue with one of our Macros.


    What this macro is supposed to do is copy line 4 and paste it into line 5 for multiple files. It's still doing that but any line from lines 1 Through to 20 that has numeric values only is being shifted to the left and not lining up with anything else. This causes issues when trying to import it into our programs.

    We have tried contacting the original author of the macro and he is stumped. We have also changed a few things (starting line, finish line etc.) but we are definitely not advanced in this knowledge.

    Any help with what we can change here to make this work would be greatly appreciated.

    I have attached a picture and highlighted the changes causing problems as well as added the code below.
    Cheers!!

    Please Login or Register  to view this content.
    Attached Images Attached Images
    Last edited by StEv3-o; 10-03-2023 at 03:25 AM.

  2. #2
    Registered User
    Join Date
    01-21-2023
    Location
    South Africa
    MS-Off Ver
    365
    Posts
    1

    Re: Excel update causing Macro problem

    Hi all, how come the macros only works on the desktop and not on the sharedrive

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Excel update causing Macro problem

    The code is a bit clunky and looks like it was based on the macro recorder so it's a little hard to unravel in the abstract. Is there any possibility of providing sample data files so that we can run this code and reproduce the results?

    It is possible that the Excel upgrade is a coincidence and something else has changed in your environment or data files to cause this. There is rarely impact to VBA from one version to the next.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Excel update causing Macro problem

    Quote Originally Posted by Jaspermuduvhadzi View Post
    Hi all, how come the macros only works on the desktop and not on the sharedrive
    Administrative Note:

    Welcome to the forum Jaspermuduvhadzi.

    We are happy to help, however it appears that your question is not related to this thread.

    Please see Forum Rule #1 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

  5. #5
    Registered User
    Join Date
    10-03-2023
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    5

    Re: Excel update causing Macro problem

    Quote Originally Posted by 6StringJazzer View Post
    The code is a bit clunky and looks like it was based on the macro recorder so it's a little hard to unravel in the abstract. Is there any possibility of providing sample data files so that we can run this code and reproduce the results?

    It is possible that the Excel upgrade is a coincidence and something else has changed in your environment or data files to cause this. There is rarely impact to VBA from one version to the next.
    Hi 6StringJazzer,

    Thanks for your reply! We have also upgraded from windows 7 to windows 10 so a definite change in environment there. Not sure if anything else has been changed by IT in the background but have sent them an e-mail to find out.

    I have attached some sample files to try and help as well You will need to change the extension back to .nc1 for the Macro to work.

    Cheers.
    Attached Files Attached Files

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Excel update causing Macro problem

    Here is the problem, I don't have a solution yet.

    You are reading in a text file. The numeric values (starting on line 11) are right-justified by inserting spaces at the beginning. When Excel opens this file, those values are converted to numeric. Then when Excel writes them back, they are written with no justification, just the numbers. Excel does not know how to blank-pad numeric values for right-justification in a text file.

    One approach is to force Excel to read everything as text, instead of converting to numbers. I do not know how to do that, but it may be possible.

    Another option is to process the files as text files instead of opening them in Excel. This would require a total rewrite of your code.

    I'll see what else I can offer.

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Excel update causing Macro problem

    Also, this seems unnecessary
    Please Login or Register  to view this content.
    You are opening the same file twice, not sure why.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi, try this !


    Quote Originally Posted by StEv3-o View Post
    What this macro is supposed to do is copy line 4 and paste it into line 5
    Nope, your original code replaces line #6 with line #5 … So weird to use Excel just for that !

    If the author of the code did well understand what is a text file
    so he did not open it under Excel to modify it so your issue can't never arise !

    So removing the useless, according to your first text file attachment a VBA basics demonstration for starters :

    PHP Code: 
    Sub Demo1()
            
    Dim VD$, R%, W%, F$, L%, S$
            
    Set V CreateObject("Shell.Application").BrowseForFolder(0vbLf "Source folder :"1"")
             If 
    V Is Nothing Then Exit Sub Else V.Self.Path "\"
            D = V & "
    NC Edited\":  If Dir(D, 16) = "" Then MkDir D
            R = FreeFile
            W = R + 1
            F = Dir$(V & "
    *.NC")
      While F > ""
            Open V & F For Input As #R
            Open D & F For Output As #W
        For L = 1 To 5
            If EOF(R) Then Exit For
            Line Input #R, S
            Print #W, S
        Next
            If L = 6 Then Print #W, S: If Not EOF(R) Then Line Input #R, S
         Do Until EOF(R)
            Line Input #R, S
            Print #W, S
         Loop
            Close #W, #R
            F = Dir$
      Wend
            Application.Speech.Speak "
    Done!", True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  9. #9
    Registered User
    Join Date
    10-03-2023
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    5
    Quote Originally Posted by 6StringJazzer View Post
    Here is the problem, I don't have a solution yet.

    You are reading in a text file. The numeric values (starting on line 11) are right-justified by inserting spaces at the beginning. When Excel opens this file, those values are converted to numeric. Then when Excel writes them back, they are written with no justification, just the numbers. Excel does not know how to blank-pad numeric values for right-justification in a text file.

    One approach is to force Excel to read everything as text, instead of converting to numbers. I do not know how to do that, but it may be possible.

    Another option is to process the files as text files instead of opening them in Excel. This would require a total rewrite of your code.

    I'll see what else I can offer.
    Ahh ok yeah that makes sense. Thank you for looking into this it's much appreciated.

  10. #10
    Registered User
    Join Date
    10-03-2023
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    5
    Quote Originally Posted by Marc L View Post

    Nope, your original code replaces line #6 with line #5 … So weird to use Excel just for that !

    If the author of the code did well understand what is a text file
    so he did not open it under Excel to modify it so your issue can't never arise !

    So removing the useless, according to your first text file attachment a VBA basics demonstration for starters :

    PHP Code: 
    Sub Demo1()
            
    Dim VD$, R%, W%, F$, L%, S$
            
    Set V CreateObject("Shell.Application").BrowseForFolder(0vbLf "Source folder :"1"")
             If 
    V Is Nothing Then Exit Sub Else V.Self.Path "\"
            D = V & "
    NC Edited\":  If Dir(D, 16) = "" Then MkDir D
            R = FreeFile
            W = R + 1
            F = Dir$(V & "
    *.NC")
      While F > ""
            Open V & F For Input As #R
            Open D & F For Output As #W
        For L = 1 To 5
            If EOF(R) Then Exit For
            Line Input #R, S
            Print #W, S
        Next
            If L = 6 Then Print #W, S: If Not EOF(R) Then Line Input #R, S
         Do Until EOF(R)
            Line Input #R, S
            Print #W, S
         Loop
            Close #W, #R
            F = Dir$
      Wend
            Application.Speech.Speak "
    Done!", True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Hi Marc L,

    Haha yeah seems a little trivial to use excel for something so small but it saves us hours of time going through hundreds of files a day manually.
    Straight off the bat this code is alot faster which is exciting. I just can't seem to get it to copy the files into the edited folder.

    Cheers
    Last edited by StEv3-o; 10-04-2023 at 07:14 PM.

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Excel update causing Macro problem


    Quote Originally Posted by StEv3-o View Post
    Straight off the bat this code is alot faster
    Yes as just reading this code you must have seen Excel is not used ‼


    Quote Originally Posted by StEv3-o View Post
    I just can't seem to get it to copy the files into the edited folder.
    It is exactly what this VBA demonstration does, just reading the code, plain text written, like you can check within the subfolder !

  12. #12
    Registered User
    Join Date
    10-03-2023
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    5
    Quote Originally Posted by Marc L View Post

    Yes as just reading this code you must have seen Excel is not used ‼


    It is exactly what this VBA demonstration does, just reading the code, plain text written, like you can check within the subfolder !
    I'm not sure I'm understanding you. So do you run this code using something different to excel?

  13. #13
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Excel update causing Macro problem

    I think what Marc is trying to say is that his rewrite of code is just using VBA to read and write a text file, which you could do in any language. It is not using any Excel files or Excel features.

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Thumbs up Re: Excel update causing Macro problem


    That's it !

    No data uploaded within any Excel worksheet like if directly achieved under a VB or PowerShell script …

+ 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. [SOLVED] Macro Causing Excel to Restart
    By JBaum2000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-13-2020, 02:35 PM
  2. Macro keeps causing excel to freeze
    By tweacle in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-06-2019, 09:45 AM
  3. Macro causing excel to crash
    By bradleyandrewdavis in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 05-16-2015, 04:29 AM
  4. Replies: 11
    Last Post: 08-02-2014, 03:22 AM
  5. [SOLVED] Macro causing Excel to freeze
    By Sassyponypants in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-04-2013, 06:10 AM
  6. Problem with excel security settings causing UserInterFaceOnly to not work
    By gazza365 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-24-2011, 03:29 AM
  7. Excel 2003 - Update or Don't Update Links Problem
    By Jamie in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-07-2005, 10:05 AM

Tags for this Thread

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