+ Reply to Thread
Results 1 to 31 of 31

Compare column number of a cell with the last cell column number in a loop

  1. #1
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Exclamation Compare column number of a cell with the last cell column number in a loop

    I am trying to save an excel sheet to .csv format with the following macro:

    Please Login or Register  to view this content.
    The following part of the code needs to be modified so that the commas appear correctly in .csv file even for null values for any given column.

    Please Login or Register  to view this content.
    I have attached the Sample Sheets. Source sheet and the Result sheet. The Result sheet doesn't provide the required result. Some data are missing and unwanted commas have been added. Please help me to correct the logic in the above piece of code. Thanks!
    Attached Files Attached Files

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

    Re: Compare column number of a cell with the last cell column number in a loop


    Excel can directly save a workbook to a .csv file with Save as command …

  3. #3
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    Quote Originally Posted by Marc L View Post

    Excel can directly save a workbook to a .csv file with Save as command …
    Hi Marc,

    Thanks for the response! I know that we can save Excel workbook as .csv file. But i need to save .csv with UTF8 encoding which is why i am using the above macro.

    I had taken the reference from "http://techspread.wordpress.com/2012/12/05/export-excel-to-csv-in-unicode-utf-8-using-macro-vba/

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

    Re: Compare column number of a cell with the last cell column number in a loop


    And what about the CSV (DOS) file format ?!

  5. #5
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    Quote Originally Posted by Marc L View Post

    And what about the CSV (DOS) file format ?!
    I tried its giving undesired results! For example ú is getting converted to £ !

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

    Re: Compare column number of a cell with the last cell column number in a loop


    Maybe it would be easier for you to let Excel save a normal CSV file
    and after you just have to convert it to UTF8 …

  7. #7
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    Quote Originally Posted by Marc L View Post

    Maybe it would be easier for you to let Excel save a normal CSV file
    and after you just have to convert it to UTF8 …
    Can we not do anything with the existing macro?

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

    Cool Try this !


    Existing macro is crappy !
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Try this !

    Quote Originally Posted by Marc L View Post

    Existing macro is crappy !
    Please Login or Register  to view this content.
    That worked perfect! :D Though i am still trying to figure out what exactly you did here :P

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

    Thumbs up Thanks !


    I just placed a comma between each column, line by line …

    See the help of worksheet function INDEX and the one of VBA function Join

  11. #11
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Thanks !

    Sure I will! Thanks a lot Marc!

  12. #12
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Thanks !

    Quote Originally Posted by Pavan Renjal View Post
    Sure I will! Thanks a lot Marc!
    Hi Marc,

    I am not sure if i need to open a new thread since this thread has been marked as solved. But i am running into few issues which i am not able to fix.

    I receive Run-Time error '13' Type mismath error. We use large number of data(more than 65k records) and it appears to me that this might be causing the issue here. Can you please suggest any workaround for this? It is also taking a lot of time to save when the numbers are huge.

  13. #13
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    The error appears on the following code:

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    Can anyone help me on this please?

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

    Re: Compare column number of a cell with the last cell column number in a loop


    Hi,

    what's the Excel's version ? What is the R value at the error time ?

  16. #16
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    Quote Originally Posted by Marc L View Post

    Hi,

    what's the Excel's version ? What is the R value at the error time ?
    Hi Marc! Thanks for the response! The R shows value of '1' . My Excel version is MS Excel 2010, Version: 14.0.6129.5000 (32-bit)

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

    Question Re: Compare column number of a cell with the last cell column number in a loop


    Means that issue is yet on the first line of data like if there is only one data line, I don't understand …

    Index needs at least 2 data lines … Row #2 is not empty ?
    Because CurrentRegion is a range bounded by any combination of blank rows and blank columns.

    Error #13 means data type is not the expected one …

  18. #18
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    Quote Originally Posted by Marc L View Post

    Means that issue is yet on the first line of data like if there is only one data line, I don't understand …

    Index needs at least 2 data lines … Row #2 is not empty ?
    Because CurrentRegion is a range bounded by any combination of blank rows and blank columns.

    Error #13 means data type is not the expected one …
    Row #2 is not empty! I have 1.5 Lakh rows in my sheet!

  19. #19
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    To test this i just reduced the number of records to just 10. I was able to save it correctly. It appears to me that it has something to do with the size of the data.

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

    Re: Compare column number of a cell with the last cell column number in a loop


    Test with 65 536 rows. If success, test with one more row, aim is to find the row limit …

  21. #21
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    Quote Originally Posted by Marc L View Post

    Test with 65 536 rows. If success, test with one more row, aim is to find the row limit …
    I have tried saving the data but it seems like the application hangs for even data that is equal or less than 65536 rows but it doesnt error out which means the code might be still executing.

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

    Question Re: Compare column number of a cell with the last cell column number in a loop


    As I wrote, aim is to find the row limit …

  23. #23
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    I have also noticed that the execution time increases as the number of rows increase. And for data as huge as this (65536 rows) the application itself hangs

  24. #24
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    Let me check for today, i will leave the application as is though it appears as 'not responding' at the moment. If the application is able to execute and save the records i will notify you.

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

    Re: Compare column number of a cell with the last cell column number in a loop


    Other way is to directly loop row by row without using Index with an array variable
    but time process should increase …

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

    Lightbulb Try this ‼

    Please Login or Register  to view this content.

  27. #27
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Try this ‼

    Quote Originally Posted by Marc L View Post
    Please Login or Register  to view this content.
    Absolutely flawless execution! Thanks a ton dude! This has literally saved my ***. Total support \m/

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

    Re: Try this ‼


    You're welcome !

    You aim right : some inner Excel's functions are limited in their arrays …
    I read it somewhere but I did'nt find any notice.

    As Index function needs at least two rows, I give it just a pair …
    Last edited by Marc L; 02-28-2014 at 05:19 AM.

  29. #29
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Try this ‼

    Quote Originally Posted by Marc L View Post

    You're welcome !

    You aim right : some inner Excel's functions are limited in their arrays …
    I read it somewhere but I did'nt find any notice.

    As Index function needs at least two rows, I give it just a pair …
    To be frank, i am still trying to figure out how you did this :D Need to dig a bit more on INDEX, JOIN.

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

    Cool Re: Compare column number of a cell with the last cell column number in a loop


    Join & Split are like Cats & Dogs, Sun & Moon or Yin & Yang :
    one creates a string with a separator from an array,
    the other creates an array from a string cut by a separator.

    Join([{"Felix","The","Cat"}], "-") = "Felix-The-Cat"

    Split("Danny-The-Dog", "-") = [{"Danny","The","Dog"}]


    The worksheet function INDEX extracts - in this case - a row from a group of rows …
    Last edited by Marc L; 02-28-2014 at 04:31 PM.

  31. #31
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Compare column number of a cell with the last cell column number in a loop

    Quote Originally Posted by Marc L View Post

    Join & Split are like Cats & Dogs, Sun & Moon or Yin & Yang :
    one creates a string with a separator from an array,
    the other creates an array from a string cut by a separator.

    Join([{"Felix","The","Cat"}], "-") = "Felix-The-Cat"

    Split("Danny-The-Dog", "-") = [{"Danny","The","Dog"}]


    The worksheet function INDEX extracts - in this case - a row from a group of rows …
    Thanks for the explanation Mark! I believe this should help me get started

+ 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. Replies: 8
    Last Post: 11-18-2013, 04:24 PM
  2. [SOLVED] How to use the value of a cell as a column number of another cell in a for next loop?
    By George59 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-29-2012, 10:31 AM
  3. Replies: 1
    Last Post: 11-05-2010, 12:44 PM
  4. Replies: 10
    Last Post: 07-12-2010, 08:55 AM
  5. Replies: 7
    Last Post: 12-18-2008, 07:34 PM

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