+ Reply to Thread
Results 1 to 68 of 68

VBA to Transpose Rows to Columns on Multiple Worksheets

  1. #1
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    VBA to Transpose Rows to Columns on Multiple Worksheets

    Greetings All Experts,

    Attached documents refer... "OriginalFile" (prior to transpose), Macro Script (code below) and "Transposed File" which contain the results after running the VBA script).

    I need to transpose all values contained in Row 1 (the total columns vary from file to file - not fixed) on each worksheet into new tables named "Table1", "Table2", "Table3", etc. (the number of tables will depend on the number of worksheets, which can be anything between 3 to probably 6 worksheets). Once all worksheets has been transposed, the various Tables must be appended into 1 'Final' Table which I will then import and process into a MS Access database for further processing.

    My problem is to code (populate) the section/string specifying the "C1:" to "Last Column" values...these are the values that we (internally) refer to "Product Style Codes" and are listed in the "Formula:=" section in the code below. These "Product Style Code" values will differ from file/worksheet to file/worksheet.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    This is one way to do Step 1...You will need to explain the appending requirement in more detail...
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by sintek; 05-31-2022 at 06:41 AM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Thank you for your reply Sintek.

    Will apply your code and revert back shortly.

    Appreciated!

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Just noticed an error...I assume the value for Col D for each row will be taken from each individual header Column...
    See red snippet change above...

  5. #5
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Your correction is perfect, thank you...

  6. #6
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Sintek,

    How can I incorporate your transpose code in my existing code? (I think I am using late binding since I'm calling the procedure from MS Access). As you can see from my code below, I am already looping through each worksheet to do some formatting and filtering. Would you be able to slot your code between the comments section "Start Sintek Code" and "End Sintek Code"?

    I love the fact that you are not creating new 'Table' worksheets as per the "TransposedFile" example I originally sent.

    Please see attached file containing my current code.
    Attached Files Attached Files

  7. #7
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    I am very certain that code of yours can be streamlined...You are repeating steps based on wildcard filter limitation...
    Will only be able to confirm if I see sample file consisting of data prior to your code snippet...

    Also, WRT appending of tables...Is this just all the tables data combined below each other?

    Anyway, need to step out will check in later if you have uploaded a sample file with before | after scenario..
    Last edited by sintek; 05-31-2022 at 07:18 AM.

  8. #8
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Hi Sintek,

    Yeah, I'm forced to repeat the filtering due to the xlor limitation... posted another thread about it asking for an alternative solution; but was told about the filter limitation and hence I left it at that... but it's working at least :-)

    Attached, please find a sample file prior to ANY modification. I have deleted quite a few columns to reduce the filesize.

    THANK YOU ONCE AGAIN!
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Correct, so once all of the worksheets have been transposed, I would need to import each transposed worksheet into MS Access. I thought it would be 'easier' to do by 1st appending all transposed worksheets into one worksheet and then do the import into a MS Access table.

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    This...Is a mouthful...

    Edit changed red snippet...
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by sintek; 05-31-2022 at 11:40 AM.

  11. #11
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    This version will be faster...
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Evening Sintek,

    You have no idea how much I appreciate your efforts! i will test this 1st thing tomorrow morning and probably revert back with 10 stars (if I could). Did not expect from you to redesign all of it!! Have a great evening!! Chat tomorrow...

  13. #13
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Apologies for not getting back to you Sintek; got distracted with other issues. Will test your code and revert back soon.

  14. #14
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    This is absolutely amazing and superfast! You are nothing short of a genius sintek!

    What would be nice would be (but not a necessity) if, once the "Appended" worksheet has been created, to have the data sorted by the values in Column A ("Hdr1")and then by Column C ("Hdr3").

    My intention now is to use your code as a module within my MS Access Application and call this module with an Excel File, something like "Call 33v16("D:\PreProcessed\ABC.xlsx") where "ABC.xlsx" will be the pre-formatted file.

    Do you think it will work if I just add this to your code?

    Public Sub 33v16(sFile as String) ...instead of just having "Sub 33v16" ?

    Or will it require additional modifications? I'm thinking in terms of late- / early binding.

    Kind Regards

  15. #15
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    have the data sorted by the values in Column A ("Hdr1")and then by Column C ("Hdr3").
    Add red snippet...
    Please Login or Register  to view this content.
    Have no Access or ever needed use thereof...

  16. #16
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Sintec,

    So I created an MS Access module (using your code; see attached image) and tried to call the procedure with a filename: Call FormatMarkhamTest(ABC.xlsx)
    Getting a compile error "Invalid or unqualified reference" on "Application.DisplayAlerts = False". Any ideas?



    Your time and efforts appreciated...
    Attached Images Attached Images

  17. #17
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Cannot assist unfortunately...
    Have no Access or ever needed use thereof...
    Time for a new thread...
    Last edited by sintek; 06-02-2022 at 08:04 AM.

  18. #18
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Perfect!

    I will provide all credit to you in our application; promise!

    Just one last request: Once the sorting by Column A and Column C executed; would you be so kind as to add code to delete all rows containing "-" values (on the final "Appended" worksheet? These "-" values indicates that there were no sales and these rows can actually be deleted.

    Only if you have time, not really important, but it would assist in 'cleaning up' irrelevant data.

    Once again, your assistance is immeasurable and will save users a huge amount of time.

    I will create a new thread related to the error I'm encountering. I believe the error relates to the fact that I need to declare objects to use your code within MS Access.

    May I have your permission to copy your code in the new thread?

    Regards
    Last edited by JohanGduToit; 06-02-2022 at 08:19 AM.

  19. #19
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    would you be so kind as to add code to delete all rows containing "-" values (on the final "Appended" worksheet? These "-" values indicates that there were no sales and these rows can actually be deleted.
    Is better to just not add them at the initial loop...see red snippet change...

    Also...Why not just place this code in a Access module...
    Please Login or Register  to view this content.
    Last edited by sintek; 06-03-2022 at 05:30 AM.

  20. #20
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Cool, actually not a bad idea... will try and revert back shortly

  21. #21
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    If you want to save and close ...just add this before the last End With...
    Please Login or Register  to view this content.
    Also...depending on speed of your pc...you might have to add a pause to allow excel to open...

    Please Login or Register  to view this content.
    See amended code above...
    Last edited by sintek; 06-02-2022 at 08:37 AM.

  22. #22
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Works like a charm! "-" entries are all gone, and in your code

    Set wb = Workbooks.Open("E:\Steven\Desktop\Tester1.xlsx")

    I replaced your path above with the "sFile" value from where I call your sub.

    So the MS Access Application now opens Excel and the specified file, runs your code perfectly; but I have to save and close Excel afterwards to enable my code to continue execution. My original code does the formatting without 'visually' opening Excel and the file being processed; it all happens 'in memory'. So the users does not see the formatting happening in the background.

  23. #23
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    My original code does the formatting without 'visually' opening Excel and the file being processed; it all happens 'in memory'. So the users does not see the formatting happening in the background.
    Keep as is and just remove this snippet...
    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Well well well; absolutely fantastic!!

    Works 100% and exactly what I wanted Sintek.

    You are a miracle worker, all kudo's to you. Do not know how to thank you enough!!

    Won't be starting a new thread; problem solved... more than I originally asked for. Forever gratefull.

  25. #25
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    ..........................
    Thanks.png

  26. #26
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    The .Application.Quit statement closes the MS Access application as well....

  27. #27
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Should not as it is within the With statement...Cannot test my code unfortunately...No Access...
    Must have the red dot...
    Please Login or Register  to view this content.
    Last edited by sintek; 06-02-2022 at 09:32 AM.

  28. #28
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Morning Sintek,

    Me bad... left out the "." before "Application.Quit"

    Working now...

    For some reason your code is failing on two of the files I tried to process...

    On the one file the code just hangs and I cannot see the error. On the other I get a Runtime Error 1004 (Application Defined or Object Defined Error) on line ".Cells(2,1).Range(x,4).Value = Temp"

    Unfortunately it seems that I am unable to attached the 2 .zip files (Get a message that the website is unavailable).

    The one file is 3.9MB and the other is 4.2MB ...is there an alternative way that I can send these two files to you to investigate?

    Regards

  29. #29
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Hi Johan...

    Try saving as binary to reduce file size..

  30. #30
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Saved to binary; but still getting "This site can't be reached" when trying to attach files.

    Binary file sizes are 2.547kb and 1.934kb respectively.
    Last edited by JohanGduToit; 06-03-2022 at 04:27 AM.

  31. #31
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    As one of the sheets within the file (WK 10) has no quantities for the relevant outlets...Add the following snippet...
    Not sure what must happen to that sheet
    Please Login or Register  to view this content.
    WK 9 runs fine...

    Have updated code in Post 19 so you have all changes made...
    Last edited by sintek; 06-03-2022 at 05:30 AM.

  32. #32
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Hi,

    Thank you, will test quickly...

    If no quantities in a worksheet it can just be ignored...final result should only include rows where quantities/values > 0 (or <> "-"

    Will also check WK 9 again.

  33. #33
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Hi,

    Changing the Evaluate function unfortunately did not resolve the mentioned issue, nor the Wait time > 4 seconds. But it's working for all the other files :-)

    Not to worry about it... we can work around this!

    Thank you once again, immensely appreciated.

  34. #34
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    What error is it throwing...Step through the code and see if any...

    Add the original line...
    Please Login or Register  to view this content.
    and step through via F8
    Last edited by sintek; 06-03-2022 at 06:06 AM.

  35. #35
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    I'm not going to bother any further...tried stepping through; but don't have the time; takes forever :-)

    If we do encounter an error I'll let you know...all good for now!

    Thank you very very much.

  36. #36
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Morning Sintek,

    Trust you are well?

    I have been running into a problem when trying to import the 'reformatted' file output that your code generate into Access. I know that by removing all worksheets except for the worksheet named "Appended" will resolve my problem.

    To put it another way, I am only interested in processing the data contained in the "Appended" worksheet.

    May I ask you to add the relevant code to accomplish this to the code you provided?

    Many Thanks...your code below:

    Please Login or Register  to view this content.

  37. #37
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    I know that by removing all worksheets except for the worksheet named "Appended" will resolve my problem.
    Please Login or Register  to view this content.

  38. #38
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Hi "sintek",

    Thank you, just added the code below before the wb.Save and wb.Close statements and it worked...

    Please Login or Register  to view this content.

  39. #39
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    So I wasted my time???
    The code in 37 is shortened...Removed quarter of code as it was no longer needed...

  40. #40
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Apologies!!!

    Only saw your filepath highlighted in red and assumed your copy statement "I know that by removing all worksheets except for the worksheet named "Appended" will resolve my problem." implied that I should try and resolve it myself... :-) Did not realize you removed some of your original code.

    Your shortened code works, as expected, 100% and is a much better solution.

    Apologies once again for misinterpreting your reply...and thank you, thank you!! Appreciated!

  41. #41
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    I am now able to process the final product into my MS Access application successfully :-)

  42. #42
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Yipppeeee...Happy coding...

  43. #43
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Afternoon Sintek,

    Trust you are well?

    There's something funny going on when executing your code on more than one Excel file... If I run it a 1st time on say FileNameNo1 it runs perfectly. If I run it again straight afterwards with either the same FileNameNo1 (or any other file for that matter, the code crashes with Run time Error '462' ("The remote server machine does not exist or is unavailable" and it seems to happen on the line:

    With wb.Sheets.Add(,Sheets(Sheets.Count)): .Name = "Appended": End With

    What I do find strange is that if I close my application after the 1st (always successfull) run and re-open the application again and do a 2nd file, then the 2nd file will process without any errors. So it seems by closing the application after each processed file eliminates any errors. It's almost as if by closing the application; certain parameters or variables are being resetted (which doesn't happen if I try to process a 2nd file after the 1st successful run. Some assigned value remain in memory somewhere which causes the program to crash when processing consecutive files.

    Any idea's??

  44. #44
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    So I am assuming that you are not using this line...

    Please Login or Register  to view this content.
    Try this...
    Please Login or Register  to view this content.
    Also, as mentioned multiple times previously, I do not have Access so cannot test...
    Perhaps you can start a new thread and ensure someone has Access to test your requirement...
    Last edited by sintek; 06-13-2022 at 09:01 AM.

  45. #45
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    I do use that code... but I see under Task Manager that Excel remains open... Geez, I believe we are sooo close!

    I replaced original code with

    Please Login or Register  to view this content.
    Now it's crashing with an error "No cells were found" on the line

    Please Login or Register  to view this content.

    Also, shouldn't there be a "Set ws = Nothing" and/or a "Set wb = Nothing" towards the end somewhere (comment out in code below, because its not working :-( ...Just wondering :-)

    Please Login or Register  to view this content.
    Last edited by JohanGduToit; 06-13-2022 at 09:31 AM.

  46. #46
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    ...............
    Also, as mentioned multiple times previously, I do not have Access so cannot test...
    Perhaps you can start a new thread and ensure someone has Access to test your requirement...
    Also...please remove my email address...That just calls for spam...

  47. #47
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Morning Sintek,

    I think I may have stumbled across an error that may shed some light on the fact that the code fails on subsequent files. When processing a 2nd file I received an error stating that the ".Name" value 'Appended' is already in use" on the line below. As mentioned before, the 1st file after program startup always process 100%; but code fails on subsequent files. If I close my program and reopen after each processed file the code works 100%...it's something to do with the "Appended" value not being released (from memory) or reset properly after processing the 1st file.

    PS : I have added these two line at the bottom hoping it would help; but it does not seem to make any difference.

    Please Login or Register  to view this content.

    If you can't think of anything I will submit on an Access forum with your consent to include your code?... don't want to waste your time any further. Just thought it might be a quick fix for you...

    Please Login or Register  to view this content.
    Current Code below:

    Public Sub FormatMarkham01(sFile As String)
    Please Login or Register  to view this content.

  48. #48
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    IGNORE this post:



    Afterthought...

    Maybe add a check before the line below to see if "Appended" already exist. If it does, then 1st delete all rows/content on the "Appended" worksheet and continue, else add/create the "Appended" worksheet and continue?

    Please Login or Register  to view this content.
    Last edited by JohanGduToit; 06-14-2022 at 03:35 AM. Reason: Not well thought comment

  49. #49
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Maybe add a check before the line below to see if "Appended" already exist. If it does, then 1st delete all rows/content on the "Appended" worksheet and continue, else add/create the "Appended" worksheet and continue?
    Let me get a better understanding...

    If I run it a 1st time on say FileNameNo1 it runs perfectly. If I run it again straight afterwards with either the same FileNameNo1
    When running the first file, Appended Sheet does not exist...Correct...This from all your sample files provided...
    Code runs perfect...This means that you will not run the code again on the same file...Correct...
    Yet above you say you do...Why?

    Please Login or Register  to view this content.
    Where are you declaring sFile..In a different Module?

    Also check here
    Last edited by sintek; 06-14-2022 at 04:15 AM.

  50. #50
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Replace this line...
    Please Login or Register  to view this content.
    With this...
    Please Login or Register  to view this content.
    Just ran the code through Microsoft Word...12 times after each other with different files...all ran smoothly...

  51. #51
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Hi,

    The sFile variable is a combination of 2x string variables) and FormatMarkham01 gets called from another Module with a path to the file that's to be formatted:

    Please Login or Register  to view this content.
    None of the original files that we receive from our customer that we select for processing contain a worksheet named "Appended". The "Appended" worksheet only gets added by your code.

    So here's the two scenario's, Scenario 1 will result in errors when processing a 2nd file, whilst Scenario 2 will process the 2nd file successfully.

    Scenario 1:
    Steps:
    Open Access Application
    Select File "A" to Process
    Result = Success (No Error on 1st file)
    Select File "B" to Process
    Result = Fail - Error on line
    Please Login or Register  to view this content.
    (Should I at this stage close Access, reopen Access and again select File B, the selected file will process successfully (after the close/open of Access))

    Scenario 2:
    Steps:

    Open Access Application
    Select File "A" to Process
    Result = Success (No Error)
    Close Access Application
    Open Access Application
    Select File "B" to Process
    Result = Success (No Error)
    Close Access Application
    Open Access Application
    Select File 'C" to Process
    Results = Success (No Error)
    .etc


    Hope that makes more sense :-) ?

  52. #52
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Will try #50 quickly and revert back :-)

  53. #53
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    I can only assume that focus is being lost to the workbook

    May I ask, why are you running this through Access and not straight from Excel...You are just processing Spreadsheets?
    Last edited by sintek; 06-14-2022 at 04:38 AM.

  54. #54
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    You are a freaking genius!!!!

    No errors whatsoever :-) :-) :-) !!!

    Here's the final working code:

    Please Login or Register  to view this content.

  55. #55
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Glad we got it sorted...Happy Coding...

  56. #56
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    We have many customers sending us their weekly sales files... each customer's files are received in different 'raw' formats, unique to each customer. All these files must be formatted, validated and presented to our ERP System in a fixed format so that the data can be imported into our ERP System. So I have developed a MS Access frontend where users can select a customer, select the 'raw' customer's file to be processed and the application will then automatically do what needs to be done to get the data in the required format for upload into our ERP System. That's the reason for having to use MS Access... I will attach an image of the frontend...
    Attached Images Attached Images
    Last edited by JohanGduToit; 06-14-2022 at 05:09 AM.

  57. #57
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    I cannot thank you enough!!

  58. #58
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    By the by...You don't need to do this...Can be omitted from code...
    There are rare circumstances when it's required...
    Excel should clear the object when it goes out of scope...
    Please Login or Register  to view this content.

  59. #59
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Noted; it was part of my troubleshooting...

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

    Arrow Re: VBA to Transpose Rows to Columns on Multiple Worksheets


    Quote Originally Posted by sintek View Post
    Excel should clear the object when it goes out of scope...
    Please Login or Register  to view this content.
    Hi, but often Excel forget to clean object variables until it is finally closed,
    the reason why the better is the VBA procedure must release object variables in order to avoid some classic memory Excel crash …

  61. #61
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Hi Marc L,

    Thank you for clearing that up... I will therefor leave the two statements in the module, just to be safe.

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

    Arrow Re: VBA to Transpose Rows to Columns on Multiple Worksheets


    In common use there is no issue but if Excel stays opened during the workday and
    the same VBA procedure is launched many times without releasing object variables then a crash may arise …

  63. #63
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    @ MarcL...
    Yes, but in this case there is no clash between objects and none referencing another...Also Excel is quitting at end anyway...

  64. #64
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    @Marc L : Which will very much be the case with our intended use of the application.

    @Both : So no harm done by leaving the two statements?
    Last edited by JohanGduToit; 06-14-2022 at 07:13 AM.

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

    Arrow Re: VBA to Transpose Rows to Columns on Multiple Worksheets


    In fact as Excel needs to load everything in memory and as in classic Excel 32 bits version the memory it can managed is limited
    then the more unreleased objects the more chance to crash, like I met the case in a company …

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

    Arrow Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Quote Originally Posted by JohanGduToit View Post
    @Marc L : Which will very much be the case with our intended use of the application.

    @Both : So no harm done by leaving the two statements?
    Yes keep the object variable = Nothing codelines as it is better than without but,
    as Excel is the worst application I know for memory management, a good idea is to ask Excel users to close Excel at lunch time,
    when they take a break, to avoid to open many other applications when they use Excel, …
    Last edited by Marc L; 06-14-2022 at 07:23 AM.

  67. #67
    Registered User
    Join Date
    02-22-2022
    Location
    Cape Town
    MS-Off Ver
    Office365
    Posts
    59

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    Thank you both for your comments; well noted.

  68. #68
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,230

    Re: VBA to Transpose Rows to Columns on Multiple Worksheets

    If you really want to...Just in extreme case where this might occur...Then keep this line only
    Please Login or Register  to view this content.
    There is no reference set to the ws object..

+ 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. Transpose multiple columns into rows
    By Kievet in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-14-2020, 11:55 AM
  2. Replies: 1
    Last Post: 01-24-2014, 06:20 PM
  3. [SOLVED] Transpose Multiple Rows to Columns
    By pjscho in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-09-2013, 10:00 AM
  4. Transpose columns into multiple rows
    By taylorj3 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-14-2013, 07:41 PM
  5. Replies: 4
    Last Post: 03-08-2013, 09:49 AM
  6. transpose multiple rows into two columns
    By thane in forum Excel General
    Replies: 2
    Last Post: 09-14-2010, 12:48 AM
  7. transpose multiple rows and columns
    By prawer in forum Excel General
    Replies: 2
    Last Post: 06-03-2009, 04:20 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