我不熟悉vba和宏,我需要帮助创建基于列表的其他工作表。所以我有一个表,第一列有一个类列表,第二列有位置,第8列是Trainer,第11列是开始日期。我还需要使用类列表数据重命名复制的选项卡,并将其放入单元格E6中好。问题是我不知道如何获得每个班级的位置,培训师和开始日期,并将其放入复制的标签中(E5中的开始日期,E7中的培训师和E8中的位置。
这是我到目前为止所拥有的:
Sub CreateCATtabs()
On Error GoTo GetOut
Dim cName As Range, cList As Range
Set cList = Sheets("Control").Range("ClassList2017")
For Each cName In cList
If cName.Value = "" Then GoTo GetOut
Sheets("Class Attendance").Copy After:=Sheets(Sheets.Count)
With ActiveSheet
.Name = cName.Value
.Range("E6").Value = cName.Value
End With
Next cName
GetOut:
End Sub
答案 0 :(得分:1)
我认为这会奏效。我重构了一点以避免GoTo语句并声明一些显式对象。正确的错误捕获总是胜过GoTo语句。
Option Explicit
Sub CreateCATtabs()
Dim wsControl As Worksheet, wsAttendance As Worksheet
Set wsControl = Worksheets("Control")
Set wsAttendance = Worksheets("Class Attendance")
Dim cName As Range, cList As Range
Set cList = wsControl.Range("ClassList2017")
For Each cName In cList
If cName.Value <> "" Then
wsAttendance.Copy After:=Sheets(Sheets.Count)
Dim wsCopy As Worksheet
Set wsCopy = ActiveSheet
With wsCopy
.Name = cName.Value
.Range("E6").Value = cName.Value
.Range("E5").Value = cName.Offset(, 10).Value 'offset is 1 minus the column number
.Range("E7").Value = cName.Offset(, 7).Value
.Range("E8").Value = cName.Offset(, 1).Value
End With
Else
Exit For
End If
Next cName
End Sub