如何从日期获得月份?

时间:2017-10-30 21:41:41

标签: excel-vba vba excel

我试图从包含约会的单元格获得一个月的时间?我试图做以下事情。但它会抛出类型不匹配的运行时错误#13。你会怎么做呢,我尝试了很多解决方案,但它不起作用。

在我的样式表中,我有where I put start and end values and have blank spaces for the Macro to write in

Sub Search()

Dim accountTxt As String
Dim propertyTxt As String
Dim startMonth As Integer
Dim endMonth As Integer

Dim iAccount As String
Dim iProperty As String

Dim acum As Integer
Dim rowMonth As Integer

accountTxt = ActiveSheet.Cells(2, 17).Text
propertyTxt = ActiveSheet.Cells(3, 17).Text

startMonth = ActiveSheet.Cells(5, 17).Value
endMonth = ActiveSheet.Cells(6, 17).Value

For i = 1 To 800
    iAccount = ActiveSheet.Cells(i, 8).Value
    iProperty = ActiveSheet.Cells(i, 7).Value
    rowMonth = Month(ActiveSheet.Cells(i, 1).Value)

    If rowMonth >= startMonth And rowMonth <= endMonth Then
        If InStr(1, iAccount, accountTxt) Then
                Cells(i, 12).Interior.ColorIndex = 4
                acum = acum + Cells(i, 12).Value
        End If
    End If
Next i
Cells(8, 17).Value = acum
End Sub

3 个答案:

答案 0 :(得分:1)

假设我们在 B9 中有类似内容:

enter image description here

它可能是真实日期,也可能只是一个看起来像日期的字符串。在任何一种情况下:

import javax.imageio.ImageIO;

public class Program extends Canvas implements Runnable{
private static final long serialVersionUID = -7579474834886333524L;

private Thread thread;
private boolean isRunning = false;

private Måler måler;
private MålerLille målerlille;

public Program() {
    new Controller();

    målerlille = new MålerLille(1920, 1080, "screen3", this);
    måler = new Måler(1920, 1080, "screen2", this);

    GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();

    gs[2].setFullScreenWindow(MålerLille.frameML);
    gs[1].setFullScreenWindow(Måler.frame);

将月份作为数字或字符串:

enter image description here

答案 1 :(得分:1)

让加里的学生回答:

Sub PerhapsADate()
    Dim s As Date, MonthAsNumber As Long
    Dim MonthAsString As String

    s = Range("B9").Text

    MonthAsNumber = DatePart("m", s)
    MonthAsString = Format(s, "mmmm")

    MsgBox MonthAsNumber & vbCrLf & MonthAsString
End Sub

此解决方案略有不同,因为键入&#34; s&#34;作为日期强制将文本识别为日期,因此在国际日期格式标准不同的情况下不太容易失败。

答案 2 :(得分:0)

我遇到类型不匹配,因为迭代器开始在&#34;日期&#34;处读取Date列。标题而不是日期值(例如01-10-2017)