我正在开发一个包含300行的Excel VBA项目,每天都会填充外发订单。每行将有4个命令按钮和各种宏。第一个是“TIME IN”,这将在订单开始时记录,并在单页1的列中放置“IN PROGRESS”字样。单击时,下一个按钮将为“TIME OUT”,这将记录完成时间在正确的列中单词“COMPLETE”并将整行移动到单独的工作簿。第三个按钮将是“HOLD”,这将记录加载时保持的时间,将整行移动到同一工作簿的第3页,并在相应的列中添加“PARTIAL HOLD”字样。第四个按钮将在第3页上,它将是一个“RESUME”按钮。此按钮将从表3中将行发送回表单1,并在相应列中显示“IN PROGRESS”字样。我的问题是,是否可以批量制作这些按钮和相应的宏,或者我是否必须制作与其一起使用的每个按钮和宏?如果可以批量进行,请告诉我如何。下面是我到目前为止单独创建的每个按钮的命令按钮代码和相关宏的示例。所有这些都具有相同的基本代码和宏相关联。感谢您帮助我更有效地写作。 命令按钮
Private Sub HOLD1_Click()
Sheet1.Cells(5, 17).Value = Format$(Now, "hh:nn:ss")
HOLD_1
End Sub
宏
Sub HOLD_1()
'
' HOLD_1 Macro
'
'
Range("M5").Select
ActiveCell.FormulaR1C1 = "PARTIAL HOLD"
Range("M6").Select
End Sub
答案 0 :(得分:0)
"可点击的单元格的示例"在我的第一条评论中提到
...如果A到K列包含数据,我会将列L到O点击 (不同的背景颜色),使用SelectionChange()事件 允许您确定每个单元格的坐标,并构建 具体操作取决于当前行
将此Sub
放入 ThisWorkbook
模块
Option Explicit
'In ThisWorkbook module - Sh parameter contains the sheet being used (ActiveSheet)
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
With Target
Select Case Sh.Name
Case "Sheet1", "Sheet2", "Sheet3" 'Processing the first 3 sheets
If .Row > 1 And .CountLarge = 1 Then 'Ignore Headers & multiple cells
Select Case .Column
Case 1 To 4 'Ignore col A to D
.Interior.Color = RGB(255, 204, 204) 'Revert to initial
.Font.ColorIndex = xlAutomatic 'Default Black
Case 5 'Mouse clicked in a cell in col E
With Sh.Cells(.Row, "A") 'Change cell in same row, col A
.Interior.Color = RGB(190, 0, 0) 'Dark Red
.Font.Color = vbYellow
End With
.Interior.Color = RGB(255, 255, 204) 'Light Yellow
.Font.Color = vbRed 'current cell (in column E)
.Font.Bold = True
Case 6 'Mouse clicked in a cell in col F
With Sh.Cells(.Row, "B") 'Change cell in same row, col B
.Interior.Color = RGB(0, 0, 190) 'Dark Blue
.Font.Color = vbYellow
End With
.Interior.Color = RGB(255, 255, 204) 'Light Yellow
.Font.Color = vbRed
.Font.Bold = True
Case 7 'Mouse clicked in a cell in col G
If Len(.Value2) > 0 Then
With Sh.Cells(.Row, "C") 'Cell in same row, col B
.Interior.Color = RGB(255, 255, 0) 'vbYellow
.Font.Color = RGB(190, 0, 0) 'Dark Red
End With
End If
Case 8
.Value = Format(Now, "ddd mm-dd-yyyy")
.Font.Bold = True
.Offset(, 1).Value2 = "In Progress"
End Select
End If
Case "Sheet4", "Sheet5"
'...
End Select
End With
End Sub
结果,单击E到H列中的单个单元格
<强> Sheet1
强>
<强> Sheet2
强>
<强> Sheet3
强>
检测当前选择的另一种方法
Option Explicit
'In Sheet1 module
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Me.UsedRange.Columns(1)) Is Nothing Then
MsgBox "Clicked cell in Column 'A', Row: " & Target.Row
End If
End Sub
单击Sheet1.Cell(A5)
时的消息框
答案 1 :(得分:0)
非常感谢保罗。我结束了这个:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean)
Application.EnableEvents = False
On Error GoTo Xit
If Target.Column = 11 Then
Cancel = True
Target.Offset(, 2).Value = "IN PROGRESS"
Target.Offset(, 4).Value = Time
ElseIf Target.Column = 12 Then
Cancel = True
Target.Offset(, 1).Value = "COMPLETE"
Target.Offset(, 4).Value = Time
ElseIf Target.Column = 14 Then
Cancel = True
Target.Offset(, -1).Value = "PARTIAL HOLD"
Target.Offset(, 3).Value = Time
End If
Xit:
Application.EnableEvents = True
End Sub