变量函数不使用第二个函数值进行更改

时间:2015-01-14 00:19:59

标签: excel-vba vba excel

正在尝试从

旋转PiqA值

D到N,N到F1,F1到F2和F2回到D.

我们的想法是创建一个工作日历,其中每天都是D,N,F1或F2,但由于某种原因,函数PiqSt中的变量没有被更改;

Public PiqA As String
Public PiqB As String
Public PiqC As String
Public PiqD As String

Sub Calendario()
Cells.Clear

Columns("A:AJ").ColumnWidth = 3.5
Columns("A:AJ").HorizontalAlignment = xlCenter
Cabecalho

End Sub

Sub Cabecalho()
Dim MyYear As Variant
Dim MyMonth As Variant

'MyYear = InputBox("What year ?")
'MyMonth = InputBox("What month ?")
MyYear = "2015"
MyMonth = "2"
TDaysMonth = Day(DateSerial(MyYear, MyMonth + 1, 1) - 1)

Range("A1:AF1").Merge
Range("A1").Value = Format("1/" & MyMonth & "/" & MyYear, "mmmm")
Range("A3").Value = "A"
Range("A4").Value = "B"
Range("A5").Value = "C"
Range("A6").Value = "D"

PiqA = "D"
PiqB = "F1"
PiqC = "N"
PiqD = "F2"

For I = 1 To TDaysMonth
    Cells(2, I + 1).Value = I
    Cells(7, I + 1).Value = Left(Format(I & "/" & MyMonth & "/" & MyYear, "dddd"), 1)

    Cells(3, I + 1).Value = PiqA
    PiqSt ("A")
    'Cells(4, I + 1).Value = PiqB
    'Cells(5, I + 1).Value = PiqC
    'Cells(6, I + 1).Value = PiqD
Next I

End Sub

Function PiqSt(Piq)
If Piq = "A" Then
    PiqA = NextSt(PiqA)
ElseIf Piq = "B" Then
    PiqB = NextSt(PiqB)
ElseIf Piq = "C" Then
    PiqC = NextSt(PiqC)
ElseIf Piq = "D" Then
    PiqD = NextSt(PiqD)
End If
End Function

Function NextSt(Status)
If Status = "D" Then
    Status = "N"
ElseIf Status = "N" Then
    Status = "F1"
ElseIf Status = "F1" Then
    Status = "F2"
ElseIf Status = "F2" Then
    Status = "D"
End If
End Function

1 个答案:

答案 0 :(得分:0)

看起来你没有从函数中传回任何东西。具体来说是NextSt

尝试使用Select Case代替一堆IF语句。

同样PiqSt根本不需要是一个功能。我将包含需要更改的代码。

Sub PiqSt(Piq As String)

    Select Case Piq
        Case Is = "A"
            PiqA = NextSt(PiqA)
        Case Is = "B"
            PiqB = NextSt(PiqB)
        Case Is = "C"
            PiqC = NextSt(PiqC)
        Case Is = "D"
            PiqD = NextSt(PiqD)
    End Select

End Sub

此函数已设置Status,但您从未设置NextSt = Status。函数以自己的名称传回一个值。所以你要说someVariable = NextSt(string)

Function NextSt(Status As String) As String

    Select Case Status
        Case Is = "D"
            Status = "N"
        Case Is = "N"
            Status = "F1"
        Case Is = "F1"
            Status = "F2"
        Case Is = "F2"
            Status = "D"
    End Select

    NextSt = Status

End Function