【问题标题】:Select a fixed columns in a VBA在 VBA 中选择固定列
【发布时间】:2019-08-20 17:50:57
【问题描述】:

我需要有关此 VBA 的帮助。提前致谢。

我想为值选择一个范围(值,整列): FirstDateEndDateNumber。 我的 VBA:

Sub DateTest()
    Dim FirstDate As Date    ' Declare variables.
    Dim IntervalType As String
    Dim Number As Integer
    Dim EndDate As Date
    Dim TempDate As Date
    Dim i As Integer

    IntervalType = "m" ' "m" specifies MONTHS as interval.
    FirstDate = Cells(1, 1).Value
    EndDate = Cells(1, 2).Value
    Number = Cells(1, 3).Value  ' "Number" For the syntax DateAdd.

    ' If the number is not greater than zero an infinite loop will happen.
    If Number <= 0 Then
        MsgBox "Number needs to be greater than 0", vbCritical
        Exit Sub
    End If

    i = 1
    Do Until TempDate = EndDate
       If i <= 1 Then
           TempDate = DateAdd(IntervalType, Number, FirstDate)
       Else
           TempDate = DateAdd(IntervalType, Number, TempDate)
        End If
        i = i + 1
        Debug.Print i
    Loop
    Range("D1").Value = i - 1
End Sub

正如我之前写的那样,我不仅要为第一个单元格运行宏(当前宏对于值 (1,1) (1,2) (1,3) 运行良好 ),正如您在上面看到的 FirstDateEndDateNumber 我想用于所有日期: Column1Column2Column3

我做了一些更改,例如 EndDate:

EndDate = Format(.Cells(lRow, 2).Value)

不起作用,取 1 个单元格的值,该列中的其余值将被忽略。

我试过了:

    FirstDate = Range("A1:A20").Select

    EndDate = Range("B1:B20").Select

    Number = Range("C1:C20").Select

但我得到:“数字需要大于 0”

【问题讨论】:

  • 你查过如何申请.Format吗?除此之外,在第二次尝试中,您试图选择一个范围,同时将值应用于某些变量?我认为您要做的是遍历一个范围。在互联网上四处看看等等。我敢打赌有一些有用的例子可以找到。

标签: excel vba


【解决方案1】:

代码中大概有2个问题:

  • 从 Excel 读取
  • 业务逻辑

关于从工作表中读取,请确保始终提及父工作表。例如,不要写FirstDate = Cells(1, 1).Value,而是写FirstDate = Worksheets("DatesWorksheetName")Cells(1, 1).Value。此外,这是 VBA 的必读主题 - How to avoid using Select in Excel VBA

对于业务逻辑,将从工作表中读取的值替换为一些标准值,并试一试直到它起作用:

Sub TestMe()

    Dim tempDate As Date
    Dim endDate As Date
    Dim firstDate As Date

    firstDate = #5/19/2019#
    endDate = #11/19/2019#
    tempDate = #8/19/2019#

    Dim i As Long: i = 1
    Dim intervalType As String: intervalType = "m"
    Dim number As Long: number = 5

    Do Until tempDate >= endDate
        If i <= 1 Then
           tempDate = DateAdd(intervalType, number, firstDate)
        Else
           tempDate = DateAdd(intervalType, number, tempDate)
        End If
        i = i + 1
        Debug.Print i
    Loop

End Sub

【讨论】:

  • 嗨,Vityata 感谢您抽出宝贵时间,请参阅我添加了 FirstDate = Worksheets("Sheet1").Cells(1, 1).Value EndDate = Worksheets("Sheet1").Cells(1, 2) .Value Number = Worksheets("Sheet1").Cells(1, 3).Value 我得到 Run time error '5' invalid procedure call or agument。
  • @Lorenzo Castagno - 我现在很喜欢,但是你在我的代码中做了替换吗?然后它应该工作,我猜。只是为了确保 - “Cells(1,3)”是“C1”而不是“A3”。
【解决方案2】:

已解决,见下图:

 Sub DateTest()
    Dim FirstDate As Date    ' Declare variables.
    Dim IntervalType As String
    Dim Number As Integer
    Dim EndDate As Date
    Dim TempDate As Date
    Dim i As Integer

    IntervalType = "m" ' "m" specifies MONTHS as interval.

    With ActiveWorkbook.Worksheets(1)
    lLastRow = .UsedRange.Rows.Count

    For lRow = 1 To lLastRow

    FirstDate = Format(.Cells(lRow, 1).Value, "YYYY-MM-DD")
    EndDate = Format(.Cells(lRow, 2).Value, "YYYY-MM-DD")
    Number = .Cells(lRow, 3).Value

    ' If the number is not greater than zero an infinite loop will happen.
    If Number <= 0 Then
        MsgBox "Number needs to be greater than 0", vbCritical
        Exit Sub
    End If

    i = 1
    Do Until TempDate = EndDate
       If i <= 1 Then
           TempDate = DateAdd(IntervalType, Number, FirstDate)
       Else
           TempDate = DateAdd(IntervalType, Number, TempDate)
        End If
        i = i + 1
        Debug.Print i
    Loop
    Cells(lRow, 4).Value = i - 1
    'Range("D1").Value = i - 1

      Next
      End With

    End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-25
    • 2016-09-09
    • 2019-12-24
    • 2021-09-10
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多