我需要遵循以下协议:
我正在扫描Sheet1并为该工作表上的每个唯一empName选择单独的empName工作表
在单个empName工作表上捕获O o列中最后一个单元格中的值
将值存储在变量tper中(它是一个百分比)
选择sheet1
将标题写入列N1
选择N列中的第一个空单元格(不包括标题)
将tper的值写入N列中的选定单元格
重复,直到所有empNames都已从Sheet1处理
我的语法似乎应该执行,直到此行lr1 = Cells(Rows.Count, 13).End(xlUp).Row
,它会抛出错误
错误无效限定符
为了遵循上述协议,我需要重新编写什么内容?
Function Test()
Dim lr As Long, i As Long, lr1 As Long, i1 As Long
Dim WS As Worksheet, empName As String, tper As Variant
Set WS = ActiveSheet
lr = Cells(Rows.Count, 2).End(xlUp).Row
For i = 2 To lr
empName = WS.Cells(i, 2).Value
Sheets(empName).Select
tper = "=LOOKUP(2,1/(O:O<>""),O:O)"
Sheets("Sheet1").Select
Range("N1").FormulaR1C1 = "Percent"
lr1 = Cells(Rows.Count, 13).End(xlUp).Row
For i1 = 2 To lr1
lr1.Cells.FormulaR1C1 = tper
Next i1
Next i
End Function
答案 0 :(得分:2)
我试图修改你的代码。看看这是否有效。因为你想循环遍历第一个for循环所处理的Sheet1中的所有员工,所以我摆脱了第二个循环。
Sub Test()
Dim empName As String, tper As Variant
Dim WS As Worksheet, empSheet As Worksheet
Set WS = Sheets("Sheet1")
Dim lr As Long
lr = WS.Cells(Rows.Count, 2).End(xlUp).Row
Dim i As Long
For i = 2 To lr
'scanning Sheet1 and for each unique empName
empName = WS.Cells(i, 2).Value
'selecting the individual empName worksheet
'just set to variable. no need to select
Set empSheet = Sheets(empName)
'on the individual empName worksheet capturing the value in the last cell in column O
'Storing the value in variable tper (it's a percentage)
tper = empSheet.Range("O" & Rows.Count).End(xlUp).Value
'Selecting Sheet1
'Writing a header to column N1
WS.Range("N1").FormulaR1C1 = "Percent"
'Selecting the 1st empty cell in column N (excluding the header)
WS.Range("N" & Rows.Count).End(xlUp).Offset(1, 0) = tper
'Repeat until all empNames have been processed from Sheet1
' next i will reapeat for the next employee in sheet 1
Next i
End Sub
您还提到了
为每个唯一的empName
代码不会检查。