[英]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. 这种模式将使调试和向代码添加功能变得更加容易。
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
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.