Forum Discussion

Andrewsayer22's avatar
Andrewsayer22
Copper Contributor
Jan 23, 2024

Paste data in a row based on data input

Hi All

I am struggling to write a macro to paste data into a particular row based upon a variable input. I have a small array of data that is self populating based on drop down menu inputs. This in turn returns a row number where I would like to then start to paste the data once i run the macro. Can anyone help?

1 Reply

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    Andrewsayer22 

    If I understand correctly, you want to paste data into a specific row based on a row number obtained from a dropdown menu. Here is a simple example of a VBA macro that does this:

    Sub PasteDataBasedOnInput()
        Dim ws As Worksheet
        Dim dataToPaste As Range
        Dim targetRow As Long
    
        ' Set the worksheet (change "Sheet1" to the name of your sheet)
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
        ' Get the target row from your dropdown menu or any other source
        ' In this example, assuming the target row is in cell A1
        targetRow = ws.Range("A1").Value
    
        ' Set the range of data to paste (adjust the range as needed)
        Set dataToPaste = Sheets("Sheet1").Range("A2:C2")
    
        ' Check if the target row is within the valid range
        If targetRow >= 1 And targetRow <= ws.Rows.Count Then
            ' Paste data into the specified row
            dataToPaste.Copy ws.Cells(targetRow, 1)
        Else
            MsgBox "Invalid target row number!"
        End If
    End Sub

    Make sure to adjust the sheet name, dropdown cell reference, data range to paste, and any other specific details according to your actual workbook.

    After setting up the macro, you can assign it to a button or run it directly from the Excel interface. You may want to customize the macro further based on your specific needs and data. The text, steps and code were created with the help of AI.

    If this code not help you, I recommend adding more information to your topic. Information such as Excel version, operating system, storage medium, file extension, VBA Code, etc. In this link you will find some more information about it:

    Welcome to your Excel discussion space!

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

Resources