Consulting

Results 1 to 3 of 3

Thread: Solved: Need help with toggle button

  1. #1

    Solved: Need help with toggle button

    Hi

    I currently have been using a Ctrl+T to toggle between two macros that either add or delete a dividing line (offet is 27 columns from active cell) between groups of data in a spreadsheet. I would like to change this to one, form-button (instead of Ctr+T). In other words ... click the button to add the dividing line, click the same button to delete the line. I did a test but am finding it does not work when changing to the next macro name. Any help would be greatly appreciated. Below is my code:

    [vba]Sub Macro_ADD_LINE()
    Range(ActiveCell, ActiveCell.Offset(0, 27)).Select
    'add bottom border:
    With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With

    'add bold, right border:
    With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

    'change button command to delete line:
    ActiveSheet.Shapes("Button 1").Select
    Selection.OnAction = "Macro_DELETE_LINE"

    Sheets("Sheet1").Select
    Range(ActiveCell, ActiveCell.Offset(0, 0)).Select

    End Sub


    Sub Macro_DELETE_LINE()
    'delete line:
    Range(ActiveCell, ActiveCell.Offset(0, 27)).Select
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

    'change button command to add line:
    ActiveSheet.Shapes("Button 1").Select
    Selection.OnAction = "Macro_ADD_LINE"

    Sheets("Sheet1").Select
    Range(ActiveCell, ActiveCell.Offset(0, 0)).Select

    End Sub[/vba]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [VBA]

    Sub Macro_LINE()
    Dim btn As Object

    Set btn = ActiveSheet.Buttons(Application.Caller)
    If btn.Caption = "Add line" Then

    With Range(ActiveCell, ActiveCell.Offset(0, 27))

    'add bottom border:
    With .Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With

    'add bold, right border:
    With .Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    .Borders(xlInsideHorizontal).LineStyle = xlNone
    End With

    'change button command to delete line:
    btn.Caption = "Delete line"
    Else

    'delete line:

    With Range(ActiveCell, ActiveCell.Offset(0, 27))

    .Borders(xlEdgeBottom).LineStyle = xlNone
    .Borders(xlEdgeRight).LineStyle = xlNone
    End With

    'change button command to add line:
    btn.Caption = "Add line"
    End If

    Sheets("Sheet1").Select
    Range(ActiveCell, ActiveCell.Offset(0, 0)).Select

    End Sub
    [/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thank you, Thank you!

    It worked perfectly.

Posting Permissions

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