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
 
The code works but the pasting progress to cells is slow ( pasting cell after cell).
The code need to check each cell at first row to understand it is Networkday or Holiday or Not.
Meantime the code applies to entire range. What will be the code if I want to apply it to a single target cell/row only where I run the module , if needed, manually.
Are you want Worksheet Change Event to when you change data at column A or Column of First day of Month Code runs automatically.
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Yes, it was fault.
Now I better understand your request.
Select name in the column "A" and call "AutoInput9" procedure after that.

This goes to the ThisWorkbook module.
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Call KeepTarget
End Sub
This goes to the standard module. This public variable will transfer "Target" through workbook.
VBA Code:
Public vTarget As Range
Sub StartV()
   Set vTarget = ActiveCell
End Sub
And this is modification of your code.
VBA Code:
Sub AutoInput9()

    Dim aRng As Range, aRng1 As Range
    Dim alastRow As Long, alastCol As Long
    Dim aRngCol As Range, aRngRow As Range
    
    Application.ScreenUpdating = False
    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))
    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)
    Dim aRngInput As Range
    Set aRngInput = Range(Cells(3, acolumnB1.Column + 1), _
                    Cells(alastRow, acolumnE1.Column))
    If aRng.Rows.Count > 2 Then
        Set aRng1 = Intersect([vTarget], 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 vTarget.Column = 1 And Not (IsEmpty(vTarget)) Then
            For vN = acolumnB1.Column + 1 To acolumnE1.Column
                If IsHolWeekend(Cells(1, vN)) = False Then
                    Cells(vTarget.Row, vN).Value = _
                    Cells(vTarget.Row, acolumnB1.Column).Value
                End If
            Next
        End If
    End If

End Sub
 
Upvote 0
Correction...
replace the name of the procedure "StartV" with name "KeepTarget".
 
Upvote 0
Try this Version:
VBA Code:
Sub FillNetworkDays()
Dim i As Long, j As Long, M As Long, Lr As Long, F As Long, C As String, R As String, S As Variant
Dim Ca As Variant, Ra As Variant
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)
For j = M + 1 To F
If Application.WorksheetFunction.NetworkDays(Cells(1, j).Value, Cells(1, j).Value) = 1 And IsHolWeekend(Cells(1, j).Value) = False 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

And if you want Worksheet Change Event Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, j As Long, M As Long, Lr As Long, F As Long, C As String, R As String, S As Variant
Dim Ca As Variant, Ra As Variant
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)
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
If Application.WorksheetFunction.NetworkDays(Cells(1, j).Value, Cells(1, j).Value) = 1 And IsHolWeekend(Cells(1, j).Value) = False 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,
Your code is working much much faster now. However It was my fault from the beginning that instead of copying data from the first date of month, it should be the first workday of the month - like in August, the data in Aug 2 and in Jan next year, the data in Jan 3 should be used to copy. Appreiciate your help.
 
Upvote 0
Correction...
replace the name of the procedure "StartV" with name "KeepTarget".
Hi EXCEL MAX,
Your latest code runs cell after cell which is not efficient. Your initial code is much better but needs to modify that if the value in row of column A is empty, the code not applies and the active cell should be the first workday of the month not in column A. Please help.
 
Upvote 0
For Normal Macro Try this:
VBA Code:
Sub FillNetworkDays()
Dim i As Long, j As Long, M As Long, Lr As Long, F As Long, C As String, R As String, S As Variant
Dim Ca As Variant, Ra As Variant
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

For j = M + 1 To F
If Application.WorksheetFunction.NetworkDays(Cells(1, j).Value, Cells(1, j).Value) = 1 And IsHolWeekend(Cells(1, j).Value) = False 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

And WorkSheet Change Event:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, j As Long, M As Long, Lr As Long, F As Long, C As String, R As String, S As Variant
Dim Ca As Variant, Ra As Variant
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)
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, 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
If Application.WorksheetFunction.NetworkDays(Cells(1, j).Value, Cells(1, j).Value) = 1 And IsHolWeekend(Cells(1, j).Value) = False 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 want to apply the code to DoubleClick event but limit it to the first networkdays cell in row 2 of that column ( if the first networkdays is Jan 4, 2022 in column K, then double click K2 triggers the run)(see image). My code applies to the whole row in the range. How to modify it ?

Meantime If I run the worksheet change event, the error highlights IsHolWeekend .
k2.png

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
          lastColR = Cells(1, Columns.Count).End(xlToLeft).Column
        
           If Not Intersect(Target, Range(Cells(2, 3), Cells(2, lastColR))) Is Nothing Then              'Apply to Row 2 in Range
            Cancel = True
          Application.DisplayAlerts = False
             FillNetworkDays
          Application.DisplayAlerts = True
          
          End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,820
Members
449,469
Latest member
Kingwi11y

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