Copy value from one cell to balance cells in same row

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys, I want to copy value of a cell at the 1st day of a month to rest of the workdays that month in the same row if the cell in column A is not empty. Appreciate any help !



AgentProposal_Roster0728_0829.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKAL
1MDateAttendance27-Jul28-Jul29-Jul30-Jul31-Jul1-Aug2-Aug3-Aug4-Aug5-Aug6-Aug7-Aug8-Aug9-Aug10-Aug11-Aug12-Aug13-Aug14-Aug15-Aug16-Aug17-Aug18-Aug19-Aug20-Aug21-Aug22-Aug23-Aug24-Aug25-Aug26-Aug27-Aug28-Aug29-Aug30-Aug31-Aug
2DateSummary(5)(4)(3)(2)(1)12345678910111213141516171819202122232425262728293031
3Mary WT:22 L:0 D:1 E:0 N:0GGGGGD
4M JoshT:22 L:0 D:0 E:0 N:0DDDDD
5C RossiniT:22 L:0 D:0 E:0 N:0EEEEE
6J WcmathT:22 L:0 D:0 E:0 N:0D4PMD4D4AL
7T:22 L:0 D:0 E:0 N:0DDVLVLD
8T:22 L:0 D:0 E:0 N:0DDDD
9T:22 L:0 D:0 E:0 N:0DDDD
10T:22 L:0 D:0 E:0 N:0DDDD
11T:22 L:0 D:0 E:0 N:0VLVLDD
12T:22 L:0 D:0 E:0 N:0DDDDD
13T:22 L:0 D:0 E:0 N:0DNDD
14T:22 L:0 D:0 E:0 N:0DDDDD
202108
Cells with Data Validation
CellAllowCriteria
H3:AL14List=ShiftcodeNew
A3:A13List=HelpAgent
A14List=HelpAgent



Code:
Public Function IsHolWeekend(InputDate As Date) As Boolean

    Dim vLastRow As Long
    Dim vR1 As Range

    With ThisWorkbook.Worksheets("Data")
        vLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        For Each vR1 In .Range("A2:A" & vLastRow)
            If Day(InputDate) = Day(vR1) And _
               Month(InputDate) = Month(vR1) And _
               Year(InputDate) = Year(vR1) Or _
               Weekday(InputDate) = 1 Or _
               Weekday(InputDate) = 7 Then
               IsHolWeekend = True
               Exit Function
            Else
               IsHolWeekend = False
            End If
        Next vR1
     End With
    
End Function


Sub AutoInput9()


Dim aRng As Range, aRng1 As Range
Dim alastRow As Long, alastCol As Long
Dim aRngCol As Range, aRngRow As Range


alastRow = Cells(Rows.Count, "A").End(xlUp).Row
alastCol = Cells(1, Columns.Count).End(xlToLeft).Column


Set aRng = Range(Cells(1, 1), Cells(alastRow & alastCol))
Set aRngRow = Range(Cells(1, 3), Cells(1, alastCol))

    
'Locate the columns of the first and last dates of the month
aDayB1 = CDate(Format(ActiveSheet.Name, "0000-00"))
aDayE1 = DateAdd("m", 1, aDayB1) - 1
Set acolumnB1 = aRngRow.Find(aDayB1, , xlFormulas)
Set acolumnE1 = aRngRow.Find(aDayE1, , xlFormulas)

Debug.Print acolumnB1.Address, acolumnE1.Address

Dim aRngInput As Range
Set aRngInput = Range(Cells(3, acolumnB1.Column + 1), Cells(alastRow, acolumnE1.Column))

Debug.Print aRngInput.Address
 
 If aRng.Rows.Count > 2 Then
        Set aRng1 = Intersect(Target, aRng.Offset(1, 0).Resize(aRng.Rows.Count - 1, aRng.Columns.Count))
    Else
       Set aRng1 = Nothing
    End If

    If Not aRng1 Is Nothing Then
        If Target.Column = 1 And Not (IsEmpty(Target)) Then
           If IsHolWeekend(aRngRow.Cells(1)) = False Then
             Target.Value = Target.aRngInput.Value
             End If
                End If
                  End If

End Sub
 
Are your date is in row 2 or row 1 ?

VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
        Select Case Sh.Name
                     Case "Data"
              Exit Sub
                     Case Else
              End Select
            
          Dim lastColR As Long, Lr&, M&, F&
          lastColR = Cells(1, Columns.Count).End(xlToLeft).Column
          Lr = Range("A" & Rows.Count).End(xlUp).Row
        M = Application.Match(CLng(DateSerial(Year(Range("C1").Value), Month(Range("C1").Value) + 1, 0)) + 1, Rows(1), 0)
        F = Application.Match(CLng(DateSerial(Year(Cells(1, M).Value), Month(Cells(1, M).Value) + 1, 0)), Rows(1), 0)
Resum2:
        If Application.WorksheetFunction.NetworkDays(Cells(1, M).Value, Cells(1, M).Value) = 1 And IsHolWeekend(Cells(1, M).Value) = False Then
        Else
        M = M + 1
        GoTo Resum2
        End If
        
           If Intersect(Target, Range(Cells(2, M), Cells(2, M))) Is Nothing Then Exit Sub             'Apply to Row 2 in Range
            Cancel = True
          Application.DisplayAlerts = False
             FillNetworkDays
          Application.DisplayAlerts = True
     
    
End Sub
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Are your date is in row 2 or row 1 ?

VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
        Select Case Sh.Name
                     Case "Data"
              Exit Sub
                     Case Else
              End Select
            
          Dim lastColR As Long, Lr&, M&, F&
          lastColR = Cells(1, Columns.Count).End(xlToLeft).Column
          Lr = Range("A" & Rows.Count).End(xlUp).Row
        M = Application.Match(CLng(DateSerial(Year(Range("C1").Value), Month(Range("C1").Value) + 1, 0)) + 1, Rows(1), 0)
        F = Application.Match(CLng(DateSerial(Year(Cells(1, M).Value), Month(Cells(1, M).Value) + 1, 0)), Rows(1), 0)
Resum2:
        If Application.WorksheetFunction.NetworkDays(Cells(1, M).Value, Cells(1, M).Value) = 1 And IsHolWeekend(Cells(1, M).Value) = False Then
        Else
        M = M + 1
        GoTo Resum2
        End If
        
           If Intersect(Target, Range(Cells(2, M), Cells(2, M))) Is Nothing Then Exit Sub             'Apply to Row 2 in Range
            Cancel = True
          Application.DisplayAlerts = False
             FillNetworkDays
          Application.DisplayAlerts = True
     
    
End Sub
Are your date is in row 2 or row 1 ?

VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
        Select Case Sh.Name
                     Case "Data"
              Exit Sub
                     Case Else
              End Select
            
          Dim lastColR As Long, Lr&, M&, F&
          lastColR = Cells(1, Columns.Count).End(xlToLeft).Column
          Lr = Range("A" & Rows.Count).End(xlUp).Row
        M = Application.Match(CLng(DateSerial(Year(Range("C1").Value), Month(Range("C1").Value) + 1, 0)) + 1, Rows(1), 0)
        F = Application.Match(CLng(DateSerial(Year(Cells(1, M).Value), Month(Cells(1, M).Value) + 1, 0)), Rows(1), 0)
Resum2:
        If Application.WorksheetFunction.NetworkDays(Cells(1, M).Value, Cells(1, M).Value) = 1 And IsHolWeekend(Cells(1, M).Value) = False Then
        Else
        M = M + 1
        GoTo Resum2
        End If
        
           If Intersect(Target, Range(Cells(2, M), Cells(2, M))) Is Nothing Then Exit Sub             'Apply to Row 2 in Range
            Cancel = True
          Application.DisplayAlerts = False
             FillNetworkDays
          Application.DisplayAlerts = True
     
    
End Sub
The dates are in row 1 but I apply double click to row 2.
 
Upvote 0

Attachments

  • ishol.png
    ishol.png
    11.7 KB · Views: 6
Upvote 0
Are you add IsHolWeekend posted by you at first post, after this code at same module?
 
Upvote 0
Another Option without need to IsHolWeekend Function
Try this as Workbook Change Event :
VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
          If Sh.Name = "Data" Then Exit Sub
          Dim Lc&, Lr&, Lr2&, M&, F&, N&
          Lr = Range("A" & Rows.Count).End(xlUp).Row
          Lc = Cells(1, Columns.Count).End(xlToLeft).Column
          Lr2 = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
        M = Application.Match(CLng(DateSerial(Year(Range("C1").Value), Month(Range("C1").Value) + 1, 0)) + 1, Rows(1), 0)
        N = Evaluate("=INDEX(" & Cells(1, M).Address & ":" & Cells(1, Lc).Address & ",MATCH(TRUE,COUNTIF(Data!A1:A" & Lr2 & "," & Cells(1, M).Address & ":" & Cells(1, Lc).Address & ")>0,0))")
        N = Application.Match(N, Rows(1), 0)
Resum2:
        If Application.WorksheetFunction.NetworkDays(Cells(1, M).Value, Cells(1, M).Value) = 1 And N <> M Then
        Else
        M = M + 1
        GoTo Resum2
        End If
        F = Application.Match(CLng(DateSerial(Year(Cells(1, M).Value), Month(Cells(1, M).Value) + 1, 0)), Rows(1), 0)
           If Intersect(Target, Range(Cells(2, M), Cells(2, M))) Is Nothing Then Exit Sub             'Apply to Row 2 in Range
            Cancel = True
          Application.DisplayAlerts = False
             FillNetworkDays
          Application.DisplayAlerts = True
 End Sub

And This as FillNetworkDays as Normal Macro:
VBA Code:
Sub FillNetworkDays()
On Error Resume Next
Dim i&, j&, C As String, R As String, S As Variant, Ca As Variant, Ra As Variant
Dim Lc&, Lr&, Lr2&, M&, F&, N&
Lr = Range("A" & Rows.Count).End(xlUp).Row
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
Lr2 = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
M = Application.Match(CLng(DateSerial(Year(Range("C1").Value), Month(Range("C1").Value) + 1, 0)) + 1, Rows(1), 0)
N = Evaluate("=INDEX(" & Cells(1, M).Address & ":" & Cells(1, Lc).Address & ",MATCH(TRUE,COUNTIF(Data!A1:A" & Lr2 & "," & Cells(1, M).Address & ":" & Cells(1, Lc).Address & ")>0,0))")
N = Application.Match(N, Rows(1), 0)
F = Application.Match(CLng(DateSerial(Year(Cells(1, M).Value), Month(Cells(1, M).Value) + 1, 0)), Rows(1), 0)
Resum2:
        If Application.WorksheetFunction.NetworkDays(Cells(1, M).Value, Cells(1, M).Value) = 1 And N <> M Then
        Else
        M = M + 1
        GoTo Resum2
        End If
Resum3:


For j = M + 1 To F
N = 0
N = Application.Match(Cells(1, j).Value, Sheets("Data").Range("A1:A" & Lr2), 0)
If Application.WorksheetFunction.NetworkDays(Cells(1, j).Value, Cells(1, j).Value) = 1 And N = 0 Then C = C & " " & j - M - 1
Next j
For i = 3 To Lr
If Range("A" & i).Value <> "" Then R = R & " " & i - 3
Next i
Ra = Split(Right(R, Len(R) - 1))
Ca = Split(Right(C, Len(C) - 1))
ReDim S(Lr - 3, F - M - 1)
For i = LBound(Ra) To UBound(Ra)
    For j = LBound(Ca) To UBound(Ca)
        S(CLng(Ra(i)), CLng(Ca(j))) = Cells(CLng(Ra(i)) + 3, M).Value
    Next j
Next i

Cells(3, M + 1).Resize(Lr - 2, F - M).Value = S
End Sub

Or Only Use this as Worksheet Change Event:
Excel Formula:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i&, j&, C As String, R As String, S As Variant, Ca As Variant, Ra As Variant
Dim Lc&, Lr&, Lr2&, M&, F&, N&
Lr = Range("A" & Rows.Count).End(xlUp).Row
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
Lr2 = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
M = Application.Match(CLng(DateSerial(Year(Range("C1").Value), Month(Range("C1").Value) + 1, 0)) + 1, Rows(1), 0)
N = Evaluate("=INDEX(" & Cells(1, M).Address & ":" & Cells(1, Lc).Address & ",MATCH(TRUE,COUNTIF(Data!A1:A" & Lr2 & "," & Cells(1, M).Address & ":" & Cells(1, Lc).Address & ")>0,0))")
N = Application.Match(N, Rows(1), 0)
Resum2:
        If Application.WorksheetFunction.NetworkDays(Cells(1, M).Value, Cells(1, M).Value) = 1 And N <> M Then
        Else
        M = M + 1
        GoTo Resum2
        End If
If Intersect(Target, Union(Range("A3:A" & Lr), Range(Cells(3, M), Cells(Lr, M)))) Is Nothing Then Exit Sub
F = Application.Match(CLng(DateSerial(Year(Cells(1, M).Value), Month(Cells(1, M).Value) + 1, 0)), Rows(1), 0)
For j = M + 1 To F
N = 0
N = Application.Match(Cells(1, j).Value, Sheets("Data").Range("A1:A" & Lr2), 0)
If Application.WorksheetFunction.NetworkDays(Cells(1, j).Value, Cells(1, j).Value) = 1 And N = 0 Then C = C & " " & j - M - 1
Next j
i = Target.Row
If Range("A" & i).Value <> "" Then R = R & " " & i - 3

Ra = Split(Right(R, Len(R) - 1))
Ca = Split(Right(C, Len(C) - 1))
ReDim S(F - M - 1)
    For j = LBound(Ca) To UBound(Ca)
        S(CLng(Ca(j))) = Cells(Target.Row, M).Value
    Next j
Range(Cells(Target.Row, M + 1), Cells(Target.Row, F)).Value = S

End Sub
 
Upvote 0
Hi maabadi,
I tried normal code but it does not work and it removes those input in the 1st networkday column !
 
Upvote 0
What is the first date you used as Networkdays?
I test it with august-2021 and it copied data from 2-aug to other cells.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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