简体   繁体   中英

I want to copy a range of cells and paste them to another sheet dependent on a drop down selection and activated using a button

I have an Export to sheet button but I can't get it working correctly. I selects the correct cells to copy but can't then transpose them on to selected sheet that appears in the drop down box in cell A1, I then also need it to paste on the next available row in that specific sheet. The problem is that I can't just list the sheets in VBA as the list in the drop down box changes. I have tried several ways to with no success. If someone could help it would be great

Sub Button2_Click()

    Worksheets("Sheet1").Range("a2:x2").Copy
    ActiveSheet.Paste Destination:=Worksheets("Sheet1!A1").Range("a:x")

End Sub

you need to change the sheet name in your worksheet function.

This might work.

Sub Button2_Click()

'Copying the Data
Worksheets("Sheet1").Range("a2:x2").Copy

'Pasting the data
' What we were missing was to pass the name of the tab dynamically. Now this code will pick up the name that appears in the Cell A1.

ActiveSheet.Paste Destination:=Worksheets(Worksheets("Sheet1").Range("A1").value).Range("A1")

End Sub

Also in the paste range you only need to put the first cell range to paste values.

To paste the Transposed Values check the function PasteSpecial with Transpose property set to True.

Here is some more code I have tried for the issue but still does not seem to work.

Sub ExportButton1() ' ' ExportButton1 Macro ' Exports Data to staff sheet from drop down box ' ' Keyboard Shortcut: Ctrl+e ' ActiveWorkbook.Save End Sub Private Sub CommandButton1_Click(ByVal Target As Range) Application.ScreenUpdating = False Dim copySheet As Worksheet Dim pasteSheet As Worksheet

Set copySheet = Worksheets("Data") 'On Error Resume Next 'If Not (Application.Intersect(Range("H2"), Target) Is Nothing) Then _

Set pasteSheet = Worksheets(ActiveSheet.Range("H2"))

copySheet.Range("G5:AA5").Copy pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues Application.CutCopyMode = False Application.ScreenUpdating = True End Sub

'I have found another way around the problem to copy paste cells to certain sheet then on 'staff sheet a formula in a table to tests column A value on data sheet for name and only 'transpose those rows

Sub Macro1()
Range("A2:J2").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Sheets("Sheet1").Select

End Sub

'Formula on staff sheet is -=IF(Sheet3!A:A="Column1",Sheet3!$B:$B,"")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM