Page 8 of 12 FirstFirst ... 678910 ... LastLast
Results 71 to 80 of 117

Thread: Tests and Notes on Range objects in Excel Cell

  1. #71
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,457
    Rep Power
    10

    Number stored as text, alignment of numeric values in cells

    This is post #22059



    Some notes arising from discussions in this Thread
    http://www.eileenslounge.com/viewtop...61085a6575decc







    Number stored as text, alignment of numeric values in cells

    This problem/feature pops up from time to time.
    https://i.postimg.cc/GpwT3Mzj/Number-stored-as-text.jpg
    Attachment 5104

    I have seen it so far in these sort of places/ things, such as text box contents are strings by definition , …VBA enters text that is inherent to a TextBox. Every 'number' in a textBox is a string "1234". VBA has to convert those strings into long/integer, etc….

    , and when pasting things out,
    https://www.eileenslounge.com/viewto...269105#p269105 https://eileenslounge.com/viewtopic....272704#p272704
    , but it’s not always consistent, - in the next example the 3 seems to go in as a number, but the 4 and 5 go in as …" Number stored as text"
    https://i.postimg.cc/cCzwRfG1/Number...onsistancy.jpg
    Code:
    '   https://www.excelfox.com/forum/showthread.php/2909-Appendix-Thread-Evaluate-Range-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=21923&viewfull=1#post21923
    Sub Number_stored_as_text__alignment_of_numeric_values_in_cells()   '      https://i.postimg.cc/cCzwRfG1/Number-stored-as-text-inconsistancy.jpg
    Dim Ex As String
     Let Ex = "3"
     Let ActiveSheet.Range("A10").Value = Ex ' Puts in as number
    Dim Exs(1 To 2) As String: Let Exs(1) = "4": Exs(2) = "5"
     Let ActiveSheet.Range("A11:B11").Value = Exs()
    End Sub



    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=jdPeMPT98QU
    https://www.youtube.com/watch?v=QdwDnUz96W0&lc=Ugx3syV3Bw6bxddVyBx4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgxsozCmRd3RAmIPO5B4AaABAg.9fxrOrrvTln9g9wr8mv2 CS
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g96yGbAX 4t
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9g7pczEpcTz
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g7lhoX-ar5
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gD0AA-sfpl
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gECpsAVGbh
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg.9g9wJCunNRa9gJGhDZ4R I2
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Attached Images Attached Images
    Last edited by DocAElstein; 08-01-2023 at 02:51 PM.

  2. #72
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,457
    Rep Power
    10
    Number stored as text, alignment of numeric values in cells
    Evaluate Range correction


    The problem from the last post lends itself nicely to the Evaluate Range ideas. As often, this idea is based on a simple mathematical formula, which is itself one of a few ways in Excel and VBA we have a few ways to coerce a text to a number:
    In Excel and VBA we have a few ways to coerce or "Methods to convert", http://www.eileenslounge.com/viewtop...309140#p309140
    One simple way is to multiply by 1.

    As ever with these Evaluate Range solutions, we start with a single cell formula which works in Excel.
    Take the screenshot example from the last post. In C11 this appears to work, =1*A11
    _____ Workbook: Number stored as text, alignment of numeric values in cells.xls ( Using Excel 2013 32 bit )
    Row\Col A B C
    10 3
    11 4 5 =1*A11
    12
    Worksheet: Sheet1
    https://i.postimg.cc/prJFZt2s/1xA11.jpg
    Attachment 5105
    What that simple formula appears to do is to make the 4 in cell A11 be written in cell C11 a true number, and so we effectively get rid of that warning of the form , "Number stored as Text"

    Now the Evaluate Range bit.
    Evaluate Range ideas are often a neat alternative to looping in VBA
    Staying with Excel initially. (**Note: This explanation may not suit so well for an Excel version with the Spill/Dynamic arrays stuff, (currently from about version 365).)
    In Excel, like in a lot of computer stuff, what we look at, or get as output to see, is based on something
    chucking out from left to right
    ,then returning to the left, ( sometimes referred to as "invisible" character like Carriage Return vbCr )
    , and then going to a new line/row, ( sometimes referred to as "invisible" character like LineFeed vbLf )
    , then starting again until we get a 2 dimensional picture we as humans understand. In some cases this means that similar calculations may be done by default over an extended range, but normally we don’t see that as in Excel we look at doing stuff in one of excel boxes/cells. Somehow Excel will be wired to show a single value in a cell in a typical formula such as our =1*A11
    Whether by design or accident, right from the start of when Excel came about, we can get some interesting results if we replace a single cell reference by a multiple one.
    For example, in C11, write =1*A11:B11 instead, so as to maybe see if we can somehow do our basic formula for cells A11 and B11 at the same time.
    But, try that and it will error**. Never mind. Let’s keep experimenting.
    https://i.postimg.cc/k5CmNLgp/1x-A11-B11.jpg
    https://i.postimg.cc/zBXY6WWw/1xA11B11.jpg

    (** For Excel 365 it may not error, and I prefer not to consider yet what is happening there)

    Type 1 CSE entry
    There may be some reason such as extra overheads for Excel to show the same calculation for both cells, resulting in perhaps a deliberate prevention of the thing working, but my guess is that it does work without too much effort. Often we talk about Type 1 CSE entry, or CSE entry in general, as a way to tell Excel that you would like to take advantage of the possibility to do the calculation for a range other than a single cell.
    To get the first inclination about what I am talking about, do this: do the same thing again, in C11, write =1*A11:B11 , but don’t hit Enter yet. Instead, hold keys Ctrl+Shift and then hit Enter
    We don’t error anymore. But we still only see one value. https://i.postimg.cc/m2v5xMCx/1x-A11-B11-CSE1.jpg
    If you apply a bit of lateral thinning to what we said earlier about how computer things work , ( ……chucking out from left to right , returning to the left and gong to a new line/row, then starting again ) , a good guess might be that in our example the other calculated value might be somewhere close to the right. That takes us nicely on to the next section, and how that relates to our example

    Type 2 CSE entry
    CSE generally seems to tell Excel to do multiple cell calculations, at least that is what we suggested. (We talk sometimes about "array calculations", but this may be strictly in mathematical definitions a bit incorrect)
    But we generally write things in a cell, and generally for simple formulas we would expect a result in a cell.

    But let's try it a bit differently, bearing in mind our example. So, start again, but before you type =1*A11:B11, first select the two cells, C11 and D11. Then after you type that, do the CSE entry again, not a simple Enter
    https://i.postimg.cc/m2w2rQN9/1x-A11-B11-CSE2.jpg
    Bingo!
    https://i.postimg.cc/m2RgRdMR/1x-A11-B11-CSE2-Bingo.jpg
    Attachment 5106


    Evaluate Range Solution as alternative to VBA Looping
    So far we have just considered an Excel solution. That background was necessary to understand the Evaluate Range type Solution as alternative to VBA Looping
    In VBA, a conventional way to do multi cell calculations would be to Loop. It loops, in other words does a similar codung a few times. It would need to do it twice, https://i.postimg.cc/Wb8ZRJM7/Loop-i...mn-A-and-B.jpg , in our example:
    Code:
    Sub LoopItTwice()  '  https://www.excelfox.com/forum/showthread.php/2909-Appendix-Thread-Evaluate-Range-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=21924&viewfull=1#post21924
    Dim Nmbr As Long
        For Nmbr = 1 To 2 Step 1
         Let ActiveSheet.Cells.Item(12, Nmbr + 2).Value = ActiveSheet.Cells.Item(11, Nmbr).Value  '    Always uses row 12 ,   done twice, first  for  column 3 value = column 1 value    and  then   column 4 value = column 2
        Next Nmbr
    End Sub
    We have just discussed that the CSE stuff in Excel gives us a way to do it in one go. Let’s investigate if we can do something like that in VBA

    Do Excel stuff in VBA
    In VBA there is a Evaluate("__") function. In simple terms, to a first approximation, we can put Excel formulas inside the quotes, here
    Evaluate("here")
    , and the result is returned, similar to what would be returned in a cell with that formula in it.
    Exactly what may be "returned" (chucked at us from Evaluate("__") ) , may be many things, depending on the exact formula, so it makes sense that the thing is wired to return a Variant type, so that can hold whatever actual type comes from the evaluation. So something like this sort of form would be done in VBA
    Dim Var As Variant
    _Let Var = Evaluate("=A11:B11*1")


    If you put a Stop statement (or alternatively click in the left margin to get a brown circle which works the same), then run the following full macro, then while it is paused, (or before you start) , highlight Var , hold key Shift and then hit key F9, to put a watch on that variable, then you can see the results.
    https://i.postimg.cc/vmPf13Cx/Put-a-watch-on-Var.jpg
    https://i.postimg.cc/mDtM77GX/Put-a-watch-on-Var.jpg
    Code:
    Sub RangeEvaluate()
    Dim Var As Variant
     Let Var = Evaluate("=A11:B11*1")
    
    Stop   '     If you put a Stop statement (or alternatively click in the left margin to get a brown circle which works the same), then run the following full macro, then while it is paused, (or before you start) , highlight Var , hold key Shift  and then hit key F9, to put a watch on that variable, then you can see the results.
    'https://i.postimg.cc/vmPf13Cx/Put-a-watch-on-Var.jpg
    'https://i.postimg.cc/mDtM77GX/Put-a-watch-on-Var.jpg
    End Sub
    
    https://i.postimg.cc/9Mb931YQ/Put-a-watch-on-Var.jpg


    Occasionally, the Evaluate(" ") does not "return" more than one result in formulas involving multi cell ranges, but this appears to be a separate issue to the need for CSE measures. Often we can do empirically derived tricks to overcome such problems.
    But in many cases the Evaluate(" ") does returns the same results immediately that are only returned in a spreadsheet via the CSE way discussed. It would appear that principally the Evaluate(" ") is not restricted by the single cell , or need for CSE entry issues as in the spreadsheet. Perhaps this is because in use, Evaluate(" ") is not tied in some way to a particular spreadsheet format to show its output. There appears to be a lot of uncertainty ad empirically derived solutions and tricks in Evaluate Range

    So, we have our result, or a way to get it chucked at us , "returned" , by using an Excel spreadsheet formula from within VBA
    So what do we do with it/ how do we get it in a cell, or more specifically in our example, how do we get it in cells,
    ……. Next post
    Attached Images Attached Images

  3. #73
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,457
    Rep Power
    10
    Number stored as text, alignment of numeric values in cells
    Evaluate Range correction


    The problem from the last post lends itself nicely to the Evaluate Range ideas. As often, this idea is based on a simple mathematical formula, which is itself one of a few ways in Excel and VBA we have a few ways to coerce a text to a number:
    In Excel and VBA we have a few ways to coerce or "Methods to convert", http://www.eileenslounge.com/viewtop...309140#p309140
    One simple way is to multiply by 1.

    As ever with these Evaluate Range solutions, we start with a single cell formula which works in Excel.
    Take the screenshot example from the last post. In C11 this appears to work, =1*A11
    _____ Workbook: Number stored as text, alignment of numeric values in cells.xls ( Using Excel 2013 32 bit )
    Row\Col A B C
    10 3
    11 4 5 =1*A11
    12
    Worksheet: Sheet1
    https://i.postimg.cc/prJFZt2s/1xA11.jpg
    Attachment 5105
    What that simple formula appears to do is to make the 4 in cell A11 be written in cell C11 a true number, and so we effectively get rid of that warning of the form , "Number stored as Text"

    Now the Evaluate Range bit.
    Evaluate Range ideas are often a neat alternative to looping in VBA
    Staying with Excel initially. (**Note: This explanation may not suit so well for an Excel version with the Spill/Dynamic arrays stuff, (currently from about version 365).)
    In Excel, like in a lot of computer stuff, what we look at, or get as output to see, is based on something
    chucking out from left to right
    ,then returning to the left, ( sometimes referred to as "invisible" character like Carriage Return vbCr )
    , and then going to a new line/row, ( sometimes referred to as "invisible" character like LineFeed vbLf )
    , then starting again until we get a 2 dimensional picture we as humans understand. In some cases this means that similar calculations may be done by default over an extended range, but normally we don’t see that as in Excel we look at doing stuff in one of excel boxes/cells. Somehow Excel will be wired to show a single value in a cell in a typical formula such as our =1*A11
    Whether by design or accident, right from the start of when Excel came about, we can get some interesting results if we replace a single cell reference by a multiple one.
    For example, in C11, write =1*A11:B11 instead, so as to maybe see if we can somehow do our basic formula for cells A11 and B11 at the same time.
    But, try that and it will error**. Never mind. Let’s keep experimenting.
    https://i.postimg.cc/k5CmNLgp/1x-A11-B11.jpg
    https://i.postimg.cc/zBXY6WWw/1xA11B11.jpg

    (** For Excel 365 it may not error, and I prefer not to consider yet what is happening there)

    Type 1 CSE entry
    There may be some reason such as extra overheads for Excel to show the same calculation for both cells, resulting in perhaps a deliberate prevention of the thing working, but my guess is that it does work without too much effort. Often we talk about Type 1 CSE entry, or CSE entry in general, as a way to tell Excel that you would like to take advantage of the possibility to do the calculation for a range other than a single cell.
    To get the first inclination about what I am talking about, do this: do the same thing again, in C11, write =1*A11:B11 , but don’t hit Enter yet. Instead, hold keys Ctrl+Shift and then hit Enter
    We don’t error anymore. But we still only see one value. https://i.postimg.cc/m2v5xMCx/1x-A11-B11-CSE1.jpg
    If you apply a bit of lateral thinning to what we said earlier about how computer things work , ( ……chucking out from left to right , returning to the left and gong to a new line/row, then starting again ) , a good guess might be that in our example the other calculated value might be somewhere close to the right. That takes us nicely on to the next section, and how that relates to our example

    Type 2 CSE entry
    CSE generally seems to tell Excel to do multiple cell calculations, at least that is what we suggested. (We talk sometimes about "array calculations", but this may be strictly in mathematical definitions a bit incorrect)
    But we generally write things in a cell, and generally for simple formulas we would expect a result in a cell.

    But let's try it a bit differently, bearing in mind our example. So, start again, but before you type =1*A11:B11, first select the two cells, C11 and D11. Then after you type that, do the CSE entry again, not a simple Enter
    https://i.postimg.cc/m2w2rQN9/1x-A11-B11-CSE2.jpg
    Bingo!
    https://i.postimg.cc/m2RgRdMR/1x-A11-B11-CSE2-Bingo.jpg
    Attachment 5106


    Evaluate Range Solution as alternative to VBA Looping
    So far we have just considered an Excel solution. That background was necessary to understand the Evaluate Range type Solution as alternative to VBA Looping
    In VBA, a conventional way to do multi cell calculations would be to Loop. It loops, in other words does a similar codung a few times. It would need to do it twice, https://i.postimg.cc/Wb8ZRJM7/Loop-i...mn-A-and-B.jpg , in our example:
    Code:
    Sub LoopItTwice()  '  https://www.excelfox.com/forum/showthread.php/2909-Appendix-Thread-Evaluate-Range-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=21924&viewfull=1#post21924
    Dim Nmbr As Long
        For Nmbr = 1 To 2 Step 1
         Let ActiveSheet.Cells.Item(12, Nmbr + 2).Value = ActiveSheet.Cells.Item(11, Nmbr).Value  '    Always uses row 12 ,   done twice, first  for  column 3 value = column 1 value    and  then   column 4 value = column 2
        Next Nmbr
    End Sub
    We have just discussed that the CSE stuff in Excel gives us a way to do it in one go. Let’s investigate if we can do something like that in VBA

    Do Excel stuff in VBA
    In VBA there is a Evaluate("__") function. In simple terms, to a first approximation, we can put Excel formulas inside the quotes, here
    Evaluate("here")
    , and the result is returned, similar to what would be returned in a cell with that formula in it.
    Exactly what may be "returned" (chucked at us from Evaluate("__") ) , may be many things, depending on the exact formula, so it makes sense that the thing is wired to return a Variant type, so that can hold whatever actual type comes from the evaluation. So something like this sort of form would be done in VBA
    Dim Var As Variant
    _Let Var = Evaluate("=A11:B11*1")


    If you put a Stop statement (or alternatively click in the left margin to get a brown circle which works the same), then run the following full macro, then while it is paused, (or before you start) , highlight Var , hold key Shift and then hit key F9, to put a watch on that variable, then you can see the results.
    https://i.postimg.cc/vmPf13Cx/Put-a-watch-on-Var.jpg
    https://i.postimg.cc/mDtM77GX/Put-a-watch-on-Var.jpg
    Code:
    Sub RangeEvaluate()
    Dim Var As Variant
     Let Var = Evaluate("=A11:B11*1")
    
    Stop   '     If you put a Stop statement (or alternatively click in the left margin to get a brown circle which works the same), then run the following full macro, then while it is paused, (or before you start) , highlight Var , hold key Shift  and then hit key F9, to put a watch on that variable, then you can see the results.
    'https://i.postimg.cc/vmPf13Cx/Put-a-watch-on-Var.jpg
    'https://i.postimg.cc/mDtM77GX/Put-a-watch-on-Var.jpg
    End Sub
    
    https://i.postimg.cc/9Mb931YQ/Put-a-watch-on-Var.jpg


    Occasionally, the Evaluate(" ") does not "return" more than one result in formulas involving multi cell ranges, but this appears to be a separate issue to the need for CSE measures. Often we can do empirically derived tricks to overcome such problems.
    But in many cases the Evaluate(" ") does returns the same results immediately that are only returned in a spreadsheet via the CSE way discussed. It would appear that principally the Evaluate(" ") is not restricted by the single cell , or need for CSE entry issues as in the spreadsheet. Perhaps this is because in use, Evaluate(" ") is not tied in some way to a particular spreadsheet format to show its output. There appears to be a lot of uncertainty ad empirically derived solutions and tricks in Evaluate Range

    So, we have our result, or a way to get it chucked at us , "returned" , by using an Excel spreadsheet formula from within VBA
    So what do we do with it/ how do we get it in a cell, or more specifically in our example, how do we get it in cells,
    ……. Next post
    Attached Images Attached Images

  4. #74

  5. #75
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,457
    Rep Power
    10
    From last post, to recap a bit……
    Do Excel stuff in VBA
    In VBA there is a Evaluate("__") function. In simple terms, to a first approximation, we can put Excel formulas inside the quotes, here
    Evaluate("here")
    , and the result is returned, similar to what would be returned in a cell with that formula in it.
    Exactly what may be "returned" (chucked at us from Evaluate("__") ) , may be many things, depending on the exact formula, so it makes sense that the thing is wired to return a Variant type, so that can hold whatever actual type comes from the evaluation. So something like this sort of form would be done in VBA
    Dim Var As Variant
    _Let Var = Evaluate("=A11:B11*1")


    If you put a Stop statement (or alternatively click in the left margin to get a brown circle which works the same), then run the following full macro, then while it is paused, (or before you start) , highlight Var , hold key Shift and then hit key F9, to put a watch on that variable, then you can see the results.
    https://i.postimg.cc/vmPf13Cx/Put-a-watch-on-Var.jpg
    https://i.postimg.cc/mDtM77GX/Put-a-watch-on-Var.jpg
    Code:
    Sub RangeEvaluate()
    Dim Var As Variant
     Let Var = Evaluate("=A11:B11*1")
    
    Stop   '     If you put a Stop statement (or alternatively click in the left margin to get a brown circle which works the same), then run the following full macro, then while it is paused, (or before you start) , highlight Var , hold key Shift  and then hit key F9, to put a watch on that variable, then you can see the results.
    'https://i.postimg.cc/vmPf13Cx/Put-a-watch-on-Var.jpg
    'https://i.postimg.cc/mDtM77GX/Put-a-watch-on-Var.jpg
    End Sub
    
    https://i.postimg.cc/9Mb931YQ/Put-a-watch-on-Var.jpg


    So, we have our result, or a way to get it chucked at us , "returned" , by using an Excel spreadsheet formula from within VBA
    So what do we do with it/ how do we get it in a cell, or more specifically in our example, how do we get it in cells


    Simple application of Evaluate("=A11:B11*1")type solutions
    ( Put what is chucked at us, ("returned") into cells )
    Conveniently, Excel VBA allows a “field” or array of values, in other words a “square” or grid full of values to be applied directly to a rectangular range, pseudo code examples:

    Simple application of Evaluate("=A11:B11*1") type solutions
    Conveniently, Excel VBA allows a “field” or array of values, in other words a “square” or grid full of values to be applied directly to a rectangular range, pseudo code examples:

    Range(“A1:B2”) values = { a b
    _________________--__________c d
    }

    Range(“A1:C1”) values = { x y z }

    Range(“D1:E1”) values = { 4, 5 }

    The right hand side of that last equation could be the array or field of values which was returned by our example, Evaluate("=A11:B11*1") . An actual working syntax to do in VBA what we did via CSE techniques previously would be
    Range("C11:D11").Value = Evaluate("=A11:B11*1")

    VBA coding “ works backwards “ , doing the right hand side first, so if we wanted to replace the original range with the values without the Number stored as text thing, then this would work

    Range("A11:B11").Value = Evaluate("=A11:B11*1")

    At this point we note that this basic coding layout can be written in a few different forms. In particular, we have great flexibility on the right hand side by virtue of the fact that the Evaluate takes a string argument. We are free to build up strings in VBA as we choose. They can be built up from hard coded text, as we currently have, or we can also include VBA things which would be converted at run time to what they evaluate to. For example, Range("A11:B11").Address returns $A$11:$B$11 , which for our purposes, in spreadsheet syntax, as used in a spreadsheet cell, has the same effect as A11:B11. So that last code line could be written

    Range("A11:B11").Value = Evaluate("=" & Range("A11:B11").Address & "*1")


    The last two code lines are effectively replacing a range which might have the Number stored as text thing showing to a range without that showing. In other words, it removes the Number stored as text thing.
    We are free to replace the range Range("A11:B11") with any range we choose.
    _ It could be some variable defined as a range like this
    Dim Rng As Range
    Set Rng = Range("A11:B11")
    Let Rng.Value = Evaluate("=" & Rng.Address & "*1")

    _ Similarly, we could make some selection in the spreadsheet, and then run the macro on this range using
    Let Selection.Value = Evaluate("=" & Selection.Address & "*1")
    This last macro is often the most convenient, but bear in mind that it will replace the original range, and things done by VBA can’t be undone. Best would be to test first by pasting a copy in a spare range. For example, if you have data up to column F, then this would put the test range to the side of it.
    Selection.Offset(0, 6).Value = Evaluate("=" & Selection.Address & "*1")
    (As often in VBA, the Offset works on the top left, shifting the whole range with it. In the inner workings of Excel VBA, things are often anchored or referenced to the top left)

    In a few posts time we some simple variations of the Evaluate range way to remove the Number stored as text thing issue, specifically we are making the last few coding version less susceptible to problems / more workable on different data types
    But first we will recap and discuss a useful typical format / versions of our final coding for test and developments






    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=jdPeMPT98QU
    https://www.youtube.com/watch?v=QdwDnUz96W0&lc=Ugx3syV3Bw6bxddVyBx4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgxsozCmRd3RAmIPO5B4AaABAg.9fxrOrrvTln9g9wr8mv2 CS
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g96yGbAX 4t
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9g7pczEpcTz
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g7lhoX-ar5
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gD0AA-sfpl
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gECpsAVGbh
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg.9g9wJCunNRa9gJGhDZ4R I2
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA

  6. #76
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,457
    Rep Power
    10
    From last post, to recap a bit……
    Do Excel stuff in VBA
    In VBA there is a Evaluate("__") function. In simple terms, to a first approximation, we can put Excel formulas inside the quotes, here
    Evaluate("here")
    , and the result is returned, similar to what would be returned in a cell with that formula in it.
    Exactly what may be "returned" (chucked at us from Evaluate("__") ) , may be many things, depending on the exact formula, so it makes sense that the thing is wired to return a Variant type, so that can hold whatever actual type comes from the evaluation. So something like this sort of form would be done in VBA
    Dim Var As Variant
    _Let Var = Evaluate("=A11:B11*1")


    If you put a Stop statement (or alternatively click in the left margin to get a brown circle which works the same), then run the following full macro, then while it is paused, (or before you start) , highlight Var , hold key Shift and then hit key F9, to put a watch on that variable, then you can see the results.
    https://i.postimg.cc/vmPf13Cx/Put-a-watch-on-Var.jpg
    https://i.postimg.cc/mDtM77GX/Put-a-watch-on-Var.jpg
    Code:
    Sub RangeEvaluate()
    Dim Var As Variant
     Let Var = Evaluate("=A11:B11*1")
    
    Stop   '     If you put a Stop statement (or alternatively click in the left margin to get a brown circle which works the same), then run the following full macro, then while it is paused, (or before you start) , highlight Var , hold key Shift  and then hit key F9, to put a watch on that variable, then you can see the results.
    'https://i.postimg.cc/vmPf13Cx/Put-a-watch-on-Var.jpg
    'https://i.postimg.cc/mDtM77GX/Put-a-watch-on-Var.jpg
    End Sub
    
    https://i.postimg.cc/9Mb931YQ/Put-a-watch-on-Var.jpg


    So, we have our result, or a way to get it chucked at us , "returned" , by using an Excel spreadsheet formula from within VBA
    So what do we do with it/ how do we get it in a cell, or more specifically in our example, how do we get it in cells


    Simple application of Evaluate("=A11:B11*1")type solutions
    ( Put what is chucked at us, ("returned") into cells )
    Conveniently, Excel VBA allows a “field” or array of values, in other words a “square” or grid full of values to be applied directly to a rectangular range, pseudo code examples:

    Simple application of Evaluate("=A11:B11*1") type solutions
    Conveniently, Excel VBA allows a “field” or array of values, in other words a “square” or grid full of values to be applied directly to a rectangular range, pseudo code examples:

    Range(“A1:B2”) values = { a b
    _________________--__________c d
    }

    Range(“A1:C1”) values = { x y z }

    Range(“D1:E1”) values = { 4, 5 }

    The right hand side of that last equation could be the array or field of values which was returned by our example, Evaluate("=A11:B11*1") . An actual working syntax to do in VBA what we did via CSE techniques previously would be
    Range("C11:D11").Value = Evaluate("=A11:B11*1")

    VBA coding “ works backwards “ , doing the right hand side first, so if we wanted to replace the original range with the values without the Number stored as text thing, then this would work

    Range("A11:B11").Value = Evaluate("=A11:B11*1")

    At this point we note that this basic coding layout can be written in a few different forms. In particular, we have great flexibility on the right hand side by virtue of the fact that the Evaluate takes a string argument. We are free to build up strings in VBA as we choose. They can be built up from hard coded text, as we currently have, or we can also include VBA things which would be converted at run time to what they evaluate to. For example, Range("A11:B11").Address returns $A$11:$B$11 , which for our purposes, in spreadsheet syntax, as used in a spreadsheet cell, has the same effect as A11:B11. So that last code line could be written

    Range("A11:B11").Value = Evaluate("=" & Range("A11:B11").Address & "*1")


    The last two code lines are effectively replacing a range which might have the Number stored as text thing showing to a range without that showing. In other words, it removes the Number stored as text thing.
    We are free to replace the range Range("A11:B11") with any range we choose.
    _ It could be some variable defined as a range like this
    Dim Rng As Range
    Set Rng = Range("A11:B11")
    Let Rng.Value = Evaluate("=" & Rng.Address & "*1")

    _ Similarly, we could make some selection in the spreadsheet, and then run the macro on this range using
    Let Selection.Value = Evaluate("=" & Selection.Address & "*1")
    This last macro is often the most convenient, but bear in mind that it will replace the original range, and things done by VBA can’t be undone. Best would be to test first by pasting a copy in a spare range. For example, if you have data up to column F, then this would put the test range to the side of it.
    Selection.Offset(0, 6).Value = Evaluate("=" & Selection.Address & "*1")
    (As often in VBA, the Offset works on the top left, shifting the whole range with it. In the inner workings of Excel VBA, things are often anchored or referenced to the top left)

    In a few posts time we some simple variations of the Evaluate range way to remove the Number stored as text thing issue, specifically we are making the last few coding version less susceptible to problems / more workable on different data types
    But first we will recap and discuss a useful typical format / versions of our final coding for test and developments






    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=jdPeMPT98QU
    https://www.youtube.com/watch?v=QdwDnUz96W0&lc=Ugx3syV3Bw6bxddVyBx4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgxsozCmRd3RAmIPO5B4AaABAg.9fxrOrrvTln9g9wr8mv2 CS
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g96yGbAX 4t
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9g7pczEpcTz
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g7lhoX-ar5
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gD0AA-sfpl
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gECpsAVGbh
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg.9g9wJCunNRa9gJGhDZ4R I2
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA

  7. #77
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,457
    Rep Power
    10
    Number stored as text, alignment of numeric values in cells

    Improved/ Extended Evaluate Range solution. Recap General Purpose solution
    In a few posts time we will discusses extending the last solution to overcome a couple of problems that may occur with certain types of range data, specifically, if there may be empty cells or cells with real text in it. In this and the following 2 posts we will recap and come up with a general purpose solution to then go on and modify.

    General all purpose version of our final Evaluate Range solution Range variable, Rng
    Let us both standardise and recap on a good general purpose version of our Evaluate Range solution, with emphasis on learning and test and developing

    Range variable, Rng
    It’s probably more often than not a good idea to use a variable for our main data range. In technical terms we might re say that, as a variable for our main Range object. Using a variable for things is often a good idea, and particular a good idea for a range object, since Excel and Excel is all about boxes and manipulating them, and in VBA in particular, we have a lot of useful built in things like Methods, Properties, and the such, that we can use on them/ with them, in particular the range object since that is in particular to do with the box, and boxes structure that Excel is all about.
    So for some test range, such as A1:F1,
    https://i.postimg.cc/nctNH0WJ/Range-A1-F7.jpg
    Attachment 5113
    , then somewhere towards the start of any macro we might have something like this
    Dim Rng As Range
    _Set Rng = Worksheets.Item("Sheet1").Range("A1:F7")

    You can often get away without the Worksheets.Item("Sheet1"). , if you know what you are doing and are sure that Excel will guess correctly which worksheet you are wanting to refer to, but often in a final coding, Excel may guess wrong, so more fully referencing a range is a good habit to get into.
    We may as well, from the start of our coding get fully into such good habits, and use a variable for the worksheet also
    Dim Ws1 As Worksheet
    _Set Ws1 = ThisWorkbook.Worksheets.Item("Sheet1")
    Dim Rng As Range
    _Set Rng = Ws1.Range("A1:F7")

    This Rng is referring to the test data range……._
    Attached Images Attached Images

  8. #78
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,457
    Rep Power
    10
    Number stored as text, alignment of numeric values in cells

    Improved/ Extended Evaluate Range solution. Recap General Purpose solution
    In a few posts time we will discusses extending the last solution to overcome a couple of problems that may occur with certain types of range data, specifically, if there may be empty cells or cells with real text in it. In this and the following 2 posts we will recap and come up with a general purpose solution to then go on and modify.

    General all purpose version of our final Evaluate Range solution Range variable, Rng
    Let us both standardise and recap on a good general purpose version of our Evaluate Range solution, with emphasis on learning and test and developing

    Range variable, Rng
    It’s probably more often than not a good idea to use a variable for our main data range. In technical terms we might re say that, as a variable for our main Range object. Using a variable for things is often a good idea, and particular a good idea for a range object, since Excel and Excel is all about boxes and manipulating them, and in VBA in particular, we have a lot of useful built in things like Methods, Properties, and the such, that we can use on them/ with them, in particular the range object since that is in particular to do with the box, and boxes structure that Excel is all about.
    So for some test range, such as A1:F1,
    https://i.postimg.cc/nctNH0WJ/Range-A1-F7.jpg
    Attachment 5113
    , then somewhere towards the start of any macro we might have something like this
    Dim Rng As Range
    _Set Rng = Worksheets.Item("Sheet1").Range("A1:F7")

    You can often get away without the Worksheets.Item("Sheet1"). , if you know what you are doing and are sure that Excel will guess correctly which worksheet you are wanting to refer to, but often in a final coding, Excel may guess wrong, so more fully referencing a range is a good habit to get into.
    We may as well, from the start of our coding get fully into such good habits, and use a variable for the worksheet also
    Dim Ws1 As Worksheet
    _Set Ws1 = ThisWorkbook.Worksheets.Item("Sheet1")
    Dim Rng As Range
    _Set Rng = Ws1.Range("A1:F7")

    This Rng is referring to the test data range……._
    Attached Images Attached Images

  9. #79
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,457
    Rep Power
    10
    General all purpose version of our final Evaluate Range solution Range variable, The left hand side, LHS, of a general purpose code line.
    The left hand side, LHS, of a general purpose code line.
    _....... This Rng is referring to the test data range.
    We want to modify/ correct that, so up until now that was the thing on the LHS.
    But, it is convenient in test and development not to replace the original range, but perhaps rather, if you have space, put the output from the coding calculations somewhere alongside. That way if something goes wrong, you don’t have to try and find the original data in order to try again. For example this would be the LHS to place the result alongside to the right. _Let Rng.Offset(0, Rng.Columns.Count).Value =

    That is a good choice to use as we are sure to place to the right, since we offset the output a number of column equal to the number of columns that the range has. Personally, I might do this
    _Let Rng.Offset(0, Rng.Columns.Count + 1).Value =
    , since an extra empty column in between can have later advantages.



















    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=jdPeMPT98QU
    https://www.youtube.com/watch?v=QdwDnUz96W0&lc=Ugx3syV3Bw6bxddVyBx4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgxsozCmRd3RAmIPO5B4AaABAg.9fxrOrrvTln9g9wr8mv2 CS
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g96yGbAX 4t
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9g7pczEpcTz
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g7lhoX-ar5
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gD0AA-sfpl
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gECpsAVGbh
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg.9g9wJCunNRa9gJGhDZ4R I2
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA

  10. #80
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,457
    Rep Power
    10
    General all purpose version of our final Evaluate Range solution Range variable, The left hand side, LHS, of a general purpose code line.
    The left hand side, LHS, of a general purpose code line.
    _....... This Rng is referring to the test data range.
    We want to modify/ correct that, so up until now that was the thing on the LHS.
    But, it is convenient in test and development not to replace the original range, but perhaps rather, if you have space, put the output from the coding calculations somewhere alongside. That way if something goes wrong, you don’t have to try and find the original data in order to try again. For example this would be the LHS to place the result alongside to the right. _Let Rng.Offset(0, Rng.Columns.Count).Value =

    That is a good choice to use as we are sure to place to the right, since we offset the output a number of column equal to the number of columns that the range has. Personally, I might do this
    _Let Rng.Offset(0, Rng.Columns.Count + 1).Value =
    , since an extra empty column in between can have later advantages.



















    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=jdPeMPT98QU
    https://www.youtube.com/watch?v=QdwDnUz96W0&lc=Ugx3syV3Bw6bxddVyBx4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgxsozCmRd3RAmIPO5B4AaABAg.9fxrOrrvTln9g9wr8mv2 CS
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g96yGbAX 4t
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9g7pczEpcTz
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g7lhoX-ar5
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gD0AA-sfpl
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gECpsAVGbh
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg.9g9wJCunNRa9gJGhDZ4R I2
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA

Similar Threads

  1. Replies: 114
    Last Post: 03-04-2024, 02:39 PM
  2. Tests and Notes on Range Referrencing
    By DocAElstein in forum Test Area
    Replies: 70
    Last Post: 02-20-2024, 01:54 AM
  3. Tests and Notes for EMail Threads
    By DocAElstein in forum Test Area
    Replies: 29
    Last Post: 11-15-2022, 04:39 PM
  4. Some Date Notes and Tests
    By DocAElstein in forum Test Area
    Replies: 0
    Last Post: 11-23-2021, 10:40 PM
  5. Notes tests. Excel VBA Folder File Search
    By DocAElstein in forum Test Area
    Replies: 39
    Last Post: 03-20-2018, 04:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •