2

Im currently trying to implement an insert new row value and a automatic checkbox inserter.

I currently have the following code spread over different buttons and therefore different Subs. I have boldened the key information that i will need to increment by 1 cell. This will occur after clicking the "InsertNewBill" button.:

Private Sub InsertNewBill_Click()
    'I AM USING i TO STORE THE CELL INCREMENT, IT CURRENTLY DOES NOTHING**
    Dim i As Integer
    '**range("A30:AC30").Select**
    '**range("AC30").Activate**
    Selection.Copy
    Selection.Insert Shift:=xlDown
End Sub

Private Sub DeleteTickBoxes_Click()
    'Variables
    Dim c As CheckBox
    Dim CellRange As Range
    Dim cel As Range
    Set CellRange = ActiveSheet.Range("E7:**F30**")    
    'Delete Checkboxes within the specified range above on the ActiveSheet Only
    For Each c In ActiveSheet.CheckBoxes
        If Not Intersect(c.TopLeftCell, CellRange) Is Nothing Then
            c.Delete
        End If
    Next    
    'Insert New Checkboxes and Assign to a specified link cell using the offset
    For Each cel In CellRange
        'you can adjust left, top, height, width to your needs
        Set c = ActiveSheet.CheckBoxes.Add(cel.Left, cel.Top, 30, 6)
        With c   'Clears the textbox so it has no text
            .Caption = ""
            'Offset works by offsetting (Row offset, Column Offset) and accepts
            'positive for down/right and negative for left/up,
            'keep in not that the linked cells will automatically populate with true/false
            .LinkedCell = cel.Offset(0, -4).Address
        End With
    Next
    Call CentreCheckbox_Click
End Sub

I need all boldened values to increase by one. I.e from F30 to F31 and A30:AC30 to A31:AC31. This value also needs to be carried across from the InsertNewBill_Click sub to the DeleteTickBoxes_Click sub.

I assume i will need to remove the Private sub and possibly have a public integer variable? Im just not sure how to implement increasing only the number by 1 after each button click.

All your help is appreciated

3
  • 1
    Loop through the cells in the range and do a MyCell.Value = MyCell.Value + 1 ? Commented Jul 25, 2018 at 14:38
  • I was thinking of combining both the DeleteTickBox and InsertNewBill subs together to allow the carrying over of the integer increment. This way i could have all of this happen under one button press. I just wanted to see if i can get the integer increase sorted out Commented Jul 25, 2018 at 14:39
  • @tigeravatar If i was to increase the cell value by 1. How would i then reference that in say range("A30:AC30").Select. I apologise, im relatively new to this Commented Jul 25, 2018 at 14:40

1 Answer 1

3
Sub TestMe()

    Dim unionRange As Range
    Dim ws As Worksheet
    Set ws = Worksheets(1)

    With ws
        'as an alternative -> Set unionRange = ws.Range("A30:AC31")
        Set unionRange = Union(.Range("F30:F31"), .Range("A30:AC30"), .Range("A31:AC31"))
    End With

    Dim myCell As Range
    For Each myCell In unionRange
        If myCell.Font.Bold Then
            myCell = myCell + 1
        End If
    Next

End Sub
  • unionRange is a Union() of the 3 ranges;
  • myCell is a Range and used to loop through all the cells in unionRange;
  • myCell = myCell + 1 increments the value by 1.
  • If myCell.Font.Bold Then checks whether the cell is bold.
6
  • 1
    This is ingenious! Thank you. I will put this in a test. Thank you so far. Okay, right. Im a little stuck on implementing this into my current project. Can i be a huge pain and ask for some pointers. I apologise Commented Jul 25, 2018 at 14:42
  • 1
    @Batteredburrito - you are welcome. I hope it works :)
    – Vityata
    Commented Jul 25, 2018 at 14:46
  • 1
    @Jeeped - yup, the union can be avoided, if Set unionRange = ws.Range("A30:AC31"). Still, showing how unions work makes the example more flexible, I guess.
    – Vityata
    Commented Jul 25, 2018 at 14:55
  • @Vityata Because i am very new to this. Im going to make some further edits to the original post because i dont think ive been very clear with my intentions. This definitely works to an extent but i dont think it does exactly what i need currently Commented Jul 25, 2018 at 14:57
  • 1
    @Batteredburrito - Please do not change the op to the point that it renders existing answers invalid. Close off this question by marking an answer and ask a new question.
    – user4039065
    Commented Jul 25, 2018 at 14:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.