简体   繁体   English

使用VBA和表单按钮复制并粘贴Excel 2013

[英]Using VBA and a Form button to copy and paste in Excel 2013

I hope someone can help me, I need a button in Excel 2013 to carry out the following commands: 我希望有人可以帮助我,我需要在Excel 2013中使用一个按钮来执行以下命令:

  1. In "Sheet1" the range "A2:I2" is copied. 在“Sheet1”中,复制范围“A2:I2”。
  2. The value in A2 is looked up to determine an appropriate worksheet, which is activated and the copied range is pasted into the next available row in the range of B:J. 查找A2中的值以确定适当的工作表,该工作表已激活,复制的范围将粘贴到B:J范围内的下一个可用行中。
  3. Once the data is pasted, the value of the cell in column A in the pasted row of the activated worksheet is returned to the user. 粘贴数据后,激活的工作表的粘贴行中A列中的单元格值将返回给用户。

So for example: 例如:

When I input into Sheet1.A2 "technical report", and click on my button, it would copy Sheet1.A2:I2, then lookup the worksheet called "TEC", activate it, and paste the data into TEC.B2:I2. 当我输入Sheet1.A2“技术报告”,并单击我的按钮时,它将复制Sheet1.A2:I2,然后查找名为“TEC”的工作表,激活它,并将数据粘贴到TEC.B2:I2中。

In my "TEC" sheet, I have a pre-existing list of numbers in column A. When a data range is copied into TEC.B5:I5, the value of TEC.A5 is looked up and returned to the user (who is locked out of TEC and only has access to Sheet1). 在我的“TEC”表中,我在A列中有一个预先存在的数字列表。当数据范围被复制到TEC.B5:I5时,TEC.A5的值被查找并返回给用户(谁是锁定TEC,只能访问Sheet1)。

Importantly, every time this happens, the data must be pasted into a new row, and therefore a new number is returned to the user. 重要的是,每次发生这种情况时,必须将数据粘贴到新行中,因此会向用户返回一个新数字。


I know this is a lot to ask but I have been trying to use bits of code with no results and I am not a very advanced VBA user so any help provided here would be really appreciated! 我知道这有很多要问,但我一直在尝试使用一些没有结果的代码,而且我不是一个非常高级的VBA用户,所以这里提供的任何帮助都会非常感激!

Code so far is: 到目前为止的代码是:

 Sub button() Application.CutCopyMode = False Sheets("sheet1").Range("A2:I2").Copy 'copy document data If Sheets("Sheet1").Range("A2") = "Technical Report" Then Sheets("TEC").Activate ' check for type of document and activate relevant document worksheet ElseIf Sheets("Sheet1").Range("A2") = "Engineering Coordination Memo" Then Sheets("ECM").Activate ElseIf Sheets("Sheet1").Range("A2") = "Critical Design Review" Then Sheets("CDR").Activate ElseIf Sheets("Sheet1").Range("A2") = "Preliminary Design Review" Then Sheets("PDR").Activate ElseIf Sheets("Sheet1").Range("A2") = "Qualification by Similarity and Analysis" Then Sheets("QSR").Activate ElseIf Sheets("Sheet1").Range("A2") = "Qualification by Test Procedure" Then Sheets("QTP").Activate ElseIf Sheets("Sheet1").Range("A2") = "Reliability Report" Then Sheets("REL").Activate ElseIf Sheets("Sheet1").Range("A2") = "Specification Compliance Tabulation" Then Sheets("SCT").Activate End If Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer 'code to select next empty cell in coumn B Dim currentRowValue As String sourceCol = 2 'column B has a value of 2 rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row 'for every row, find the first blank cell and select it For currentRow = 1 To rowCount currentRowValue = Cells(currentRow, sourceCol).Value If IsEmpty(currentRowValue) Or currentRowValue = "" Then Cells(currentRow, sourceCol).Select Exit For End If Next Paste ActiveCell.Offset(columnoffset:=-1).Select Copy Sheets("Sheet1").Activate Sheets("Sheet1").Range("A5").Paste End Sub 

In general I don't like to use copy/paste in VBA but rather assign values to ranges. 一般来说,我不喜欢在VBA中使用复制/粘贴,而是将值分配给范围。 This way you don't have to move back and forth between sheets. 这样您就不必在工作表之间来回移动。 The following will work assuming you have a header row in each worksheet you are moving data to (since rowCount is never returned as zero). 假设您在将数据移动到的每个工作表中都有一个标题行,以下内容将起作用(因为rowCount永远不会返回为零)。

Sub button()

    Dim sheetName As String

    If Sheets("Sheet1").Range("A2") = "Technical Report" Then
    sheetName = "TEC"   ' check for type of document and activate relevant document worksheet

    ElseIf Sheets("Sheet1").Range("A2") = "Engineering Coordination Memo" Then
    sheetName = "ECM"

    ElseIf Sheets("Sheet1").Range("A2") = "Critical Design Review" Then
    sheetName = "CDR"

    ElseIf Sheets("Sheet1").Range("A2") = "Preliminary Design Review" Then
    sheetName = "PDR"

    ElseIf Sheets("Sheet1").Range("A2") = "Qualification by Similarity and Analysis" Then
    sheetName = "QSR"

    ElseIf Sheets("Sheet1").Range("A2") = "Qualification by Test Procedure" Then
    sheetName = "QTP"

    ElseIf Sheets("Sheet1").Range("A2") = "Reliability Report" Then
    sheetName = "REL"

    ElseIf Sheets("Sheet1").Range("A2") = "Specification Compliance Tabulation" Then
    sheetName = "SCT"
    End If

    Dim rowCount As Integer
    Dim sourceCol As Integer

    sourceCol = 2   'column B has a value of 2

    With Sheets(sheetName)
        rowCount = .Cells(Rows.Count, sourceCol).End(xlUp).Row
        .Range("B" & rowCount + 1 & ":I" & rowCount + 1) = Sheets("Sheet1").Range("B2:I2").Value
    End With

    Sheets("Sheet1").Range("A5") = Sheets(sheetName).Range("A" & rowCount + 1).Value

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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