我的代码遇到了一个简单的问题。我在我的宏中声明了一些值,并且我想将spesified单元格的值(它是一个日期)设置为变量。但它不起作用,当我用F8检查它时,它表明它等于" 0"。
我愿意接受任何建议。
以下是代码的一部分;
If j = 1 And Worksheets(WS_All).Cells(8, 3).Value = "Yeni Egitim" Or j = 1 And Worksheets(WS_All).Cells(8, 3).Value = "Tanitim / Reklam" Then
WA1 = Worksheets(j_WS).Cells(Satir_bul, 7).Value
如您所见," WS_All"," Satir_bul"和" j_WS"在宏中预定义,我看到F8检查的值,但WA1为" 0"。 我也将WA1声明为整数。
答案 0 :(得分:0)
我认为你应该在你的代码中使用括号
If (j = 1 And Worksheets(WS_All).Cells(8, 3).Value = "Yeni Egitim") Or (j = 1 And Worksheets(WS_All).Cells(8, 3).Value = "Tanitim / Reklam") Then
WA1 = Worksheets(j_WS).Cells(Satir_bul, 7).Value
答案 1 :(得分:0)
我稍微修改了一下来尝试代码:
Sub test()
Dim WS_all As String
Dim j_WS As String
Dim WA1 As Variant
Dim Satir_bul As Integer
Dim j As Integer
WS_all = "List1"
j_WS = "List2"
Satir_bul = 1
j = 1
If j = 1 And Worksheets(WS_all).Cells(8, 3).Value = "Yeni Egitim" Or j = 1 And Worksheets(WS_all).Cells(8, 3).Value = "Tanitim / Reklam" Then
WA1 = Worksheets(j_WS).Cells(Satir_bul, 7).Value
MsgBox (WA1)
End If
End Sub
并为我工作。您可能会在Cells(8, 3)
之前或之后和之后的String
中有一些空格,这将不会满足条件。
然后可以使用简单的Trim
函数:
If j = 1 And Trim(Worksheets(WS_all).Cells(8, 3).Value) = "Yeni Egitim" Or j = 1 And Trim(Worksheets(WS_all).Cells(8, 3).Value) = "Tanitim / Reklam" Then