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

时间:2017-08-14 07:29:09

标签: excel vba excel-vba type-mismatch

我是stackoverflow的新手,并且发现它非常有用。希望有人会在下面回答我的问题。

Currency1 = Application.WorksheetFunction.HLookup("Currency", _ 
Worksheets("abc").Range("T5:Z6"), 2, False)
...
Currency1 = "USD" Or "CNY" Or "GBP" Or "AUD" Or "NZD" Then

弹出类型不匹配错误。
它在添加“Or ...”语句之前工作正常。

我尝试了以下几行的排列,但它们没有解决问题。

Dim Currency1 As String

If Currency1 = ("USD" Or "CNY" Or "GBP" Or "AUD" Or "NZD") Then

任何帮助将不胜感激,谢谢。

3 个答案:

答案 0 :(得分:0)

If Currency1 = "USD" Or Currency1 = "CNY" Or Currency1 = "GBP" Or Currency1 = "AUD" Or Currency1 = "NZD" Then

答案 1 :(得分:0)

除了If Currency1 = "USD" Or Currency1 = "CNY"之外,还有其他选项,如评论中已提出的那样。它有点复杂,它使用一个额外的函数,检查值是否在数组中。

因此,在这种情况下,我们可以根据我们的需要将数组作为字符串"USD:AUD:DEM"或实数数组Array("USD", "AUD", "DEM")传递。在下面的例子中,我展示了两种选择。

Option Explicit

Public Function b_value_in_array(my_value As Variant, my_array As Variant, Optional b_is_string As Boolean = False) As Boolean

    Dim l_counter

    If b_is_string Then
        my_array = Split(my_array, ":")
    End If

    For l_counter = LBound(my_array) To UBound(my_array)
        my_array(l_counter) = CStr(my_array(l_counter))
    Next l_counter

    b_value_in_array = Not IsError(Application.Match(CStr(my_value), my_array, 0))

End Function

Public Sub TestMe()

    Dim Currency1 As String

    Currency1 = "USD"

    If b_value_in_array(Currency1, Array("USD", "AUD", "DEM")) Then
        Debug.Print "Value is in the array."
    End If

    If b_value_in_array(Currency1, "USD:AUD:DEM", True) Then
        Debug.Print "Value is in the array."
    End If

End Sub

答案 2 :(得分:0)

为什么不简单呢? 声明一个字符串变量来保存所有货币,并使用Instr函数检查Currency1变量是否有效。

import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';

import { IProduct } from './product';

@Injectable()
export class ProductService {
    private _productUrl = './api/products/products.json';

    constructor(private _http: HttpClient) { }

    getProducts(): Observable<IProduct[]> {
        return this._http.get<IProduct[]>(this._productUrl)
            .do(data => console.log('All: ' + JSON.stringify(data)))
            .catch(this.handleError);
    }

    private handleError(err: HttpErrorResponse) {
        // in a real world app, we may send the server to some remote logging infrastructure
        // instead of just logging it to the console
        let errorMessage = `Server returned code: ${err.status}, error message is: ${err.message}`;
        console.error(errorMessage);
        return Observable.throw(errorMessage);
    }
}