简体   繁体   English

如何基于特定列而不是可变行中单元格的值命名工作表,同时创建/复制工作表?

[英]How can I create/copy a worksheet while naming it based on the value of a cell in a specific column but variable row?

Essentially I'm creating a tracking sheet which will have a cell on it that, when clicked, will create a new excel sheet in the same workbook. 本质上,我正在创建一个跟踪表,上面将带有一个单元格,单击该单元格将在同一工作簿中创建一个新的Excel表。 For testing purposes I'm currently just having it create a new sheet, but eventually I'll have a sheet that it'll copy. 为了进行测试,我目前只是让它创建一个新工作表,但最终我会得到一个要复制的工作表。 What I need help with is, how do I get VB to pull a cell value to use as the name of the new/copied sheet? 我需要帮助的是,如何使VB提取一个单元格值用作新的/已复制工作表的名称? Here's the scenario: 这是场景:

Each row will have a Client column (which is Column C) which I want to use for the names of the workbooks that will be created. 每行将有一个Client列(即C列),我想将其用作将要创建的工作簿的名称。 I'm trying to have a cell (say column R in that row) that when clicked creates a new worksheet and pulls in the value of column C in that row as the worksheet's name. 我正在尝试创建一个单元格(例如该行中的R列),单击该单元格将创建一个新工作表,并将该行中C列的值作为工作表的名称。

So, say Row 5 has "Test Client" in C5. 因此,假设第5行在C5中具有“测试客户端”。 When R5 is clicked, I want it to create a sheet that is named "Test Client". 单击R5时,我希望它创建一个名为“ Test Client”的工作表。 I've seen solutions that use loops to go through the column and create a sheet for each, but that wouldn't really work for my scenario as I'd need them to be created on the fly and not always for each row. 我已经看到了使用循环遍历该列并为每个循环创建工作表的解决方案,但是这对于我的场景而言并不能真正起作用,因为我需要即时创建它们,而不是总是为每一行创建它们。

I know how to create the sheets in vb but my issue is getting the name. 我知道如何在vb中创建工作表,但我的问题是获取名称。 Is there a way to get vba to pull the name from column C for the row in which it was activated? 有没有办法让vba从激活它的行的C列中提取名称? So if it was activated for Row 5, it pulls C5, if it was Row 10, it pulls C10 etc. 因此,如果为第5行激活了它,则拉C5;如果为第10行激活了它,则拉C10,依此类推。

Any suggestions would be greatly appreciated, I'm currently using this to create the sheets: 任何建议将不胜感激,我目前正在使用它来创建工作表:

Sub CreateSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:= _
         ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
End Sub

and this to call: 这个叫:

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

If Target.Row > 5 And Target.Column = 18 And Target.Count = 1 Then Call CreateSheet

End Sub

The code below reads the value in Column C for the relevant row, and then passes it as a String to your Function: 下面的代码读取相关行的C列中的值,然后将其作为String传递给您的Function:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Row > 5 And Target.Column = 18 And Target.Count = 1 Then
    Dim ShtName         As String

    ShtName = Cells(Target.Row, "C").Value
    Call CreateSheet(ShtName)
End If

End Sub

This is your function, I've added a String that is passed representing the worksheet name: 这是您的函数,我添加了一个传递的代表工作表名称的String

Public Sub CreateSheet(ws_Name As String)

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:= _
         ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))

ws.Name = ws_Name

End Sub

Update: As Shai Rado pointed out I was missing an error handler. 更新:正如Shai Rado指出的那样,我缺少一个错误处理程序。

You should test to see if the worksheet exists first. 您应该测试看看工作表是否首先存在。 This pattern will make it easier to debug and add functionality to your code. 这种模式将使调试和向代码添加功能变得更加容易。

Worksheet Module 工作表模块

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim ws As Worksheet
    Dim WorksheetName As String

    If Target.Row > 5 And Target.Column = 18 And Target.Count = 1 Then

        WorksheetName = Cells(Target.Row, "C").Value

        Set ws = getWorkSheet(WorksheetName)

        If Not ws Is Nothing Then Set ws = getNewWorkSheet(WorksheetName)           

    End If

End Sub

Standard Module 标准模块

Function getWorkSheet(WorksheetName As String, Optional WorkbookName As String) As Worksheet
    If Len(WorkbookName) = 0 Then WorkbookName = ThisWorkbook.Name

    With Workbooks(WorkbookName)
        On Error Resume Next
        Set getWorkSheet = .Worksheets(WorksheetName)
        On Error GoTo 0
    End With

End Function

Function getNewWorkSheet(WorksheetName As String, Optional WorkbookName As String) As Worksheet
    Dim ws As Worksheet

    If Len(WorkbookName) = 0 Then WorkbookName = ThisWorkbook.Name

    With Workbooks(WorkbookName)
        Set ws = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
        On Error Resume Next
        ws.Name = WorksheetName

        If Err.Number = 0 Then
            Set getNewWorkSheet = ws
        Else
            ws.Delete
        End If
        On Error GoTo 0

    End With
End Function

暂无
暂无

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

相关问题 如何根据该行中的单元格值复制特定行并将其粘贴到匹配的工作表 - How can I copy specific rows based on a cell value in that row and paste it to a matching worksheet 如何根据工作表最后一列中的单元格值隐藏行? - How to hide row based on cell value in last column of worksheet? 如何根据多个单元格值将一个工作表中的特定行复制到另一个工作表? - How can I copy specific rows in one worksheet to another based on multiple cell values? 根据列中的日期将行中的特定单元格复制到月工作表 - Copy specific cells in a row to month worksheet based on date in column Excel宏-如何根据特定的单元格值复制/拆分行 - Excel Macro - How to copy/split row based on specific cell value 嗨! 如何根据条件复制特定的数据行并将其粘贴到同一工作表的新列中,并在它们之间留有空白列 - Hi! How can I copy specific rows of data based on criteria and paste those to a new column on the same worksheet with a blank column between them 复制列中的特定单元格并粘贴到另一个工作表中 - Copy specific cell in column and paste in another worksheet VBA:根据单元格的值将工作表1中的单元格复制到工作表2 - VBA: Copy cell in worksheet1 to worksheet2 based on value of cell VBA Excel:宏,该宏在一行中搜索特定单元格中的某个变量值,然后将值复制并粘贴到此列中 - VBA Excel : Macro that searches a row for a certain variable value from specific cell and then copy&pastes value to this column 如何选择具有最接近日期(在一列中)的特定单元格值过滤来选择特定行? - How can I select a specific cell value filtering with the nearest date (in a column) to select a specific row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM