由于最后一个问题我不太清楚。我是新手,在实现所有答案方面需要一些指导。
我目前有以下代码分布在不同的按钮上,因此分布在不同的Subs上。为了清楚起见,我将代码中需要增加1的范围分开,并在每次按下按钮时将需要加1的值加粗了。我很抱歉这破坏了格式,对不起。
点击“ InsertNewBill”按钮后,数字就会增加:
Private Sub InsertNewBill_Click()
'I AM USING i TO STORE THE CELL INCREMENT, IT CURRENTLY DOES NOTHING**
Dim i As Integer
范围(“ A30:AC30” )。选择
范围(“ AC30” )。激活
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
设置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。
感谢您的帮助
答案 0 :(得分:2)
您应该尝试停止使用Select
和Activate
-在这种情况下,您可以Copy
范围:
Range(cells(30,"A"),cells(30,"AC")).Copy
要将i添加为偏移量,可以使用:
Range(cells(30+i,"A"),cells(30+i,"AC")).Copy
或者您可以使用:
Range(cells(30,"A"),cells(30,"AC")).Offset(i,0).Copy
要使我在工作簿中的其他Subs上工作,请将其声明为Public
。
除非您确定该值会保持较低,否则建议将其声明为Long
,而不是Integer
。
对于Set CellRange = ActiveSheet.Range("E7:F30")
设置,您可以使用:
Set CellRange = ActiveSheet.Range("E7:F" & 30 + i)
答案 1 :(得分:1)
使用公共变量并将其连接到您的地址中。在工作簿打开事件中将值初始化为30。
此工作簿:
Option Explicit
Public i As Long
Private Sub Workbook_Open()
i = 30
End Sub
标准模块:
Option Explicit
Private Sub button2_Click()
ThisWorkbook.i = ThisWorkbook.i + 1
Debug.Print ActiveSheet.Range("E" & ThisWorkbook.i & ":F" & ThisWorkbook.i).Address
End Sub
答案 2 :(得分:0)
对于那些感兴趣的人。这是我根据自己的目的最终使用的VBA。
namespace :db do
task :full_reset do
on roles(:app) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, "exec rake db:full_reset"
end
end
end
end
end
*
Sub InsertNewBill_Click()
Dim rngCopy As range
Dim rngPaste As range
Dim clearCell As range
i = Cells(30, 1) + 1
Cells(30, 1) = i
ActiveSheet.range("A" & i & ":AD" & i).Insert
Set rngPaste = range("A" & i & ":AD" & i)
Set rngCopy = ActiveSheet.range("A" & i - 1 & ":AD" & i - 1)
rngCopy.Copy
rngPaste.PasteSpecial Paste:=xlPasteAll
With ActiveWorkSheet
Cells(i, 1).ClearContents
End With
Call DeleteTickBoxes_Click
End Sub