+ Reply to Thread
Results 1 to 16 of 16

UDF to set borders or draw line in current cell

  1. #1
    Registered User
    Join Date
    11-25-2012
    Location
    Nijmegen, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    3

    UDF to set borders or draw line in current cell

    I'd like to have a UDF, e.g. Function DrawLine (LinTyp as String) as string

    Such that if cell A1 contains "\", then a cell containing: =DrawLine(A1) , should draw a DiagonalDown line
    Similar:
    =DrawLine("/") draws a DiagonalUp,
    =DrawLine("H") draws a line on the EdgeTop
    =DrawLine("L") draws a line on the EdgeDown

    etc, etc. In fact I managed to achieve something using formatting the borders of a cell. Unfortunatly the piece of code I have (generated with the Record Macro feature), crashes Excel everytime I enter the function in a cell.

    So how would it be possible to have such a function to draw lines (or alternatively set borders)??

    _Wim_

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: UDF to set borders or draw line in current cell

    I don't think a UDF can do that.
    An Event macro would be a better choice.
    Gary's Student

  3. #3
    Registered User
    Join Date
    11-25-2012
    Location
    Nijmegen, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: UDF to set borders or draw line in current cell

    Care to eleborate more on Event macro? I don't know this feature..

    Then again, why would this code not do the job: (just an initial piece of code, it always draws a / in side a cell)
    Please Login or Register  to view this content.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    UDFs can't change anything on a worksheet including formatting, they can only return a value.

    What you could use is the worksheet''s Change event.

    That would work if the /,H etc are entered manually.
    If posting code please use code tags, see here.

  5. #5
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: UDF to set borders or draw line in current cell

    Seems to work on the Selected cell rather than the cell containing the formula.

  6. #6
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: UDF to set borders or draw line in current cell

    Norie:

    Try the OP's code...........It does seem to affect the formatting?!

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Strange, the OP said that entering it in a cell crashed Excel.

  8. #8
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: UDF to set borders or draw line in current cell

    Norie:

    I modified the UDF as follows:

    Please Login or Register  to view this content.
    1. Make sure A1 is empty
    2. In another cell, enter: =DrawLine(A1)
    3. Set A1=12

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    That's quite different from the OP's code.

  10. #10
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: UDF to set borders or draw line in current cell

    Norie:

    The point is that the OP made an interesting discovery. It seems that UDFs can do three things:
    1. Enter values in cells
    2. Enter Comments in cells
    3. Enter Borders in cells (sometimes)

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: UDF to set borders or draw line in current cell

    Do you have examples of 1 & 2?

    Whenever I've tried them I've ended up with #VALUE errors, circular references and various other things.

  12. #12
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: UDF to set borders or draw line in current cell

    We will use the OP's code with modifications:

    Please Login or Register  to view this content.
    1. Clear cell A1
    2. In another cell enter: =DrawLine(A1)
    3. Enter 3 in cell A1
    4. Clear cell A1


    At step #3 the diagonal boarder appears and the comment appears
    At step #4 the comment vanishes, but the diagonal border remains!

    Can you verify what I am seeing??

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: UDF to set borders or draw line in current cell

    What about 1?

  14. #14
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: UDF to set borders or draw line in current cell

    For 1, the UDF has always been able to put its returned value into the cell containing the UDF.

  15. #15
    Registered User
    Join Date
    11-25-2012
    Location
    Nijmegen, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: UDF to set borders or draw line in current cell

    It seems that the code sets the border, but doesn't reset... I changed the code as below.

    Set cell A1, B1, C1, D1, to resp "0", "1", "2", "3". Lets refer to as "controlling cells"
    set cell A4, B4, C4, D4 to =DrawLine(A1), =DrawLine(B1), =DrawLine(C1), =DrawLine(D1),

    What happens is that cell B4 and C4 will get a DiagonalUp border, respectivly a DiagonalDown border. Cells A4 and D4 gets a comment

    Now change cells A1 to 1, then to 2, then to 3 What happens is that indeed the comment field follows the "controlling cell", but the Diagonal borders are just added and never removed... So ".LineStyle = xlNone" does not clear the border...
    Would it anyway possible to let the function behave as expected?



    Please Login or Register  to view this content.

  16. #16
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: UDF to set borders or draw line in current cell

    It seems that UDFs can affect cell borders. Just not reliably.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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