2017-05-15 9 views
0

私は彼らの話し言葉と名前に基づいて、ゲストの文字を出力しますマクロを書いた:VBAエクセルVLOOKUPエラー

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 

私はそれを実行するたびに、私はこの行

上のエラーを取得します

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

...エラーが発生します。

aanspreking = Range("s20").Value 

それは上の行です。私は2つの行に分かれているので、どのような結果が返ってくるか見ることができ、#N/Bを返します。

私はその行を別のマクロにコピーして実行すると、正しい値が得られます。私はまったく無名...誰ですか?

+0

これは、あなたが応答を受け入れなかったとあなたはコメント賞ポイントしなかった過去4で4月27日以来、あなたの第五の質問です。多分あなたを助けることは良いビジネスではありません。 – Variatus

+0

あなたの問題は 'gasten'が置かれているシートに関係している可能性があります。あなたのコードでは 'ActiveSheet'に定義されています(何も指定しないのでデフォルトです)。したがって、コード実行時にどのシートがアクティブになっているかによって、結果が異なる可能性があります。デフォルトでも名前でも、 'ActiveSheet'を避けて問題を解決してください。 – Variatus

+0

私は実際にその日から8つの質問をしましたが、そのうちの2つを削除しました。そのうちの2つを削除しました。私は3つだけ回答しました。そのうちの2つを受け入れました。私の2番目の仕事で(2週間で)そうするだけです。コメントはまだありません。 スムースが解決されたら、私自身の質問に答えるべきですか?私はそれを自分で解決するか、コメントした人の助けを借りて解決しますか? 私は職場に戻ってすぐに 'ActiveSheet'を' HSK'(問題のシートの名前)に置き換えようとします... –

答えて

0

@Variatusが示唆しているように、特定のシート上にあると定義されていないようです。

以下のコードは、意図したとおりの文字を正常に印刷します。 amptyセルがあったそうでない場合は、マクロがエラーを返しますので、私は

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