VBA Cells to move down

Jyters

New Member
Joined
Jul 7, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I am building an interactive dashboard for our Asphalt Department and when they do their program or planning one of the queries is that when they have planned work on a specific date, and, it gets rained off. They have the option to select rained off. but they want the planned dates below that to automatically shift down a day. Ultimately pushing the program out when ever there is a rained off day.

In the attached example. When Rain off in Column H is selected all cells below except Column B will move down by the number of days in Column D in the row of Rain Off.


Thanks,
 

Attachments

  • Northern Program.PNG
    Northern Program.PNG
    31.1 KB · Views: 28

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
jyters Let's get the ball a rollin. First is this close to what you had in mind. I cut your dashboard down but it should still work. Now one of the questions I have is notice that the 5th day is on a Saturday. Also note that sometimes the number of days could go thru a Sunday. Do you want the schedule showing work on the weekend? Once you approve or change the below we can move on to the next step.

24-07-11 Nothern Program rev A.xlsm
ABCDEFGHIJK
1
2RegionDate StartDayDuration WorkingDay FinishSupervisiorManhoursClient Work OrderActivity CodesLocation
3
4Northern1-Jul-24Mon11-Jul-24JohanDairy5569Black Water
52-Jul-24Tue
6Northern3-Jul-24Wed13-Jul-24JohanRain Off5569River Run
74-Jul-24Thu
85-Jul-24Fri
96-Jul-24Sat
107-Jul-24Sun
11Northern8-Jul-24Mon512-Jul-24Johan8Dairy5569Black Water
129-Jul-24Tue
1310-Jul-24Wed
1411-Jul-24Thu
1512-Jul-24Fri
1613-Jul-24Sat
1714-Jul-24Sun
1815-Jul-24Mon
Input



24-07-11 Nothern Program rev A.xlsm
ABCDEFGHIJK
1
2RegionDate StartDayDuration WorkingDay FinishSupervisiorManhoursClient Work OrderActivity CodesLocation
3
4Northern1-Jul-24Mon11-Jul-24JohanDairyBlack Water
52-Jul-24Tue
63-Jul-24WedRain Off
7Northern4-Jul-24Thu14-Jul-24Johan5569River Run
85-Jul-24Fri
96-Jul-24Sat
107-Jul-24Sun
118-Jul-24Mon
12Northern9-Jul-24Tue513-Jul-24Johan8Dairy5569Black Water
1310-Jul-24Wed
1411-Jul-24Thu
1512-Jul-24Fri
1613-Jul-24Sat
1714-Jul-24Sun
1815-Jul-24Mon
Output
 
Upvote 0
Hi there, thank you so much for your response. What is shown is what I was asking. Just want the dates below the rain off to shift down, as the power query would return a blank date for the rain off as shown. The dates are set as Workday.Intl as we mostly work at nights, Sonday evenings will be our 1st working day. Saturdays will be a non working day. :) I do however have another question on the same worksheet, do i have to create a different threat? Regards,
 
Upvote 0
jyters Any other questions about this worksheet use this thread. So, it sounds like the workers don't want to work in the heat of the day so let's start the workday at night. So it sounds like the work week starts Sunday night to Monday morning and the work week ends Friday night to Saturday morning. This shouldn't be a problem with the schedule. Now I believe the best solution to this problem would be to use a program or macro. Do you know anything about visual basic programing that is connected to excel? If you don't, it just means we have to go a little slower. If you do know then we can cut to the chase.
 
Upvote 0
jyters Any other questions about this worksheet use this thread. So, it sounds like the workers don't want to work in the heat of the day so let's start the workday at night. So it sounds like the work week starts Sunday night to Monday morning and the work week ends Friday night to Saturday morning. This shouldn't be a problem with the schedule. Now I believe the best solution to this problem would be to use a program or macro. Do you know anything about visual basic programing that is connected to excel? If you don't, it just means we have to go a little slower. If you do know then we can cut to the chase.
Thanks, i have very little knowledge of VBA, i only have researched some VBA's I am using, Currently also on this worksheet there is a Worksheet protection VBA. I have recorded and used macros before, but slow is better. As i would like to understand what is being coded. Another question on this query, we have about 8 Regions/Crews running. Can the VBA code have a section where there are more worksheets added, the worksheet name can be added in the code for the VBA to work on those sheets?
 
Upvote 0
jyters VBA is very powerful and yes you can add worksheets to a workbook using VBA. We just have to take it one step at a time so let's work on the problem above.
 
Upvote 0
jyters here is my solution to the first problem. Now I usually get something wrong so let me know what it is. I am going to assume you know how to copy and paste this program to your excel workbook and save it as an Excel Macro. This usually generates more questions than answers so let the discussion begin.


VBA Code:
Sub RainDelay()
Dim CellCnt As Integer
Dim strRng As String
Dim Row1 As Integer
'this line will count how many rows in column H
CellCnt = Cells(Rows.Count, "H").End(xlUp).Row
'we loop from the bottom of H up
For i = CellCnt To 3 Step -1
'we go from the bottom up because you could have another rain day after the first rain day
If Cells(i, 8) = "Rain Off" Then
Row1 = i
For n = CellCnt To i Step -1
'this if statement fills down the number of days. Now you will have 2 dates showing
If Cells(Row1, 4) <> "" Then
    Range((Cells(Row1, 4)), Cells(Row1 + 1, 4)).Select
    Selection.FillDown
    Row1 = Row1 + 1
End If
 
Row1 = Row1 + 1
Next n

'this line just makes a string that looks like this Range("Ai,Fi:Gi,Ii:Ki,Hi).Now the i is a variable for the row number. The , and : gives us the spacing.
strRng = "A" & i & "," & "F" & i & ":" & "G" & i & "," & "I" & i & ":" & "K" & i & "," & "H" & i + 2
Range(strRng).Select
'this line just shifts the selected cells down one cell.
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'we have to exit the loop after this operation.
Exit For
End If

Next i

Range("A1").Select

End Sub

24-07-11 Nothern Program rev G.xlsm
ABCDEFGHIJK
1
2RegionDate StartDayDuration WorkingDay FinishSupervisiorManhoursClient Work OrderActivity CodesLocation
3
4Northern01-Jul-24Mon1Tuesday, July 2, 2024JohanDairy5569Black Water
502-Jul-24Tue 
6Northern03-Jul-24Wed2Friday, July 5, 2024JohanDairy5569River Run
704-Jul-24Thu 
805-Jul-24Fri 
906-Jul-24Sat 
1007-Jul-24Sun 
11Northern08-Jul-24Mon5Sunday, July 14, 2024Johan8Dairy5569Black Water
1209-Jul-24Tue 
1310-Jul-24Wed 
1411-Jul-24Thu 
1512-Jul-24Fri 
1613-Jul-24Sat 
1714-Jul-24Sun 
1815-Jul-24Mon 
1916-Jul-24Tue 
2017-Jul-24Wed 
2118-Jul-24Thu 
Work Schedule
Cell Formulas
RangeFormula
C4:C21C4=WEEKDAY(B4)
E4:E21E4=IF(D4="","",WORKDAY.INTL(B4,D4,17))
 
Upvote 0
Hi, thanks, but i did copy and paste firstly created a new module, changed the "H" to "G" as since i have made the query changed some columns. After I created a new module and saved the document as xlsm re-open it and selected the Rain Off on Column "G" nothing happened. Also, copied the VBA below an existing VBA for Protect Password, Changed the column back to "H" and added a selection for Rain Off. It still did not work. hehe as you predicted there might have been something missing :)
 
Upvote 0
jyters if I am reading you correctly, it sounds like you made changes to the Work Schedule worksheet. A problem there is the program really only works with the worksheet above. Let's try this. You can open a new workbook, go to the above worksheet, Work Schedule, go to just below the upper left corner, copy and paste to the new workbook. Then copy and paste the program to a module in the new workbook. It sounds like you have a handle on that part. I need to have you see the program work and say yes that is what I want. Or no that isn't what I want and here is what I want. You know jeyters, I too had problems with the Rain Off if statement. I finally just went to the program and copied the Rain Off from the program and pasted it in a cell. So if you have any questions let us know.
 
Upvote 0
Hi, sorry for my delayed response. On your message when you send me the VBA, there is no attachment to test the scenario. And I cant edit the cells on the message either. So there is no way for me to confirm if it indeed works. Furthermore, you mention that I should copy and paste the VBA on my worksheet, which I did and had to make the changes as I had changed the columns in the original example. I did not save the original example send. if that is where you meant for me to copy and paste the VBA? Look forward to your reply..
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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