根据两个单元格的值更改单元格的值 - VBA Excel

时间:2017-03-28 09:14:35

标签: excel vba excel-vba

我正在尝试通过根据该列中的值和另一列中的值更改一列中的单元格值来为电子表格添加一些自动化。到目前为止我已经得到了下面的代码。如果我使用.text,代码运行良好,但不会更改单元格的值。如果我使用.value,我会收到以下错误消息:

运行时错误' 13:类型不匹配

请有人可以告诉我这里做错了什么。

Sub change_orrtime_4()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'For Each employee In Range("Timesheet_RawData[Employee]")

Dim employee As Range
Dim datefield As Range
Dim tbl As ListObject
Dim tRows As Long
Dim tCols As Long
Dim i As Long



Set tbl = Sheets("Timesheet Data").ListObjects("Timesheet_RawData")


With tbl.DataBodyRange
    tRows = .Rows.Count
'    tCols = .Colummns.Count

End With


With Sheets("Timesheet Data")

Set employee = Sheets("Timesheet Data").Range("Timesheet_RawData[Employee]")
Set datefield = Sheets("Timesheet Data").Range("Timesheet_RawData[Date]")

End With

With Sheets("Timesheet Data")

For i = 2 To tRows


If employee.Value = "Some Name" And datefield.Value = "1" Then ' type mismatch doesnt occur with .text but then nothing works

employee.Value = "Some Name_SomeTeam"


End If

Next i

End With

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

1 个答案:

答案 0 :(得分:0)

您将employee(以及datefield)设置为多个单元格范围,因此您无法访问Value属性,而您可以访问它{{1}如果所有单元格共享相同的文本或其他Text

,则返回文本的属性

因此您必须指向该范围内的特定单元格,例如:

Null

最后你可以按照以下方式重构你的代码:

employee(i).Value