如何使用 VBA 根据值和组合框选择填充 excel 中的行?
How to populate rows in excel based on value and combobox selection using VBA?
我是 VBA 的初学者,我有一个时间表(下面的屏幕截图),我正在尝试使用用户表单进行更改。
我有一个带有 3 个组合框的用户窗体,1 个用于要执行的过程类型,然后 1 个用于开始时间,另一个用于结束时间(这些时间基于屏幕截图中的第一行)。
我想使用用户表单针对特定过程从本质上更改电子表格。例如,假设我想将过程 7 更改为从 2:00 持续到 19:00。在这种情况下,我希望看到所有包含 7 行的行在这些列下填充 7(参见结果屏幕截图)
我所拥有的只是查找哪些行包含特定值然后该值被抵消的代码...但老实说我不知道从这里去哪里...非常感谢您的帮助!!!
Dim c As Range
Dim firstAddress As String
Count = 7
With Sheet49.Range("AA7:AA636")
Set c = .Find(7, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do While Not c Is Nothing
Count = Count + 1
c.Offset(, -8).Value = c.Value
Set c = .FindNext(c)
If Count = 636 Then
Exit Do
End If
Loop
End If
End With
i want to change procedure 7 to last from 2:00 until 19:00. In a case like that then i would want to see all the rows that have 7 in them to be filled in with 7 under those columns
逻辑:
- 找到开始和结束时间列
- 找到相关的程序,然后一次性填满整个范围!
代码:
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim ColA As Long, ColB As Long
Dim aCell As Range, bCell As Range
Dim i As Long
'~~> I am hardcoding these values. Pick them up from combobox
Dim ProcToFind As Long: ProcToFind = 7
Dim StartTime As String: StartTime = "2:00"
Dim EndTime As String: EndTime = "19:00"
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Find the start and end time columns
'~~> Hardcoded 21. Replace with last column
For i = 1 To 21
Select Case TimeValue(Format(.Cells(1, i).Value, "hh:mm:ss"))
Case TimeValue(StartTime): ColA = i
Case TimeValue(EndTime): ColB = i
End Select
Next i
If ColA = 0 Or ColB = 0 Then
MsgBox "Unable to find start or end time column(s)"
Exit Sub
End If
Set aCell = .Cells.Find(What:=ProcToFind, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
'~~> Fill the entire range in 1 go
.Range(.Cells(aCell.Row, ColA), .Cells(aCell.Row, ColB)).Value = ProcToFind
Do
Set aCell = .Cells.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
'~~> Fill the entire range in 1 go
.Range(.Cells(aCell.Row, ColA), .Cells(aCell.Row, ColB)).Value = ProcToFind
Else
Exit Do
End If
Loop
End If
End With
End Sub
正在行动:
我是 VBA 的初学者,我有一个时间表(下面的屏幕截图),我正在尝试使用用户表单进行更改。
我有一个带有 3 个组合框的用户窗体,1 个用于要执行的过程类型,然后 1 个用于开始时间,另一个用于结束时间(这些时间基于屏幕截图中的第一行)。
我想使用用户表单针对特定过程从本质上更改电子表格。例如,假设我想将过程 7 更改为从 2:00 持续到 19:00。在这种情况下,我希望看到所有包含 7 行的行在这些列下填充 7(参见结果屏幕截图)
我所拥有的只是查找哪些行包含特定值然后该值被抵消的代码...但老实说我不知道从这里去哪里...非常感谢您的帮助!!!
Dim c As Range
Dim firstAddress As String
Count = 7
With Sheet49.Range("AA7:AA636")
Set c = .Find(7, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do While Not c Is Nothing
Count = Count + 1
c.Offset(, -8).Value = c.Value
Set c = .FindNext(c)
If Count = 636 Then
Exit Do
End If
Loop
End If
End With
i want to change procedure 7 to last from 2:00 until 19:00. In a case like that then i would want to see all the rows that have 7 in them to be filled in with 7 under those columns
逻辑:
- 找到开始和结束时间列
- 找到相关的程序,然后一次性填满整个范围!
代码:
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim ColA As Long, ColB As Long
Dim aCell As Range, bCell As Range
Dim i As Long
'~~> I am hardcoding these values. Pick them up from combobox
Dim ProcToFind As Long: ProcToFind = 7
Dim StartTime As String: StartTime = "2:00"
Dim EndTime As String: EndTime = "19:00"
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Find the start and end time columns
'~~> Hardcoded 21. Replace with last column
For i = 1 To 21
Select Case TimeValue(Format(.Cells(1, i).Value, "hh:mm:ss"))
Case TimeValue(StartTime): ColA = i
Case TimeValue(EndTime): ColB = i
End Select
Next i
If ColA = 0 Or ColB = 0 Then
MsgBox "Unable to find start or end time column(s)"
Exit Sub
End If
Set aCell = .Cells.Find(What:=ProcToFind, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
'~~> Fill the entire range in 1 go
.Range(.Cells(aCell.Row, ColA), .Cells(aCell.Row, ColB)).Value = ProcToFind
Do
Set aCell = .Cells.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
'~~> Fill the entire range in 1 go
.Range(.Cells(aCell.Row, ColA), .Cells(aCell.Row, ColB)).Value = ProcToFind
Else
Exit Do
End If
Loop
End If
End With
End Sub
正在行动: