简体   繁体   English

如何使用 VBA 根据值和 combobox 选择填充 excel 中的行?

[英]How to populate rows in excel based on value and combobox selection using VBA?

I am a beginner with VBA and I have a schedule (screenshot below) that i am trying to alter using a userform.我是 VBA 的初学者,我有一个计划(下面的屏幕截图),我正在尝试使用用户表单进行更改。

I have a Userform with 3 comboboxes, 1 for the type of procedure to be performed and then 1 for the start time and another for the end time (these times are based off the first row in the screenshot).我有一个带有 3 个组合框的用户窗体,1 个用于要执行的程序类型,然后 1 个用于开始时间,另一个用于结束时间(这些时间基于屏幕截图中的第一行)。

在此处输入图像描述

I want to essentially alter the spreadsheet for a specific procedure using the userform.我想从本质上改变使用用户表单的特定过程的电子表格。 For example, lets say i want to change procedure 7 to last from 2:00 until 19:00.例如,假设我想将程序 7 更改为从 2:00 持续到 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 (see result screenshot)在这种情况下,我希望看到所有包含 7 的行在这些列下用 7 填充(见结果截图)

在此处输入图像描述

All i have is code to find which rows contain a certain value and then that value is offset... but i honestly dont know where to go from here... Any help is greated appreciated!!!我所拥有的只是查找哪些行包含某个值然后该值被偏移的代码......但老实说,我不知道从这里到 go 的位置......任何帮助都非常感谢!

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.我想将程序 7 更改为从 2:00 持续到 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在这种情况下,我希望看到所有包含 7 的行在这些列下用 7 填充

Logic:逻辑:

  1. Find the start and end time columns查找开始和结束时间列
  2. Find the relevant procedure and then fill the entire range in 1 go!找到相关程序,然后在 1 中填写整个范围!

Code:代码:

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

In Action:在行动:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM