Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Excel VBA中使用If语句中的月份_Vba_Excel - Fatal编程技术网

在Excel VBA中使用If语句中的月份

在Excel VBA中使用If语句中的月份,vba,excel,Vba,Excel,我试图编写一个Excel宏,查看A列中的日期,并打印F列中列出的每个月。我试图使用for循环和If/Else语句,但我似乎无法正确计算 y = 2 Range("F2").Formula = "=MONTH(A3)" For x = 4 To RowLast - 1 If Range("A" & x).Month = Range("F" & y) Then Else y = y + 1 Range("F" & y).Fo

我试图编写一个Excel宏,查看A列中的日期,并打印F列中列出的每个月。我试图使用for循环和If/Else语句,但我似乎无法正确计算

y = 2

Range("F2").Formula = "=MONTH(A3)"

For x = 4 To RowLast - 1
    If Range("A" & x).Month = Range("F" & y) Then
    Else
        y = y + 1
        Range("F" & y).Formula = "=MONTH(A" & x & ")"
    End If
Next
这就是我到目前为止所做的,它应该将在单元格A3中找到的第一个月打印到单元格F2(可以工作),然后每隔一天检查一次,直到它在最后一行的上方。if语句应该检查以确保它是一个新的月份,如果是,则将该月份打印到F列中的下一个单元格


如果你有任何问题,请告诉我。谢谢。

我认为是您的if声明造成了这些问题。如果您只是打印月份,这里是否需要if语句?

回答您的特定问题:
month(date)
是一个函数,它返回一个与日期参数的月份相对应的整数。例如,
Month(Now)
将返回3

RowLast = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

y = 2

Range("F2").Formula = "=MONTH(A3)"

For x = 4 To RowLast - 1
    Range("Z2").Formula = "=MONTH(A" & x & ")"
    If Range("Z2").Value = Range("F" & y).Value Then
    Else
        y = y + 1
        Range("F" & y).Formula = "=MONTH(A" & x & ")"
    End If
Next
.Month
不是。
范围
对象的属性,因此您的代码将抛出错误(“对象不支持此属性或方法”)。下面的代码显示了如何以您想要的方式使用
Month()
函数

然而,您的代码提出了一个更广泛的问题。您使用VBA仅仅是为了自动编写公式吗?如果你是,那么一切都很好。但是,当VBA能够更好地为您服务时,您是否可能正在使用工作表函数?例如,您是否有理由使用VBA识别目标月份,而仅通过Excel公式将这些目标月份写入工作表

我之所以提到它,是因为最近有相当多的帖子将其范围限制在如何自动化Excel函数(可能是录制宏的结果),而VBA的功能可能比他们想象的更强大

无论如何,同一个任务有两个非常相似的版本:第一个写公式,第二个写月份。我希望这会引发一些思考,哪种自动化类型适合您的需要:

编写公式的代码:

Public Sub FormulaGenerator()
    Dim ws As Worksheet
    Dim firstRow As Long
    Dim lastRow As Long
    Dim dateRange As Range
    Dim cell As Range
    Dim hitList As Collection
    Dim refMonth As Integer
    Dim thisMonth As Integer
    Dim r As Long
    Dim output() As Variant
    Dim item As Variant

    'Set these for your own task.
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    firstRow = 3
    lastRow = 20

    'Read the values cell by cell
    Set dateRange = ws.Range(ws.Cells(firstRow, "A"), ws.Cells(lastRow, "A"))
    Set hitList = New Collection
    For Each cell In dateRange.Cells
        item = cell.Month
        thisMonth = Month(cell.Value)
        If thisMonth <> refMonth Then
            'It's a new month so populate the collection with the cell address
            hitList.Add cell.Address(False, False)
            refMonth = thisMonth
        End If
    Next

    'Populate the output array values
    ReDim output(1 To hitList.Count, 1 To 1)
    r = 1
    For Each item In hitList
        output(r, 1) = "=MONTH(" & item & ")"
        r = r + 1
    Next

    'Write the output array starting at cell "F2"
    ws.Cells(2, "F").Resize(UBound(output, 1)).Formula = output
End Sub
Public Sub OutputGenerator()
    Dim ws As Worksheet
    Dim firstRow As Long
    Dim lastRow As Long
    Dim dates As Variant
    Dim hitList As Collection
    Dim refMonth As Integer
    Dim thisMonth As Integer
    Dim r As Long
    Dim output() As Integer
    Dim item As Variant

    'Set these for your own task.
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    firstRow = 3
    lastRow = 23

    'Read the dates into an array
    dates = ws.Range(ws.Cells(firstRow, "A"), ws.Cells(lastRow, "A")).Value

    'Loop through the array to acquire each new date
    Set hitList = New Collection
    For r = 1 To UBound(dates, 1)
        thisMonth = Month(dates(r, 1))
        If thisMonth <> refMonth Then
            'It's a new date so populate the collection with the month integer
            hitList.Add thisMonth
            refMonth = thisMonth
        End If
    Next

    'Populate the output array
    ReDim output(1 To hitList.Count, 1 To 1)
    r = 1
    For Each item In hitList
        output(r, 1) = item
        r = r + 1
    Next

    'Write the output array starting at cell "F2"
    ws.Cells(2, "F").Resize(UBound(output, 1)).Value = output
