应用带有 Or 运算符的 If Then 语句来确定 am/pm

Apply If Then statement with Or operator to determine am/pm

我有一个演示文稿,每张幻灯片上都有一张图片。图片的名称包含拍摄照片的日期和时间。每张幻灯片上都有一个文本框。

我的目标是用图片的名称、日期和时间填充文本框。

我有办法输入图片的名称和日期

我无法将图片的时间分类为下午或上午。我正在尝试使用 If Then Else 语句。所有时间都说下午。

If Then 语句在第 29 行。

Sub PlaceNameofPictureInEverySlideEdit()

    Dim sld As Slide
    Dim shp As Shape
    Dim PicName As String
    Dim PicDate As String
    Dim PicDateDate As Date
    Dim PicDate1 As String
    Dim PicDate2 As String
    Dim PicDate3 As String
    Dim PicDate4 As String
    Dim PicDate5 As String
    Dim PicDate6 As String
    Dim DayNight As String

    For Each sld In Application.ActivePresentation.Slides
        For Each shp In sld.Shapes
            If shp.Type = msoPicture Then
                PicName = shp.Name
            End If
        Next
        
        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                PicDate = shp.TextFrame.TextRange.Characters(1, 10)
                PicDate1 = shp.TextFrame.TextRange.Characters(6, 2)
                PicDate2 = shp.TextFrame.TextRange.Characters(9, 2)
                PicDate3 = shp.TextFrame.TextRange.Characters(1, 4)
                PicDate4 = shp.TextFrame.TextRange.Characters(12, 2)
                PicDate5 = shp.TextFrame.TextRange.Characters(15, 2)
                PicDateDate = CDate(PicDate)

                If PicDate4 = "12" Or "13" Or "14" Or "15" Or "16" Or "17" Or "18" Or "19" Or "20" Or "21" Or "22" Or "23" Then
                    DayNight = " pm"
                Else: DayNight = " am"
                End If
       
                shp.TextFrame.TextRange.Text = PicName & vbNewLine & PicDateDate _
                  & vbNewLine & PicDate4 & ":" & PicDate5 & DayNight
                
                With shp
                    .TextFrame.TextRange.Replace(FindWhat:=".jpg", ReplaceWhat:="") = ""
                    shp.Top = 473.5449
                    shp.Left = 536.4305
                End With
            End If
        Next
    Next

End Sub

在VBA中,使用And/Or时必须重复条件左边:

If PicDate4 = "12" Or "13" Or "14" Or "15" Or "16" Or "17" Or "18" Or "19" Or "20" Or "21" Or "22" Or "23" Then

应该是

If PicDate4 = "12" Or PicDate4 = "13" Or PicDate4 = "14"...

有几种方法可以简化它;一个是 Select Case:

If IsNumeric(PicDate4) Then
    Select Case CLng(PicDate4)
        Case 12 To 23
            DayNight = " pm"
        Case Else
            DayNight = " am"
    End Select
End If