Excel公式 - 比较日期范围

时间:2018-05-31 19:08:20

标签: excel-vba vba excel

如何编写EXCEL VBA使我的日期从04 / JUNE> 04/6月23:59我不能使用'= date + 5/6',因为我必须每天多次运行VBA,它会将我的日期添加到明天/后天。我只想把约会日期定做。请帮忙。

示例将类似于

CELL A1:12月/ 6月 CELL B1:6月6日 CELL C1:15/6月12日HRS CELL D1:= IF C1B2,“OUT OF RANGE”,“好的!”)

在这种情况下,D1仍将显示OUT OF RANGE。

我有很多这样的改变,所以我想写一个VBA来自动转换C1从15/6月 - > 15/6月23:59,这样D1会显示好的!

我尝试在vba中使用Cdate(范围(“D1”))+ 5/6使其成为23:00,并且我在一天中运行此宏几次并且它将继续添加23小时并且使其成为日期换到另一个约会。

2 个答案:

答案 0 :(得分:0)

关于你在说什么?

关于:
- " Excel Formula"
- " EXCEL VBA"

关于:
- "比较日期范围"
- "制作我的约会"

假设,VBA是......

VBA中的Date类型为Double,其中:
- 整数部分作为...的天数(见F1)和
- 一天的部分时间。

所以,你想要的" 04 / JUNE> 04/6月23日:59" 将:

? DateSerial(2018, 06, 4) + ((23& * 60& * 60&) + (59& * 60&)) / 86400&
18-06-04 23:59:00 

是的,当然,您可以使用TimeSerial代替上述内容,但是......它并没有让您正确理解VBA日期。

<强> ----------
ADD:

  

很抱歉,但此代码仅适用于一个单元格,因为我有很多   有日期的单元格,我需要VBA将它们提取出来并转换为   23:59例如我运行一个for循环来改变连续20个单元格   多种范围。而且我会在一天内运行宏几次   每次在该日期添加23小时并使其更改日期?

Public Sub sp_Test_Date()
Dim rSel As Range
Dim i&

Const H23M59 As Double = 1 - 60 / 86400

    Set rSel = Selection
    With rSel
        For i = 1 To .Cells.Count
            With .Cells(i)
                If IsDate(.Value) Then
                    .Offset(0, 1).Value = CDate(Int(.Value) + H23M59)
                End If
            End With ' .Cells(i)
        Next
    End With ' rSel
End Sub

答案 1 :(得分:0)

我尝试将它修改为下面,它让我失望:(

    `

End Sub

/**
 * Class containing logic for a single color channel.
 */
private class AnimatedChannel {
    private boolean increment = true;
    private int value;

    AnimatedChannel(int value) {
        this.value = value;
    }

    /**
     * Changes channel by random amount in [1, 3] keeping it in range [155, 255] 
     */
    private void update() {
        int delta = getRandomNumber();
        if (increment) {
            value += delta;
            if (value > 0xFF) {
                value = (2 * 0xFF) - value; // v = max - (v - max)
                increment = false;
            }
        } else {
            value -= delta;
            if (value < 155) {
                value = (2 * 155) - value; // v = min + (min - v)
                increment = true;
            }
        }
    }
}

private final Random random = new Random();

private int getRandomNumber() {
    return random.nextInt(3 - 1) + 1;
}

private final AnimatedChannel bottomRed = new AnimatedChannel(171);
private final AnimatedChannel bottomGreen = new AnimatedChannel(186);
private final AnimatedChannel bottomBlue = new AnimatedChannel(171);

private final AnimatedChannel topRed = new AnimatedChannel(0xFF);
private final AnimatedChannel topGreen = new AnimatedChannel(0xFF);
private final AnimatedChannel topBlue = new AnimatedChannel(0xFF);

private final AnimatedChannel[] channels = new AnimatedChannel[] {
    bottomRed,
    bottomGreen,
    bottomBlue,
    topRed,
    topGreen,
    topBlue
};

@Override
protected Void call() throws Exception {
    Timeline backgroundAnimator = new Timeline(new KeyFrame(Duration.millis(200), event -> {
        for (AnimatedChannel channel : channels) {
            channel.update();
        }
        backgroundPane.setBackground(new Background(new BackgroundFill(
            new LinearGradient(0, 0, 0, 1, true, CycleMethod.NO_CYCLE,
                new Stop(0, channelsToColor(topRed, topGreen, topBlue)),
                new Stop(1, channelsToColor(bottomRed, bottomGreen, bottomBlue))),
            CornerRadii.EMPTY,
            Insets.EMPTY)));
    }));
    backgroundAnimator.setCycleCount(Timeline.INDEFINITE);
    backgroundAnimator.play();
    return null;
}

private static Color channelsToColor(AnimatedChannel red, AnimatedChannel green, AnimatedChannel blue) {
    return Color.rgb(red.value, green.value, blue.value);
}