Combine two routines

bdt

Board Regular
Joined
Oct 3, 2024
Messages
53
Office Version
  1. 2019
Platform
  1. Windows
Hi all,
I have a code where I take a range of cells in a column and place them in another sheet in a row, which works fine. In my ignorance I thought I could have another similar piece of code that takes a different range of cells and puts then in the same row as the first piece of code. I didn't appreciate the second code overwrites the first. Is it possible to combine the two codes so that range AI21:AI33 and BM21:BM33 are placed in the same cells in a row on the sheet "OVERTIME". My code is;

'add overtime sunday
With ActiveSheet
WKend = .Range("M2").Value
arr = .Range("AI21:AI33").Value
End With

With Sheets("OVERTIME").Range("A:A")
Set fndRng = .Find(What:=Format(WKend, "dd/mm/yy"), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

If Not fndRng Is Nothing Then
fndRng.Offset(, 1).Resize(, UBound(arr)).Value = Application.Transpose(arr)
Else
MsgBox "Sorry, did not find " & WKend
End If

End With

'add absence sunday
With ActiveSheet
WKend = .Range("M2").Value
arr = .Range("BM21:BM33").Value
End With

With Sheets("OVERTIME").Range("A:A")
Set fndRng = .Find(What:=Format(WKend, "dd/mm/yy"), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

If Not fndRng Is Nothing Then
fndRng.Offset(, 1).Resize(, UBound(arr)).Value = Application.Transpose(arr)
Else
MsgBox "Sorry, did not find " & WKend
End If
End With

Many thanks again
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
range AI21:AI33 and BM21:BM33 are placed in the same cells in a row
When reading this section, I believe you want BM to overwrite AI in the same range, while you actually don’t want to do that. Please clarify the AI range and BM range; where do you want to paste them?
 
Upvote 0
I am also unclear on where you want the 2 lots of data to finish up.
On the off chance that you want them side by side when you copy back the BM array just change the column offset from 1 to 14.
Rich (BB code):
fndRng.Offset(, 14).Resize(, UBound(arr)).Value = Application.Transpose(arr)
 
Upvote 0
VBA Code:
Sub AddDataToOvertimeSheet()
    Dim WKend As Variant
    Dim arr1 As Variant
    Dim arr2 As Variant
    Dim fndRng As Range
    Dim arr1_len As Integer
    Dim arr2_len As Integer
    Dim totalOffset As Integer

    With ActiveSheet
        WKend = .Range("M2").Value
        arr1 = .Range("AI21:AI33").Value
        arr2 = .Range("BM21:BM33").Value
    End With

    arr1_len = UBound(arr1) - LBound(arr1) + 1
    arr2_len = UBound(arr2) - LBound(arr2) + 1

    With Sheets("OVERTIME").Range("A:A")
        Set fndRng = .Find(What:=Format(WKend, "dd/mm/yy"), _
            LookIn:=xlValues, _
            LookAt:=xlPart, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False)
        If Not fndRng Is Nothing Then
            totalOffset = 1 ' Starting column (Column B)

            fndRng.Offset(, totalOffset).Resize(, arr1_len).Value = Application.Transpose(arr1)
            totalOffset = totalOffset + arr1_len ' Update the offset

            fndRng.Offset(, totalOffset).Resize(, arr2_len).Value = Application.Transpose(arr2)
        Else
            MsgBox "Sorry, did not find " & WKend & " date.", vbExclamation, "Date Not Found"
        End If
    End With
End Sub
 
Upvote 0
pitchoute, many thanks. So nearly does what I'm after. Looks like I was not 100% clear in my goal.
I need arr1=.Range("AI21:AI33").Value and arr2=.Range("BM21:BM33").Value to be placed into the same row AND in the same columns, B to M.
Hope this makes it a little clearer.
 
Upvote 0
Could you update your code like this?
VBA Code:
Sub AddDataToOvertimeSheet()
    Dim WKend As Variant
    Dim arr1 As Variant, arr2 As Variant
    Dim merged_arr As Variant
    Dim fndRng As Range
    Dim i As Integer
    Dim arr_len As Integer

    With ActiveSheet
        WKend = .Range("M2").Value
        arr1 = .Range("AI21:AI33").Value  ' 13 rows x 1 column
        arr2 = .Range("BM21:BM33").Value  ' 13 rows x 1 column
    End With

    arr_len = UBound(arr1, 1) - LBound(arr1, 1) + 1  ' Calculate array length

    ReDim merged_arr(1 To 1, 1 To arr_len)

    For i = 1 To arr_len
        If arr1(i, 1) <> "" Then
            merged_arr(1, i) = arr1(i, 1)
        ElseIf arr2(i, 1) <> "" Then
            merged_arr(1, i) = arr2(i, 1)
        Else
            merged_arr(1, i) = ""
        End If
    Next i

    With Sheets("OVERTIME").Range("A:A")
        Set fndRng = .Find(What:=Format(WKend, "dd/mm/yy"), _
            LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False)
        If Not fndRng Is Nothing Then
            fndRng.Offset(, 1).Resize(1, arr_len).Value = merged_arr
        Else
            MsgBox "Sorry, did not find " & WKend & " date.", vbExclamation, "Date Not Found"
        End If
    End With
End Sub
 
Upvote 0
Many thanks for replying so soon.
[arr.Range("AI21:AI33").Value ' 13 rows x 1 column] is added where I wish but unfortunately [arr2 =.Range("BM21:BM33").Value ' 13 rows x 1 column] is not being added to any columns or rows.
[arr2 =.Range("BM21:BM33").Value ' 13 rows x 1 column] seems to clear any values put there by [arr.Range("AI21:AI33").Value ' 13 rows x 1 column] and places a "0" in its place
Cheers
 
Upvote 0
Retry please;


VBA Code:
Sub AddDataToOvertimeSheet()
    Dim WKend As Variant
    Dim arr1 As Variant, arr2 As Variant
    Dim merged_arr As Variant
    Dim fndRng As Range
    Dim i As Integer
    Dim arr_len As Integer

    With ActiveSheet
        WKend = .Range("M2").Value
        arr1 = .Range("AI21:AI33").Value  ' 13 rows x 1 column
        arr2 = .Range("BM21:BM33").Value  ' 13 rows x 1 column
    End With

    arr_len = UBound(arr1, 1) - LBound(arr1, 1) + 1  ' Calculate array length

    ReDim merged_arr(1 To 1, 1 To arr_len)

    For i = 1 To arr_len
        ' If arr1 has a value and it's not empty
        If Not IsEmpty(arr1(i, 1)) And arr1(i, 1) <> "" Then
            merged_arr(1, i) = arr1(i, 1)
        ' If arr1 is empty and arr2 has a value
        ElseIf Not IsEmpty(arr2(i, 1)) And arr2(i, 1) <> "" Then
            merged_arr(1, i) = arr2(i, 1)
        Else
            merged_arr(1, i) = ""
        End If
    Next i

    With Sheets("OVERTIME").Range("A:A")
        Set fndRng = .Find(What:=Format(WKend, "dd/mm/yy"), _
            LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False)
        If Not fndRng Is Nothing Then
            fndRng.Offset(, 1).Resize(1, arr_len).Value = merged_arr
        Else
            MsgBox "Sorry, did not find " & WKend & " date.", vbExclamation, "Date Not Found"
        End If
    End With
End Sub
 
Upvote 0
pitchoute, I very much appreciate your response. Unfortunately arr2 is still not adding to the row in"OVERTIME"
 
Upvote 0
I used the array for your original question in order to avoid any looping, obviously the goal post have moved.
If this is the kind of thing you're now after

bdt.xlsm
ABCDEFGHIJKLMNO
1Dates
207/09/24
314/09/24
421/09/24
528/09/24
605/10/24
712/10/24ai_21 bm_21ai_22 bm_22ai_23 bm_23ai_24 bm_24ai_25 bm_25ai_26 bm_26ai_27 bm_27ai_28 bm_28ai_29 bm_29ai_30 bm_30ai_31 bm_31ai_32 bm_32ai_33 bm_33
819/10/24
926/10/24
1002/11/24
1109/11/24
12
OVERTIME

Try this
VBA Code:
Sub testing()    ' data in same cells
    
    Dim WKend As Date
    Dim rng1 As Range, rng2 As Range
    Dim fndRng As Range, cel As Range
    Dim i As Long
    
'add overtime sunday
With ActiveSheet
    WKend = .Range("M2").Value
    Set rng1 = .Range("AI21:AI33")
    Set rng2 = .Range("BM21:BM33")
End With

With Sheets("OVERTIME").Range("A:A")
    ' find date
    Set fndRng = .Find(What:=Format(WKend, "dd/mm/yy"), _
                        LookIn:=xlValues, _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)

    If Not fndRng Is Nothing Then   'if found
        i = 1
        For Each cel In rng1
            fndRng.Offset(, i) = cel.Value
            i = i + 1
        Next cel
        i = 1
        For Each cel In rng2
            fndRng.Offset(, i) = fndRng.Offset(, i).Value & Chr(10) & cel.Value
            i = i + 1
        Next cel
    Else                            'if not found
        MsgBox "Sorry, did not find " & WKend
    End If
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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