[英]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.