I am a beginner with VBA and I have a schedule (screenshot below) that i am trying to alter using a userform.
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).
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. 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)
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!!!
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
Logic:
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: