将选定行中的值复制到另一个工作表中的特定单元格 - 宏

时间:2018-02-03 17:21:45

标签: excel vba excel-vba

我有一个项目列表和一个新的工作表按钮,如下所示:LIKE THIS

对于每个项目,您必须能够在单击该按钮时创建新工作表。它从模板复制工作表,一个工作表用于单行。现在,我希望能够将我选择的行中的一些信息复制到新的工作表单元格,当我单击按钮并将工作表重命名为其中一个单元格(ID)中的值时。我需要的值是ID和名称,可能还有一些。

Sub AddNameNewSheet2()
Dim CurrentSheetName As String

CurrentSheetName = ActiveSheet.Name

'Add New Sheet
Dim i As Byte, sh As Worksheet
For i = 1 To 1
Sheets("Predlozak").Copy After:=Sheets("Predlozak")
Set sh = ActiveSheet

Next i

On Error Resume Next

'Get the new name
 ActiveSheet.Name = InputBox("Name for new worksheet?")

'Keep asking for name if name is invalid - Here i want to change worksheet name to a specific cell value from selected row.          
Do Until Err.Number = 0
    Err.Clear
    ActiveSheet.Name = InputBox("Try Again!" _
      & vbCrLf & "Invalid Name or Name Already Exists" _
      & vbCrLf & "Please name the New Sheet")
Loop
On Error GoTo 0

End Sub

任何人都知道如何才能完成这项工作?

1 个答案:

答案 0 :(得分:0)

这就是我提出的问题,我也认为复选框是一种很好的选择方式,它非常简单:

Sub AddNameNewSheet2()
Dim x As Long
Dim wks As Worksheet
Dim IdCell As Range, NamCell As Range, FormCell As Range

Set wks = ActiveSheet
Set IdCell = wks.Range("A:A").Find("TRUE").Offset(0, 1)
Set FormCell = IdCell.End(xlToRight)
Set NamCell = IdCell.Offset(0, 1)

    Sheets.Add After:=Sheets(1), Type:= _
        "C:\Users\*yournamehere*\AppData\Roaming\Microsoft\Templates\*yourtemplatehere*.xltm"

    ActiveSheet.Name = NamCell
Dim wks2T As ListObject
Set wks2T = ActiveSheet.ListObjects(1)

With wks2T
    .ListColumns(1).Range(2) = IdCell.Value
    .ListColumns(2).Range(2).Value = NamCell.Value
    .ListColumns(3).Range(2).Value = FormCell.Value
End With

End Sub

我将模板设为一个表,因为更容易指定确切放置的位置。随着未来添加更多数据,它更易于管理。

Sheet1 image

Sheet2 image