VBA range("single cell selected based on employee name and start time of shift").value = 1

Joined
Dec 8, 2021
Messages
26
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I feel like I may be overthinking this but after hours and hours of testing different codes I am bringing it to y'all for some help.

What code can I use to select lets say Fake Guy 3 and have him start work at 5:45 AM. I want to select and enter a 1 in cell G16 in that instance

daily wrk Schedule 5 min increments NEWMR EXCEL.xlsm
DEFGHIJ
524 hour TimeFAKE GUY 1FAKE GUY 2FAKE GUY 3FAKE GUY 4FAKE GUY 5FAKE GUY 6
6
75:00 AM
85:05 AM
95:10 AM
105:15 AM
115:20 AM
125:25 AM
135:30 AM
145:35 AM
155:40 AM
165:45 AM
175:50 AM
185:55 AM
Schedule


sorry not sure why my table posted all crunched up like that:

1651918371525.png


I want this to be triggered by selecting the start time from a drop down menu on a separate sheet and I want it to be able to adjust based upon the selection made in te drop down menu.



daily wrk Schedule 5 min increments NEWMR EXCEL.xlsm
BC
5Namestart
6FAKE GUY 1
7FAKE GUY 2
8FAKE GUY 35:45 AM
9FAKE GUY 4
10FAKE GUY 5
11FAKE GUY 6
12FAKE GUY 7
13FAKE GUY 8
Sheet1
Cells with Data Validation
CellAllowCriteria
C6:C13List=MondayShift



Thank you in advance
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
VBA Code:
Sub Macro1()
   
   
' i starts as the first name on your data entry list. As the loop
' progresses it will look for the second, third, fourth... names.
'
' The loop will continue for as long as you have names in range B6:B~
' of Sheet1. The macro will stop as soon as an empty cell is reached,
' even if there are more rows with data below!
'
' This macro removes existing data for the people that are replanned,
' but it does not remove the data from people without a new plan!


' Determine the last row and column with data on worksheet Schedule.
Sheets("Schedule").Select
With ActiveSheet
    LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
    lastcolumn = .Cells(5, Columns.Count).End(xlToLeft).Column
End With


i = 6
Do While Len(Sheets("Sheet1").Range("B" & i)) > 0

    Employee = Sheets("Sheet1").Range("B" & i)
    Starttime = Sheets("Sheet1").Range("C" & i)

    If Len(Starttime) > 0 Then
        ' Identify the desired column number in the specified range on worksheet SCHEDULE
            WsSchedule_Column = WorksheetFunction.Match(Employee, Sheets("Schedule").Range(Cells(5, 5), Cells(5, lastcolumn)), 0)
   
        ' Identify the desired row number in the specified range on worksheet SCHEDULE
            WsSchedule_Row = WorksheetFunction.Match(Starttime, Range("Schedule!D6:D" & LastRow), 0)
       
        ' Remove existing planning
            Range(Cells(6, WsSchedule_Column + 4), Cells(LastRow, WsSchedule_Column + 4)).ClearContents

        ' Add a "1" in the desired cell.
            Sheets("Schedule").Range("D5").Offset(WsSchedule_Row, WsSchedule_Column).FormulaR1C1 = 1
    End If

i = i + 1
Loop



End Sub
 
Upvote 0
Solution
I have an amateur question for you. I keep getting this compule error when it hits Lastrow


VBA Code:
Sub Macro1()
 
 
' i starts as the first name on your data entry list. As the loop
' progresses it will look for the second, third, fourth... names.
'
' The loop will continue for as long as you have names in range B6:B~
' of Sheet1. The macro will stop as soon as an empty cell is reached,
' even if there are more rows with data below!
'
' This macro removes existing data for the people that are replanned,
' but it does not remove the data from people without a new plan!


' Determine the last row and column with data on worksheet Schedule.
Sheets("Schedule").Select
With ActiveSheet
    LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
    lastcolumn = .Cells(5, Columns.Count).End(xlToLeft).Column
End With


i = 6
Do While Len(Sheets("Sheet1").Range("B" & i)) > 0

    Employee = Sheets("Sheet1").Range("B" & i)
    Starttime = Sheets("Sheet1").Range("C" & i)

    If Len(Starttime) > 0 Then
        ' Identify the desired column number in the specified range on worksheet SCHEDULE
            WsSchedule_Column = WorksheetFunction.Match(Employee, Sheets("Schedule").Range(Cells(5, 5), Cells(5, lastcolumn)), 0)
 
        ' Identify the desired row number in the specified range on worksheet SCHEDULE
            WsSchedule_Row = WorksheetFunction.Match(Starttime, Range("Schedule!D6:D" & LastRow), 0)
     
        ' Remove existing planning
            Range(Cells(6, WsSchedule_Column + 4), Cells(LastRow, WsSchedule_Column + 4)).ClearContents

        ' Add a "1" in the desired cell.
            Sheets("Schedule").Range("D5").Offset(WsSchedule_Row, WsSchedule_Column).FormulaR1C1 = 1
    End If

i = i + 1
Loop



End Sub

VBA Code:
Sub Macro1()
  
  
' i starts as the first name on your data entry list. As the loop
' progresses it will look for the second, third, fourth... names.
'
' The loop will continue for as long as you have names in range B6:B~
' of Sheet1. The macro will stop as soon as an empty cell is reached,
' even if there are more rows with data below!
'
' This macro removes existing data for the people that are replanned,
' but it does not remove the data from people without a new plan!


' Determine the last row and column with data on worksheet Schedule.
Sheets("Schedule").Select
With ActiveSheet
    LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
    lastcolumn = .Cells(5, Columns.Count).End(xlToLeft).Column
End With


i = 6
Do While Len(Sheets("Sheet1").Range("B" & i)) > 0

    Employee = Sheets("Sheet1").Range("B" & i)
    Starttime = Sheets("Sheet1").Range("C" & i)

    If Len(Starttime) > 0 Then
        ' Identify the desired column number in the specified range on worksheet SCHEDULE
            WsSchedule_Column = WorksheetFunction.Match(Employee, Sheets("Schedule").Range(Cells(5, 5), Cells(5, lastcolumn)), 0)
  
        ' Identify the desired row number in the specified range on worksheet SCHEDULE
            WsSchedule_Row = WorksheetFunction.Match(Starttime, Range("Schedule!D6:D" & LastRow), 0)
      
        ' Remove existing planning
            Range(Cells(6, WsSchedule_Column + 4), Cells(LastRow, WsSchedule_Column + 4)).ClearContents

        ' Add a "1" in the desired cell.
            Sheets("Schedule").Range("D5").Offset(WsSchedule_Row, WsSchedule_Column).FormulaR1C1 = 1
    End If

i = i + 1
Loop



End Sub

I keep getting this error when I run the code. Sorry I am still an amateur in some areas with VBA

r
Screenshot 2022-05-08 115116.jpg
 
Upvote 0
Strange, it does work for me.

Are the worksheets still called Schedule and Sheet1?

You could try adding this at the beginning of the macro:

VBA Code:
Dim LastRow As Integer
Dim lastcolumn As Integer
 
Upvote 0
YEP that did it! One more thing @petertenthije ...lets say I want to add the 1 to that cell then I want VBA to copy a 1 for the next 108 cells (if they ae fulltime) would you recommend I use a XLFillValues code for that?


Strange, it does work for me.

Are the worksheets still called Schedule and Sheet1?

You could try adding this at the beginning of the macro:

VBA Code:
Dim LastRow As Integer
Dim lastcolumn As Integer
 
Upvote 0
lets say I want to add the 1 to that cell then I want VBA to copy a 1 for the next 108 cells


Replace:
Sheets("Schedule").Range("D5").Offset(WsSchedule_Row, WsSchedule_Column).FormulaR1C1 = 1


With:
Sheets("Schedule").Range("D5").Offset(WsSchedule_Row, WsSchedule_Column).Select
Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(108, 0)).FormulaR1C1 = 1
 
Upvote 0
Replace:
Sheets("Schedule").Range("D5").Offset(WsSchedule_Row, WsSchedule_Column).FormulaR1C1 = 1


With:
Sheets("Schedule").Range("D5").Offset(WsSchedule_Row, WsSchedule_Column).Select
Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(108, 0)).FormulaR1C1 = 1
You are amazing!
 
Upvote 0

Forum statistics

Threads
1,215,216
Messages
6,123,669
Members
449,114
Latest member
aides

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top