End Sub
公共子公式生成器()
将ws设置为工作表
第一排一样长
最后一排一样长
日期范围变暗为范围
暗淡单元格作为范围
Dim hitList作为集合
将月份设置为整数
将此月设置为整数
变暗,变长
Dim output()作为变量
作为变体的Dim项目
'为您自己的任务设置这些。
设置ws=ThisWorkbook.Worksheets(“Sheet1”)
第一行=3
最后一行=20
'逐单元格读取值
Set dateRange=ws.Range(ws.Cells(第一行,“A”)、ws.Cells(最后一行,“A”))
设置hitList=新集合
对于dateRange.Cells中的每个单元格
项目=单元格。月份
thisMonth=月份(单元格值)
如果这个月是一个月那么
“这是新的一个月,所以用单元格地址填充集合
命中列表.添加单元格.地址(False,False)
refMonth=本月
如果结束
下一个
'填充输出数组值
重拨输出(1对命中列表。计数,1对1)
r=1
对于hitList中的每个项目
输出(r,1)=“=月份(“&item&”)
r=r+1
下一个
'从单元格“F2”开始写入输出数组'
ws.Cells(2,“F”).Resize(UBound(output,1)).Formula=output
端接头
将月份写入整数的代码:

Public Sub FormulaGenerator()
    Dim ws As Worksheet
    Dim firstRow As Long
    Dim lastRow As Long
    Dim dateRange As Range
    Dim cell As Range
    Dim hitList As Collection
    Dim refMonth As Integer
    Dim thisMonth As Integer
    Dim r As Long
    Dim output() As Variant
    Dim item As Variant

    'Set these for your own task.
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    firstRow = 3
    lastRow = 20

    'Read the values cell by cell
    Set dateRange = ws.Range(ws.Cells(firstRow, "A"), ws.Cells(lastRow, "A"))
    Set hitList = New Collection
    For Each cell In dateRange.Cells
        item = cell.Month
        thisMonth = Month(cell.Value)
        If thisMonth <> refMonth Then
            'It's a new month so populate the collection with the cell address
            hitList.Add cell.Address(False, False)
            refMonth = thisMonth
        End If
    Next

    'Populate the output array values
    ReDim output(1 To hitList.Count, 1 To 1)
    r = 1
    For Each item In hitList
        output(r, 1) = "=MONTH(" & item & ")"
        r = r + 1
    Next

    'Write the output array starting at cell "F2"
    ws.Cells(2, "F").Resize(UBound(output, 1)).Formula = output
End Sub
Public Sub OutputGenerator()
    Dim ws As Worksheet
    Dim firstRow As Long
    Dim lastRow As Long
    Dim dates As Variant
    Dim hitList As Collection
    Dim refMonth As Integer
    Dim thisMonth As Integer
    Dim r As Long
    Dim output() As Integer
    Dim item As Variant

    'Set these for your own task.
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    firstRow = 3
    lastRow = 23

    'Read the dates into an array
    dates = ws.Range(ws.Cells(firstRow, "A"), ws.Cells(lastRow, "A")).Value

    'Loop through the array to acquire each new date
    Set hitList = New Collection
    For r = 1 To UBound(dates, 1)
        thisMonth = Month(dates(r, 1))
        If thisMonth <> refMonth Then
            'It's a new date so populate the collection with the month integer
            hitList.Add thisMonth
            refMonth = thisMonth
        End If
    Next

    'Populate the output array
    ReDim output(1 To hitList.Count, 1 To 1)
    r = 1
    For Each item In hitList
        output(r, 1) = item
        r = r + 1
    Next

    'Write the output array starting at cell "F2"
    ws.Cells(2, "F").Resize(UBound(output, 1)).Value = output
End Sub
公共子输出生成器()
将ws设置为工作表
第一排一样长
最后一排一样长
变暗日期
Dim hitList作为集合
将月份设置为整数
将此月设置为整数
变暗,变长
将输出()设置为整数
作为变体的Dim项目
'为您自己的任务设置这些。
设置ws=ThisWorkbook.Worksheets(“Sheet1”)
第一行=3
最后一行=23
'将日期读入数组
dates=ws.Range(ws.Cells(第一行,“A”)、ws.Cells(最后一行,“A”)).Value
'循环遍历数组以获取每个新日期
设置hitList=新集合
r=1至UBound(日期,1)
本月=月份(日期(r,1))
如果这个月是一个月那么
'这是一个新日期,因此使用月份整数填充集合
点击列表。本月添加
refMonth=本月
如果结束
下一个
'填充输出数组
重拨输出(1对命中列表。计数,1对1)
r=1
对于hitList中的每个项目
输出(r,1)=项目
r=r+1
下一个
'从单元格“F2”开始写入输出数组'
ws.Cells(2,“F”).Resize(UBound(output,1)).Value=output
端接头

问题在于A列中有多个日期,其中几个日期的月份相同,但我只希望它在F列中显示一次,这样我就可以根据月份计算另一列的总数。在循环的第一次运行时,您检查A4=Y2是否正确,您也可能在前一个月检查F列您已经定义了它。我试图让它只检查存在的F列值,以便最初找到F2,然后for/if循环应该检查F2,直到创建F3。如果您认为if语句是好的并且是必要的,您可以尝试获取范围(“F”&y)的属性。。。类似于范围(“F”&y”)。月份或范围(“F”&y”)。值我可以将其与一个函数组合以隐藏所有具有类似于月份范围(“D:G”)的列。公式=“=month(1)”列(“D:BC”)。如果列(“D:G”)。Hidden=True,则选择Selection.entireclumn.Hidden=False,否则选择。entireclumn.Hidden=True结束If