Macro copy and paste based on condition

ferr

New Member
Joined
Jun 9, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
I need a macro that does the following.

- if there is a value in A3:E3, it copies and pastes to the corresponding cell in table 1 based on the date in A2:E2 and the corresponding date in E9:E11

in this example, copy and paste from C3 to D11=4,000,000 (August) and E3 to D13=4,000,000 (October)

However, with a condition that if the value under header 'B' is < the value in row 3, it pastes in the next table where the value is => than the value in row 3.

So, in this case, copy and paste from C3 to D11, but since all the October dates under header 'B' are less than E3, it will then paste in X9
 

Attachments

  • image003.png
    image003.png
    48.8 KB · Views: 14

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
See if this works to your requirement
VBA Code:
Sub Test()

Dim n As Long
Dim ArryCol() As String
Dim cell As Range, rngData As Range, rngFind As Range, rngSearch As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")                         ' Assuming sheet name is Sheet1
Set rngData = ws.Range("A2", "E2")

ArryCol = Split("E,J,O,T,Y", ",")                                            ' Define date column in tables

For Each cell In rngData
    If Not cell.Offset(1) = 0 Then
        For n = 0 To 4
            Set rngSearch = ws.Range(ArryCol(n) & "9", ArryCol(n) & "19")
            Set rngFind = rngSearch.Find(Month(cell))
            If Not rngFind Is Nothing Then
                If cell.Offset(1) <= rngFind.Offset(, -2) Then
                    rngFind.Offset(, -1) = cell.Offset(1)
                    Exit For
                End If
            End If
        Next
    End If
Next

End Sub
 
Last edited:
Upvote 0
Solution
See if this works to your requirement
VBA Code:
Sub Test()

Dim n As Long
Dim ArryCol() As String
Dim cell As Range, rngData As Range, rngFind As Range, rngSearch As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")                         ' Assuming sheet name is Sheet1
Set rngData = ws.Range("A2", "E2")

ArryCol = Split("E,J,O,T,Y", ",")                                            ' Define date column in tables

For Each cell In rngData
    If Not cell.Offset(1) = 0 Then
        For n = 0 To 4
            Set rngSearch = ws.Range(ArryCol(n) & "9", ArryCol(n) & "19")
            Set rngFind = rngSearch.Find(Month(cell))
            If Not rngFind Is Nothing Then
                If cell.Offset(1) <= rngFind.Offset(, -2) Then
                    rngFind.Offset(, -1) = cell.Offset(1)
                    Exit For
                End If
            End If
        Next
    End If
Next

End Sub
Perfect. Thank you.
 
Upvote 0
Any idea why nothing is being populated in N9 for August?
 

Attachments

  • B0765E6C-E26E-47F2-9525-2B2974A91900.png
    B0765E6C-E26E-47F2-9525-2B2974A91900.png
    47.7 KB · Views: 9
Upvote 0
Any idea why nothing is being populated in N9 for August?
Sorry. I was on leave Friday. Working with Date is always tricky for me :mad:

It seem like the use of Find cause the problem. I don't know how to set Find to look for month value. So, it probably just look for something else. It is much easier to just use For loop. Here is my amended code
VBA Code:
Sub Test()

Dim nRow As Long, nCol As Long
Dim ArryCol() As String
Dim cell As Range, rngData As Range, rngFind As Range, rngSearch As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")                         ' Assuming sheet name is Sheet1
Set rngData = ws.Range("A2", "E2")

ArryCol = Split("E,J,O,T,Y", ",")                                            ' Define date column in tables

For Each cell In rngData
    If Not cell.Offset(1) = 0 Then
        For nCol = 0 To 4
            For nRow = 9 To 19
                If Month(ws.Range(ArryCol(nCol) & nRow)) = Month(cell.Text) Then
                    If cell.Offset(1) <= ws.Range(ArryCol(nCol) & nRow).Offset(, -2) Then
                        ws.Range(ArryCol(nCol) & nRow).Offset(, -1) = cell.Offset(1)
                        Exit For
                    End If
                End If
            Next
        Next
    End If
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,553
Members
449,038
Latest member
Guest1337

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