简体   繁体   English

尝试创建打开新工作表的excel vba宏,根据原始工作表中相对单元格的值重命名

[英]Trying to create excel vba macro that opens new sheet, renames it according to value of relative cell in original sheet

I have a master list of contacts. 我有一个主要的联系人列表。 I'm trying to create a macro that uses a relative reference point to: 我正在尝试创建一个使用相对参考点的宏:

Open a specific sheet template Give it a name that = value of ActiveCell or first cell activated in macro and copy and pastes information from the master list to the new sheet open 打开特定的工作表模板为其指定一个名称= ActiveCell的值或在宏中激活的第一个单元格并复制并将主列表中的信息粘贴到新工作表打开

I can figure out how to do open the sheet and do the copy and pasting but I always get an error when it comes to renaming the sheet. 我可以弄清楚如何打开工作表并进行复制和粘贴,但在重命名工作表时总是会出错。

ActiveCell.Range("A1,A2:B26").Select
ActiveCell.Offset(1, 0).Range("A1").Activate
ActiveWindow.ScrollRow = 5
ActiveWindow.ScrollRow = 4
ActiveWindow.ScrollRow = 3
ActiveWindow.ScrollRow = 2
ActiveWindow.ScrollRow = 1
ActiveCell.Offset(-1, 0).Range("A1").Select
Sheets("Patient List").Select
Sheets.Add Type:= _
    "C:\Users\Valerie\AppData\Roaming\Microsoft\Templates\Patient-History-Template1.xltx"
Sheets("Patient List").Select
Selection.Copy
Sheets("Patient List").Select
Sheets("Patient List").Name = "Patient List"
Sheets("Patient 1").Select

Below here, is where I'd like the name of the new sheet = the relative value of the first cell activated in the macro instead of "Jones". 在这里,我想要新工作表的名称=在宏中激活的第一个单元格的相对值而不是“琼斯”。 This way I can run the macro and get seperate sheets for each name in columnA. 这样我可以运行宏并为columnA中的每个名称获取单独的工作表。

Sheets("Patient 1").Name = "Jones"
Sheets("Jones").Select
ActiveSheet.Paste
Sheets("Patient List").Select
ActiveCell.Offset(0, 1).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Jones").Select
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveSheet.Paste
Sheets("Patient List").Select
ActiveCell.Offset(0, 1).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Jones").Select
ActiveCell.Offset(2, -1).Range("A1").Select
ActiveSheet.Paste
Sheets("Patient List").Select

You should probably be doing this in a loop, over the range of cells containing patient names. 您可能应该在包含患者姓名的细胞范围内循环执行此操作。

Sub TestAddPatientSheet()
Dim rng As Range
Dim r As Long 'row iterator
Dim patientName As String
Dim patientSheet As Worksheet

Sheets("Patient List").Activate

Set rng = Set rng = Sheets("Patient List").Range("A2:B26")   '<-- this is the range of cells w/patient names in Col A
    For r = 1 To rng.Rows.Count
        patientName = rng(r, 1).Value
        'Creates a new worksheet
        Set patientSheet = Sheets.Add(After:=Sheets("Patient List"), _
            Type:="C:\Users\Valerie\AppData\Roaming\Microsoft\Templates\Patient-History-Template1.xltx")
ResRetry:
        'Attempt to rename the sheet, trapping errors (if any) and allowing re-try
        On Error GoTo ErrName:
        patientSheet.Name = patientName
    Next
Exit Sub

ErrName:
Err.Clear
MsgBox patientName & " is not a valid worksheet name", vbCritical

patientName = InputBox("Please rename the worksheet for " & patientName & "." & _
                        vbCRLF & "Make sure the sheet name doesn't already exist, is " & _
                        "fewer than 32 characters, and does not contain " & vbCRLF & _
                        "special characters like %, *, etc.", "Rename sheet for " & patientName, patientName)
Resume ResRetry


End Sub

暂无
暂无

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

相关问题 VBA Excel尝试创建一个从文件导入数据的宏,然后如果数据等于特定值,则将一个单元格放到新文件的工作表中 - VBA excel trying to create a macro that imports data from file then if the data is equal to a specific value put one cell into a sheet on the new file Excel宏提供输入框,创建新工作表,将数据从原始工作表复制到新工作表 - Excel Macro to give Input Box, Create New Sheet, Copy Data from Original Sheet into New Sheet VBA宏可根据单元格内容将单元格移动到新的Excel表 - VBA macro to move cells to a new sheet Excel based on cell content VBA宏可将单元格移至新工作表并在Excel中删除单元格标题 - VBA macro to move cells to a new sheet and removing cell headers in Excel Excel VBA 创建新工作表并将文本复制到单元格中 - Excel VBA create new sheet and copy text into cell Excel VBA:我切换到新工作簿并想删除工作表,但代码试图删除宏工作簿中的工作表 - Excel VBA: I switched to a new workbook and want to delete a sheet, but code is trying to delete sheet in macro workbook 通过在Excel 2007中使用宏创建新表 - create a new sheet by using macro in excel 2007 使用现有宏在 excel 中创建新工作表 - create new sheet in excel using existing macro 根据单元格值的EXCEL VBA动态工作表名称-单元格中的公式时不起作用 - EXCEL VBA Dynamic Sheet Name according to a cell value - Not working when formula in the cell Excel VBA 将工作表复制到新工作簿并重命名基于工作表的单元格值 - Excel VBA Copy sheet to new workbook with rename sheet based cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM