将变量写入 VBA 中的单元格
Write Variable To Cell In VBA
我需要遵循以下协议:
我正在扫描 Sheet1 并针对该工作表上的每个唯一 empName 选择单独的 empName 工作表。
在单个 empName 工作表上捕获 O
列最后一个单元格中的值
将值存储在变量 tper 中(它是一个百分比)
选择工作表 1
将 header 写入列 N1
选择 N 列中的第一个空单元格(不包括 header)
将 tper 的值写入 N
列中的选定单元格
重复直到 Sheet1
处理完所有 empNames
我的语法似乎可以正常执行,直到这一行 lr1 = Cells(Rows.Count, 13).End(xlUp).Row
抛出错误
error invalid qualifier
我需要 re-write 什么才能遵循上述协议?
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
我已尝试修改您的代码。看看这是否有效。由于您想遍历第一个 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
你还提到了
for each unique empName
代码不检查。
我需要遵循以下协议:
我正在扫描 Sheet1 并针对该工作表上的每个唯一 empName 选择单独的 empName 工作表。
在单个 empName 工作表上捕获 O
列最后一个单元格中的值
将值存储在变量 tper 中(它是一个百分比)
选择工作表 1
将 header 写入列 N1
选择 N 列中的第一个空单元格(不包括 header)
将 tper 的值写入 N
列中的选定单元格
重复直到 Sheet1
我的语法似乎可以正常执行,直到这一行 lr1 = Cells(Rows.Count, 13).End(xlUp).Row
抛出错误
error invalid qualifier
我需要 re-write 什么才能遵循上述协议?
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
我已尝试修改您的代码。看看这是否有效。由于您想遍历第一个 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
你还提到了
for each unique empName
代码不检查。