填充单元时,我在运行几个单元格后会遇到运行时错误13

发布于 2025-01-29 12:30:35 字数 840 浏览 1 评论 0原文

我正在尝试构建一个代码,该代码将帮助我根据另一个工作表中的2个不同表格的团队名称填充列表。

该代码实际上在前10个单元格中运行良好,然后我突然得到一个运行时错误13“ type Inbatch”,我无法确定它的代码是什么,

我的代码是,

Sub populateteam()

Dim wsAkasaka As Worksheet
Dim wsList As Worksheet

Set wsAkasaka = ThisWorkbook.Worksheets("Akasaka")
Set wsList = ThisWorkbook.Worksheets("All Japan")

Dim consultant As String
Dim manager As String
Dim team As String

consultant = wsAkasaka.Range("b" & (ActiveCell.Row)).Value
manager = Application.VLookup(consultant, wsList.Range("a13:c200"), 3, False)
team = Application.VLookup(manager, wsList.Range("E2:F11"), 2, False)

If IsEmpty(ActiveCell.Value) Then
ActiveCell.Value = team
ActiveCell.Offset(1, 0).Select

End If


End Sub

如果有人可以给我一个启示发生以及我该如何解决。

  • 编辑 我忘了写我在“团队”声明上的错误, 我取出它并用“经理”运行了代码,但对此没有任何问题,但是当我要求团队时,我会收到错误

I am trying to build a code that will help me populate a list with the team names based on 2 different tables in another worksheet.

The code actually runs quite well for the first 10 cells, and then I suddenly get a Runtime error 13 "Type mismatch" and I cannot figure what is wrong with it

the code I have is

Sub populateteam()

Dim wsAkasaka As Worksheet
Dim wsList As Worksheet

Set wsAkasaka = ThisWorkbook.Worksheets("Akasaka")
Set wsList = ThisWorkbook.Worksheets("All Japan")

Dim consultant As String
Dim manager As String
Dim team As String

consultant = wsAkasaka.Range("b" & (ActiveCell.Row)).Value
manager = Application.VLookup(consultant, wsList.Range("a13:c200"), 3, False)
team = Application.VLookup(manager, wsList.Range("E2:F11"), 2, False)

If IsEmpty(ActiveCell.Value) Then
ActiveCell.Value = team
ActiveCell.Offset(1, 0).Select

End If


End Sub

If anyone could give me a light for why this is happening and how could I fix it.

  • Edit
    I forgot to write I get the error on the "team" declaration,
    I took it out and ran the code populating with "manager" and had no problem with it, but when I ask for the team I get the error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2025-02-05 12:30:35

问题是您将变量定义为字符串,并且如果您的vlookup返回错误(如果vlookup找不到任何东西),则错误不能施加到字符串中,您会得到不匹配错误

因此,我建议以下内容:

' your code here …

Dim consultant As String
consultant = wsAkasaka.Range("b" & (ActiveCell.Row)).Value

Dim manager As Variant
manager = Application.VLookup(consultant, wsList.Range("a13:c200"), 3, False)

If IsError(manager) Then  ' check if consultant was found, if not exit
    MsgBox "Consultant """ & consultant & """ not found."
    Exit Sub
End If

Dim team As Variant
team = Application.VLookup(manager, wsList.Range("E2:F11"), 2, False)

If IsError(team) Then  ' check if manager was found, if not exit
    MsgBox "Manager """ & manager & """ not found."
    Exit Sub
End If

' your code here …

The issue is that you define your variables As String and if your VLookup returns an error (in case VLookup doesn't find anything), this error cannot be cast into a String and you get a Mismatch Error.

Therefore I recommend the following:

' your code here …

Dim consultant As String
consultant = wsAkasaka.Range("b" & (ActiveCell.Row)).Value

Dim manager As Variant
manager = Application.VLookup(consultant, wsList.Range("a13:c200"), 3, False)

If IsError(manager) Then  ' check if consultant was found, if not exit
    MsgBox "Consultant """ & consultant & """ not found."
    Exit Sub
End If

Dim team As Variant
team = Application.VLookup(manager, wsList.Range("E2:F11"), 2, False)

If IsError(team) Then  ' check if manager was found, if not exit
    MsgBox "Manager """ & manager & """ not found."
    Exit Sub
End If

' your code here …
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文