Forum Discussion

Valdon1070's avatar
Valdon1070
Copper Contributor
Sep 21, 2025

Macro to copy

Hello all you "Brilliant Minds" here is a chance to prove your "Excel-ence. I want to copy  a value to one cell from the source which is random and as I haven't a clue about this stuff I asked Microsoft CoPilot. Being on the 4th version Where Microsoft excel still will not accept Microsoft CoPilot's creation, I am appealing to the Community. So here is your chance to prove that you are smarter than CoPilot. 

Here we go, everything but attach a file. If anyone is interested send me an email so I can send a couple of small files.

Donald

15 Replies

  • queenss's avatar
    queenss
    Copper Contributor

    I got a win last night and it was real, I played on the JO777 site

  • peiyezhu's avatar
    peiyezhu
    Bronze Contributor

    https://techcommunity.microsoft.com/discussions/excelgeneral/re-excel-code-trying-to-copy-cell-data-from-one-sheet-to-another-based-on-2-crit/4452991/replies/4457127

    • peiyezhu's avatar
      peiyezhu
      Bronze Contributor

      Oops.

      https://filetransfer.io/

      Try this cloud storage.

  • peiyezhu's avatar
    peiyezhu
    Bronze Contributor

    Some times,the attachment will dispear. maybe the network not stable instead of being declined.

    Try more times.

  • NikolinoDE's avatar
    NikolinoDE
    Platinum Contributor

    To help the community support you more effectively, it would be useful if you could clarify a few things:

    What exactly is the source cell or range you want to copy from?

    Where do you want the value pasted (a specific sheet/cell, or dynamically chosen)?

    Should this copy happen automatically (e.g., when data changes), or only when you run a macro manually?

    Provide detailed information about the file (if known), including which Excel version (Excel 2016, 365 Home, etc.) was used to create the file?

    Whether the file extension has ever been changed (.xls, .xlsx, .xlsm, etc.)?

    Which operating system you are using to open the file, e.g., Windows 10 (21H1)?

     …and what medium is the file saved on (OneDrive, Sharepoint, hard disk, etc.)?

    If you can share a short description of your data layout (or a simplified example, rather than sending full files), it will be much easier for others to suggest a working macro.

    Many people here prefer not to receive files directly by email for safety reasons, but if you can describe or paste a sample setup, the community can usually provide a solution quite quickly.

     

    That way someone can give you code tailored to your exact need.

     

    Thank you for your understanding and patience

     

    Hope I was able to help you with this information.

    • Valdon1070's avatar
      Valdon1070
      Copper Contributor

      Thanks for your response   - from the top

      Q? (random)

      Y47

      Auto

      365 Home V2508 Build 20240

      .xlsm

      11 Home 10.0.26100

      HDD (but copied to OneDrive)

      See ScreenShot

      I use MS-Defender

       

      MacroCopyStockHoldingToSale

      In “Excel” I need to automatically copy the “Stock Holding” Value showing in Column (Q), to the requesting cell in Column (Y).

      This Value is not in the same Row because the sale occurs on a random date, days or months after the “Bought Date”. (Rows 95 and 96 are set up for Testing)

      The Trigger for this is an entry into Columns (V and W) in the Row associated to the Sale which is automatically copied.

      Maybe a Macro has to search downwards from Row (1) in Columns (O7 and P7 and Q7) to find the Oldest Matching “Code and Units and a Positive Value” and then copy the Value from Column (Q) to the Target Cell in “CODE” (Column (Y).

      To complete the action the program should return to the Source Cell in Column (Q) which is an Active Value in Purple Color with no fill and reformat the Value in that cell to an Inactive Value, by use of an apostrophe, change the Color to Red and make the Fill Yellow.

      This is the new revised copy
      Sub CopyStockHoldingToSale()
      Dim ws As Worksheet
      Dim saleRow As Long
      Dim saleCode As String
      Dim saleUnits As Double
      Dim targetRow As Long
      Dim sourceRow As Long
      Dim lastRow As Long
      ' Set worksheet
      Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your actual sheet name
      ' Define the row where the sale is recorded saleRow = 7 ' Change this if needed
      ' Get sale details
      saleCode = ws.Cells(saleRow, "O").Value
      saleUnits = ws.Cells(saleRow, "P").Value
      targetRow = saleRow ' Assuming you want to copy to same row in column Y
      ' Find the last row with data in column O
      lastRow = ws.Cells(ws.Rows.Count, "O").End(xlUp).Row
      ' Loop from top to bottom to find the oldest matching entry For sourceRow = 1 To lastRow
      If ws.Cells(sourceRow, "O").Value = saleCode And _ ws.Cells(sourceRow, "P").Value = saleUnits Then
      ' Check if Q is numeric and positive
      If IsNumeric(ws.Cells(sourceRow, "Q").Value) Then
      If ws.Cells(sourceRow, "Q").Value > 0 Then
      ' Copy value from column Q to column Y
      ws.Cells(targetRow, "Y").Value = ws.Cells(sourceRow, "Q").Value
      Exit For End If
      End If End If
      Next sourceRow End Sub

      ✅ Updated Requirements ex CoPilot

      1. Search from Row 1 downward in columns O, P, and Q.
      2. Find the oldest row where:

      o   Column O matches saleCode

      o   Column P matches saleUnits

      o   Column Q contains a positive numeric value

      1. Copy the value from column Q to column Y in the “Stock Holding” row.
      2. Ignore any cells in column Q that:
      • Are text (e.g., '123 or '0)
      • Are non-numeric
      • Are zero or negative

       

      Problem = Excel sees TEXT cell Q37 as Zero but Macro sees it as a value 11,700.00 ??

      • NikolinoDE's avatar
        NikolinoDE
        Platinum Contributor

        So far i understand…

        Requirements :

        1. Search from row 1 downward in columns O (Code), P (Units), Q (Stock Holding).
        2. Find the oldest row where:
        • Column O = saleCode
        • Column P = saleUnits
        • Column Q is numeric and positive
        • Ignore text, zero, or negative numbers
        1. Copy the value from Q to Y in the sale row
        2. After copying, mark the source cell as inactive:
        • Prefix with apostrophe (')
        • Font color red, fill color yellow

        Here’s a VBA macro:

        Sub CopyStockHoldingToSale()
            Dim ws As Worksheet
            Dim saleRow As Long
            Dim saleCode As String
            Dim saleUnits As Double
            Dim targetRow As Long
            Dim sourceRow As Long
            Dim lastRow As Long
            Dim cellValue As Double
            
            ' Set worksheet (change as needed)
            Set ws = ThisWorkbook.Sheets("Sheet1")
            
            ' Define the row where the sale is recorded
            saleRow = 7 ' Change to the row where the sale occurs
            
            ' Get sale details
            saleCode = ws.Cells(saleRow, "O").Value
            saleUnits = ws.Cells(saleRow, "P").Value
            targetRow = saleRow ' Copy to column Y in the same row
            
            ' Find the last row with data in column O
            lastRow = ws.Cells(ws.Rows.Count, "O").End(xlUp).Row
            
            ' Loop from top to bottom to find the oldest matching entry
            For sourceRow = 1 To lastRow
                ' Check code and units match
                If ws.Cells(sourceRow, "O").Value = saleCode And _
                   ws.Cells(sourceRow, "P").Value = saleUnits Then
                   
                    ' Only consider numeric and positive values in Q
                    If IsNumeric(ws.Cells(sourceRow, "Q").Value) Then
                        cellValue = CDbl(ws.Cells(sourceRow, "Q").Value)
                        
                        If cellValue > 0 Then
                            ' Copy value to target cell in column Y
                            ws.Cells(targetRow, "Y").Value = cellValue
                            
                            ' Format the source cell as inactive
                            With ws.Cells(sourceRow, "Q")
                                .Value = "'" & cellValue ' Prefix with apostrophe
                                .Font.Color = vbRed
                                .Interior.Color = vbYellow
                            End With
                            
                            Exit For ' Stop after the first match
                        End If
                    End If
                End If
            Next sourceRow
            
            ' Optional: Message if no match found
            If ws.Cells(targetRow, "Y").Value = "" Then
                MsgBox "No matching positive stock holding found.", vbExclamation
            End If
        End Sub

        You can upgrade this macro to automatically trigger whenever a value is entered in Columns V or W, so you don’t have to run it manually every time. This would fully automate your workflow.

        Here the auto trigger code:

        Private Sub Worksheet_Change(ByVal Target As Range)
            Dim ws As Worksheet
            Dim saleRow As Long
            Dim saleCode As String
            Dim saleUnits As Double
            Dim targetRow As Long
            Dim sourceRow As Long
            Dim lastRow As Long
            Dim cellValue As Double
            
            Set ws = Me ' Refers to this sheet
            
            ' Only trigger if change happened in column V or W
            If Intersect(Target, ws.Range("V:W")) Is Nothing Then Exit Sub
            
            ' Determine the row where the change happened
            saleRow = Target.Row
            targetRow = saleRow ' Copy to column Y in the same row
            
            ' Get sale details from columns O and P in the same row
            saleCode = ws.Cells(saleRow, "O").Value
            saleUnits = ws.Cells(saleRow, "P").Value
            
            ' Find last row with data in column O
            lastRow = ws.Cells(ws.Rows.Count, "O").End(xlUp).Row
            
            ' Loop from top to bottom to find the oldest matching entry
            For sourceRow = 1 To lastRow
                If ws.Cells(sourceRow, "O").Value = saleCode And _
                   ws.Cells(sourceRow, "P").Value = saleUnits Then
                   
                    If IsNumeric(ws.Cells(sourceRow, "Q").Value) Then
                        cellValue = CDbl(ws.Cells(sourceRow, "Q").Value)
                        
                        If cellValue > 0 Then
                            ' Copy value to target cell in column Y
                            ws.Cells(targetRow, "Y").Value = cellValue
                            
                            ' Format the source cell as inactive
                            With ws.Cells(sourceRow, "Q")
                                .Value = "'" & cellValue
                                .Font.Color = vbRed
                                .Interior.Color = vbYellow
                            End With
                            
                            Exit For
                        End If
                    End If
                End If
            Next sourceRow
        End Sub

        My answers are voluntary and without guarantee!

         

        Hope this will help you.

Resources