将variables写入VBA中的单元格

我需要遵循以下协议:
我正在扫描Sheet1,并为该工作表上的每个唯一的empNameselect单个的empName工作表。
在单独的empName工作表中捕获列O中最后一个单元格中的值
将值存储在variablestper(这是一个百分比)
selectsheet1
向N1列写入标题
select列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 

我试图修改你的代码。 看看这是否工作。 既然你想要循环Sheet1中的所有员工,这是第一个for循环处理,我摆脱了第二个循环。

 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

该代码不检查。