Worksheet Change Filling & Emptying

CasualDabbler

New Member
Joined
Oct 29, 2018
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
Need the following 2 items please
Item 1
If a cell in a range (D4:D2000) has a value entered, the cell at Target.Offset(0, 5).Value = "09:00" and subsequent values "17:00", "09:00" etc
Sheet is a work in progress and may have other columns inserted, so can the solution factor easily changing target ranges.
I.e. I set the initial target "MonOpen" and the rest flow from that cell

Target.Offset(0, 5).Value = "09:00"
Target.Offset(0, +1).Value = "17:00" etc


MRE01.JPG


Item 2
If a cell in a range (D4:D2000) has its value cleared I need to clear the corresponding adjacent range. As per above sheet is WIP, looking to set the start of the range and just enter a width value

Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Create a variable with your initial target column

VBA Code:
n = 5
Target.Offset(0, n).Value = "09:00"
Target.Offset(0, n + 1).Value = "17:00"
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter an ID in column D and press the RETURN key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("D4:D2000")) Is Nothing Then Exit Sub
    Dim lCol As Long, x As Long
    lCol = Cells(3, Columns.Count).End(xlToLeft).Column
    If Target <> "" Then
        For x = 9 To lCol Step 2
            Cells(Target.Row, x).Resize(, 2) = Array("09:00", "17:00")
        Next x
    Else
        Cells(Target.Row, 9).Resize(, lCol - 4).ClearContents
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
If Column D remains the 'Target' column regardless of any column insertions then maybe tweak @mumps code as below?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("D4:D2000")) Is Nothing Then Exit Sub
    Dim lCol As Long, x As Long, fcol As Long
      
    lCol = Cells(3, Columns.Count).End(xlToLeft).Column
    fcol = WorksheetFunction.Match("MonOpen", Range(Cells(3, 1), Cells(3, lCol)))
    If Target <> "" Then
        For x = fcol To lCol Step 2
            Cells(Target.Row, x).Resize(, 2) = Array("09:00", "17:00")
        Next x
    Else
        Cells(Target.Row, fcol).Resize(, lCol - 4).ClearContents
    End If
    Application.ScreenUpdating = True
End Sub

*If it is only ever 14 columns of opening times then fCol = sCol + 13 and there wont be any issue if there is other data in columns to right of lCol?
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter an ID in column D and press the RETURN key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("D4:D2000")) Is Nothing Then Exit Sub
    Dim lCol As Long, x As Long
    lCol = Cells(3, Columns.Count).End(xlToLeft).Column
    If Target <> "" Then
        For x = 9 To lCol Step 2
            Cells(Target.Row, x).Resize(, 2) = Array("09:00", "17:00")
        Next x
    Else
        Cells(Target.Row, 9).Resize(, lCol - 4).ClearContents
    End If
    Application.ScreenUpdating = True
End Sub
Hi Mumps, this is pretty close to what I'm after - Thanks
Where do I set the start column. I now need to offset from column D by 9
My array is not a clean 09:00, 17:00. The Sat & Sun are 00:00. I'm happy to hard code all 14 values if that helps
Your code is filling to the end of the row. I need a fixed length range for the on change input (14 cells) and a separate fixed length range for when the cell in col d is cleared (x cells)

(Sheet is WIP -Apologies my initial post was not clear enough on that) - so need to be able to set the start points until I've got all the columns in place I need
Input range needs to have a start offset I can set, then 14 values
Clear range needs to have a start offset I can set & a width I can set (2 Variables that determine the Range)

MRE02.JPG
 
Upvote 0
I've "butchered" the code to get to the following, which seems to work ok. Any thoughts?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("D4:D2000")) Is Nothing Then Exit Sub
    Dim FCol As Long
    Dim ClrSt As Long, ClrLen As Long
    
    FCol = 13
    
    ClrSt = 5
    ClrLen = 22
    
    If Target <> "" Then
            Cells(Target.Row, FCol).Resize(, 14) = Array("09:00", "17:00", "09:00", "17:00", "09:00", "17:00", "09:00", "17:00", "09:00", "17:00", "00:00", "00:00", "00:00", "00:00")
    Else
        Cells(Target.Row, ClrSt).Resize(, ClrLen).ClearContents
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

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