+ Reply to Thread
Results 1 to 16 of 16

Format column

  1. #1
    Registered User
    Join Date
    04-12-2011
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Format column

    Hello all
    Again thanks to all who have helped in the past

    I have a new issue with the same process I have been working with

    The code provided by tigeravatar works wonderfully.

    The only issue (and I think this comes from how the log files are being generated) is that in the number in minutes for each test run comes in as a text format and not a number format. I need all information in the TTR column (Column J) to be in number format with no decimal places.

    Oddly enough some of the entries are numbers while others are not. As you can see with 1300 plus entries going back in a formating by hand would be a real pain.

    Can any help me with getting some VBA code that will force the J column to see the entries as numbers.

    Please note I have tried creating and recording a macro and formating a column and then modifying it and adding that to the existing VBA. This does not work. I have set the column to view all as a number, which also does not work.

    I am actually starting to wonder if I have stumbled onto a bug.
    Attached is the file I am working with so far.

    Thanks

    Shaun
    Attached Files Attached Files

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Formating a stubborn column the way I want

    Please Login or Register  to view this content.



  3. #3
    Registered User
    Join Date
    04-12-2011
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Formating a stubborn column the way I want

    Sadly this did not work, it is still importing the data from the individual CSV files as text, and will not change them to a number format.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Formating a stubborn column the way I want

    The reason some values are numbers and some are not are because of the hidden character in some of the cells. Look at cell J3, for instance, it is actually 240(char 13).

    Try adding this line of code at the end somewhere, or run it separately:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    04-30-2011
    Location
    Halifax, NS
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Formating a stubborn column the way I want

    The code that you apparently got from tigeravatar worked "wonderfully" - except that it landed you with a serious problem. In looking at the code it became apparent that it was written by someone with a great deal of ingenuity, but not very much depth of knowledge of VBA. For example, instead of using the 12 line SearchString(criteria, source) function, you could use the built-in VBA function: InStr(1, source, criteria, 1) which does exactly the same thing. And in fact you do not need this function at all if you use the built-in VBA Split() function to split your csv file into lines and then into line elements, as in the following revision of your code:

    Please Login or Register  to view this content.
    The use of vbNewLine as a separator between log lines (instead of Chr(10)) avoids the problem pointed out by JBeaucaire of the Chr(13) that gets left behind with the code you were using.

    I could not test the above code with your files, but I did test the section between the first If and the corresponding End If with a csv file I had on hand.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Formating a stubborn column the way I want

    or using VBCrLf and Texttocolumns:

    Please Login or Register  to view this content.
    Last edited by snb; 05-05-2011 at 03:17 PM.

  7. #7
    Registered User
    Join Date
    04-30-2011
    Location
    Halifax, NS
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Formating a stubborn column the way I want

    Hi snb,

    A model of compact code!!

    One thing it does not do, compared to the original code, is to apply the Trim() function to all cells. However, I am not sure whether this was a requirement, a precaution or a misunderstanding of what Trim does or does not do.

  8. #8
    Registered User
    Join Date
    04-12-2011
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Formating a stubborn column the way I want

    Keyston and Snb,
    Excellent code and an answer to prayer all the way around.
    Thanks much, now I just have to figure out how to get this all to execute from a command line using powershell, populate a chart and create a JPG......LOL not asking you all to help with that....YET...I want to try to figure it out on my own first, it is the best way that I learn.
    Shaun

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Formating a stubborn column the way I want

    @Keyston,

    on 'trimming'

    approaching:
    Please Login or Register  to view this content.
    or 100% trimming:

    Please Login or Register  to view this content.
    Last edited by snb; 05-08-2011 at 07:08 AM.

  10. #10
    Registered User
    Join Date
    04-30-2011
    Location
    Halifax, NS
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Formating a stubborn column the way I want

    100% trimming - code suggested by snb is:
    Please Login or Register  to view this content.
    Again, very compact code - but unfortunately it does not work, at least not for me. I get a compile error: "Expected function or variable", with the highlight on the left side of the second line. I tried to understand how this one-liner for trimming all cells in a range was intended to work, but I failed. And I did find a couple of web sites where it was stated explicitly that it could not be done with a one-liner - but perhaps the authors were not smart enough.

    In any event, what does work for me is:
    Please Login or Register  to view this content.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Formating a stubborn column the way I want

    Try this, after selecting the range of interest:

    Please Login or Register  to view this content.
    Note that this willl replace any formulas with their (trimmed) values.
    Last edited by shg; 05-06-2011 at 07:01 PM.
    Entia non sunt multiplicanda sine necessitate

  12. #12
    Registered User
    Join Date
    04-30-2011
    Location
    Halifax, NS
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Formating a stubborn column the way I want

    In fact the following does work:

    Please Login or Register  to view this content.
    as does your code, shg, and the following shortened variation:
    Please Login or Register  to view this content.
    However, I do not know why, in these two cases, if I substitute [snb] for Range("snb") I get the error: Expected function or variable.

    And then there are the equivalent one-liners:
    Please Login or Register  to view this content.
    Mind you, I have no idea how or why they work. If you have a reference to some details on this subject, I would be interested.

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Formating a stubborn column the way I want

    You could consider a 'named range' to be an object.
    You can use namedranges (or names) in an Excel-fromula, because the Excel -compiler checks whether the name you use in the formula is defined in the collection of 'names' (Ctrl-F3). If it is, then the 'name' will be replaced by it's equivalent.

    By using the brackets [ and ] we trigger the same 'checking' mechanism (the brackets are an equivalent for the method 'application.Evaluate'). Using brackets is a shorthand code for Application.evaluate.

    So if we do
    Please Login or Register  to view this content.
    we can use Range("snb") to indicate the usedrange of sheet(1)

    The shorthand writing of Range("A1") is [A1]
    The same can be applied to Range("snb"): [snb]
    On my system [snb] = gives no error

    Now the second part:
    [trim(snb)] is like an Excelformula without "= "
    'snb' will be recognised (evaluated) as a name range
    trim will only be applied to the first cell of the range; (what happens if you use a formula instead of a arrayformula)
    that's not what we intend.
    so we have to indicate that the whole range is meant
    we can use 2 methods: using 'if' or using index
    with 'index' we can indicate which row or which column we want to use from the range: row5: index(---,5); colum 7: index(--,,7),
    if we do not specify any column or row, the whole matrix will be indicated; in this way we turn the formula into an arrayformula.

    So what we want can be achieved by:

    Please Login or Register  to view this content.
    Last edited by snb; 05-08-2011 at 06:06 AM.

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Formating a stubborn column the way I want

    I had to edit the last example to remove a couple quotes to get it to work, but other than that this is great stuff!
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    04-30-2011
    Location
    Halifax, NS
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Formating a stubborn column the way I want

    Many thanks, snb, for your detailed explanation, although it does not cover the If (Row(),.. of shg - but I can live without that!.

    I have found out why the [snb] gave me an error. In your 100% trimming code example, which I copied and pasted, you gave the same name, snb, to the Sub. Looking through the collection of names it was understandably the Sub that was found first!

  16. #16
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Format column

    @Jerry,

    Thanks for correcting my code. I had too little patience to test that code, but it also illustrates why I prefer the shorthand code. Because I'm not that good in counting () most of the time in this kind of case I build a string like this in this way: Instead of " I use ~ and replace them by chr(34).

    Please Login or Register  to view this content.
    But on second thought we don't even need this:
    Please Login or Register  to view this content.

    @keyston
    Nice you found out yourself. I wouldn't have been able to detect that from a distance. But I also learned that you should avoid names for named ranges that have 'reserved names', be it by Excel or by yourself introducing macro/functions etc.

    The code shg gave:
    Please Login or Register  to view this content.
    In fact he gives the 'longhand' version of 'evaluate':
    He uses 'if( , )' to gain the arrayformula result. But as the 'if'-construction is purely introduced to convert the formula into an arrayformula a condition must be found that is always true for all cells in the range. Shg chooses row(), but it could also be column() or 12 or you name what.
    In such a case I'd prefer the testing condition 'True', because that's the least confusing one.

    For the whole usedrange:
    Please Login or Register  to view this content.
    To illustrate that a namedrange is an 'object', you can achieve the same result using an object-variable, for instance:

    Please Login or Register  to view this content.
    Last edited by snb; 05-08-2011 at 07:14 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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