【问题标题】:Increment Cell Values in a Range by 1 with VBA Excel使用 VBA Excel 将范围内的单元格值增加 1
【发布时间】:2018-07-25 23:01:14
【问题描述】:

我目前正在尝试实现插入新行值和自动复选框插入器。

我目前有以下代码分布在不同的按钮上,因此也有不同的 Subs。我已将需要增加 1 个单元格的关键信息加粗。这将在单击“InsertNewBill”按钮后发生。:

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

我需要所有加粗的值都加一。即从 F30 到 F31 和 A30:AC30 到 A31:AC31。 这个值也需要从 InsertNewBill_Click 子传递到 DeleteTickBoxes_Click 子。

我假设我需要删除 Private 子并且可能有一个公共整数变量? 我只是不确定如何在每次单击按钮后仅将数字增加 1。

感谢您的所有帮助

【问题讨论】:

  • 遍历范围内的单元格并执行MyCell.Value = MyCell.Value + 1 ?
  • 我正在考虑将 DeleteTickBox 和 InsertNewBill 子组合在一起,以允许整数增量的结转。这样我就可以在按下一个按钮的情况下完成所有这一切。我只是想看看我是否可以整理出整数增加
  • @tigeravatar 如果我要将单元格值增加 1。那么我将如何引用 range("A30:AC30").Select。我很抱歉,我对此比较陌生

标签: vba excel


【解决方案1】:
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 是 3 个范围中的 Union()
  • myCell 是一个 Range,用于循环遍历 unionRange 中的所有单元格;
  • myCell = myCell + 1 将值增加 1。
  • If myCell.Font.Bold Then 检查单元格是否为粗体

【讨论】:

  • 这太巧妙了!谢谢你。我会对此进行测试。到目前为止谢谢。好的,对。我有点坚持将其实施到我当前的项目中。我可以成为一个巨大的痛苦,并要求一些指示。我道歉
  • @Batteredburrito - 不客气。我希望它有效:)
  • @Jeeped - 是的,如果Set unionRange = ws.Range("A30:AC31"),可以避免联合。不过,我想,展示联合的工作方式会使示例更加灵活。
  • @Vityata 因为我对此很陌生。我将对原始帖子进行一些进一步的编辑,因为我认为我的意图不是很清楚。这在一定程度上确实有效,但我认为它不能完全满足我目前的需要
  • @Batteredburrito - 请不要将操作更改为使现有答案无效的程度。通过标记答案并提出新问题来结束此问题。
猜你喜欢
  • 2020-02-09
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
相关资源
最近更新 更多