繁体   English   中英

VBA Excel vlookup错误

[英]VBA Excel vlookup error

我已经写了一个宏,可以根据客人的口头语言和姓名打印出客人的来信:

Sub VIPbrieven()

'change printersettings here

Application.Dialogs(xlDialogPrinterSetup).Show

'print letters
'set ranges

'this range contains 4 columns; the first column contains the languague, the third contains the salutation, the fourth the word 'guest' in said alnguages
Dim gasten As Range
Set gasten = Range("r8:u13")

'this is the guestlist, containing the guest's names and languages
Dim Lijst As Range
Set Lijst = Range("a8:p120")

Dim kamers As Range
Set kamers = Range("a8:a120")

'only rows containing information need to be checked
Dim rij As Double
For rij = 1 To kamers.SpecialCells(xlCellTypeConstants).Count

'this is supposed to look for the right salutation. works perfecty when i copy just this line to another macro
Range("s20").Value = Application.VLookup(Lijst.Cells(rij, 16).Value, gasten, 3, False)

Dim aanspreking As String
    aanspreking = Range("s20").Value

'groups come with drivers and/or guides, we don't know their names. in those cases we just put 'guest' in said language
If Left(Lijst.Cells(rij, 15).Value, 5) = "Guide" Or Left(Lijst.Cells(rij, 15).Value, 5) = "Drive" Or Left(Lijst.Cells(rij, 15).Value, 5) = "guide" Or Left(Lijst.Cells(rij, 15).Value, 5) = "drive" Then

    Dim naam As String
    naam = Application.VLookup(Lijst.Cells(rij, 16).Value, gasten, 4, False)

    If IsEmpty(Lijst.Cells(rij, 2)) = False Then
    With Sheets(Lijst.Cells(rij, 16).Value)
        .Range("b20").Value = StrConv(aanspreking & " " & naam & ",", 3)
        .PrintOut
    End With
    End If
'if we do know their names, we just put the name
    Else

    Dim naam2 As String
    naam2 = Lijst.Cells(rij, 15).Value


    If IsEmpty(Lijst.Cells(rij, 2)) = False Then
    With Sheets(Lijst.Cells(rij, 16).Value)
        .Range("b20").Value = StrConv(aanspreking2 & " " & naam2 & ",", 3)
        .PrintOut
    End With
    End If
End If


Next rij

Range("s20").ClearContents

End Sub

每当我执行它时,我都会在这行上得到一个错误

aanspreking = Range("s20").Value

这是上面的行:

Range("s20").Value = Application.VLookup(Lijst.Cells(rij, 16).Value, gasten, 3, False)

...导致错误。 我分为两行,所以我可以看到它返回的结果,并且返回#N / B。

当我将该行复制到另一个宏并执行它时,我得到了正确的值。 我完全一无所知...有人吗?

就像@Variatus所建议的那样,似乎范围未定义为在其所在的特定工作表上。

现在,下面的代码可以按预期方式成功打印字母。 我加了

If Not IsEmpty(Lijst.Cells(rij, 16)) Then

因为否则,如果存在一个宽敞的单元格,则宏将返回错误。

Sub VIPbrieven()
Application.ScreenUpdating = False
'change printersettings here

Application.Dialogs(xlDialogPrinterSetup).Show

'print letters
'set ranges

'this range contains 4 columns; the first column contains the languague, the third contains the salutation, the fourth the word 'guest' in said alnguages
Dim gasten As Range
Set gasten = Sheets("HKG").Range("r8:u13")

'this is the guestlist, containing the guest's names and languages
Dim Lijst As Range
Set Lijst = Sheets("HKG").Range("a8:p120")

Dim kamers As Range
Set kamers = Sheets("HKG").Range("a8:a120")

'only rows containing information need to be checked
Dim rij As Double
For rij = 1 To kamers.SpecialCells(xlCellTypeConstants).Count

'this is supposed to look for the right salutation. works perfecty when i copy just this line to another macro
If Not IsEmpty(Lijst.Cells(rij, 16)) Then
Sheets("HKG").Range("s20").Value = Application.VLookup(Lijst.Cells(rij, 16).Value, gasten, 3, False)



'groups come with drivers and/or guides, we don't know their names. in those cases we just put 'guest' in said language
If Left(Lijst.Cells(rij, 15).Value, 5) = "Guide" Or Left(Lijst.Cells(rij, 15).Value, 5) = "Drive" Or Left(Lijst.Cells(rij, 15).Value, 5) = "guide" Or Left(Lijst.Cells(rij, 15).Value, 5) = "drive" Then

    Dim aanspreking As String
    aanspreking = Sheets("HKG").Range("s20").Value

    Dim naam As String
    naam = Application.VLookup(Lijst.Cells(rij, 16).Value, gasten, 4, False)

    If IsEmpty(Lijst.Cells(rij, 2)) = False Then
    With Sheets(Lijst.Cells(rij, 16).Value)
        .Range("b20").Value = StrConv(aanspreking & " " & naam & ",", 3)
        .PrintOut
    End With
    End If

'if we do know their names, we just put the name
Else

    Dim naam2 As String
    naam2 = Lijst.Cells(rij, 15).Value

    Dim aanspreking2 As String
    aanspreking2 = Sheets("HKG").Range("s20").Value

    If IsEmpty(Lijst.Cells(rij, 2)) = False Then
    With Sheets(Lijst.Cells(rij, 16).Value)
        .Range("b20").Value = StrConv(aanspreking2 & " " & naam2 & ",", 3)
        .PrintOut
    End With
    End If
End If

End If
Next rij


Sheets("HKG").Range("s20").ClearContents
Sheets("HKG").Select
Application.ScreenUpdating = True
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM