【问题标题】:how to find next Available date if it didn't find comended date using VBA如果没有使用 VBA 找到推荐日期,如何找到下一个可用日期
【发布时间】:2018-01-05 07:10:06
【问题描述】:

我在这里需要帮助。我有工作表 1 和工作表 2。在 Sheet1/2 中,我在 B 列中有日期,并且两个工作表日期都不相同,但是当我推荐选择打印日期时,如果 VBA 找不到我的日期,我希望 VBA 选择最近的日期。例如:-如果我要求 VBA 从 17 年 8 月 12 日开始打印,我可以在 sheet1 中选择,但在 Sheet 2 中没有 8 月 12 日,因此它必须选择 13 日或 11 日并打印。在我的编码中,如果它在同一日期,它将打印两张表。但是如果失败了就会显示错误。

代码

Sub CreatePDF()
Dim Sh As Worksheet
Set sh2 = Sheets("Sheet2")
Set sh3 = Sheets("Sheet3")
Dim i, j2, j3, sh2EndCell, sh3EndCell As Integer
Dim closest As Date
Dim W1Enddate As Date

W1Enddate = Application.InputBox("Enter the End Date")
sh2EndCell = sh2.Range("b" & Rows.Count).End(xlUp).Row
sh3EndCell = sh3.Range("b" & Rows.Count).End(xlUp).Row
For i = 2 To sh2EndCell
    If sh2.Range("b" & i).Value = W1Enddate Then
        j2 = i
        Exit For
    End If
Next i

For i = 2 To sh3EndCell
    If sh3.Range("b" & i).Value = W1Enddate Then
        j3 = i


        Exit For
    End If
Next i

sh2.Range("A1", "K" & j2).PrintPreview
sh3.Range("A1", "K" & j3).PrintPreview

Application.ScreenUpdating = False

sh2.PageSetup.PrintArea = ("A1:K" & j2)
sh3.PageSetup.PrintArea = ("A1:K" & j3)
Sheets(Array("sheet2", "sheet3")).Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="", _
OpenAfterPublish:=True
Application.ScreenUpdating = True

End Sub

请看上面我的代码。

【问题讨论】:

  • 你想让代码做什么有两个日期,距离相同。例如,您选择了第 11 个,但是,虽然第 11 个不在表中,第 10 个和第 12 个在。你要哪个?

标签: vba excel date spreadsheet


【解决方案1】:

我认为您的代码存在 2 个问题:

  1. j2 & j3 是变体(不是整数,我认为你想要)
  2. 您的代码没有做任何事情来查找“最接近”的日期 - 您有一个未在任何地方使用的 closest Date 变量

由于 (1),如果未找到与日期完全匹配的日期,j2j3 将不会被定义,因此像 sh3.Range("A1", "K" & j3).PrintPreview 这样的行将崩溃。请注意在我的代码中 j2j3 是如何变成整数的。相比之下,在您的代码中,ij2j3sh2EndCell 的类型未指定,因此默认为 Variant。

为了解决 (2),下面的代码在每种情况下查找最接近的日期。 min 以一个大数字开始,并被 diff 替换,每次发现日期之间的差异较小。请注意,我的代码中没有更多的Exit For,因为它循环遍历所有日期以确保找到最接近的日期。希望对您有所帮助。

Option Explicit
Sub CreatePDF()
Dim Sh As Worksheet, sh2 As Worksheet, sh3 As Worksheet
Set sh2 = Sheets("Sheet2")
Set sh3 = Sheets("Sheet3")
Dim i As Integer, j2 As Integer, j3 As Integer, sh2EndCell As Integer, sh3EndCell As Integer
Dim closest As Date, diff As Long, min As Long
Dim W1Enddate As Date

W1Enddate = Application.InputBox("Enter the End Date")
sh2EndCell = sh2.Range("b" & Rows.Count).End(xlUp).Row
sh3EndCell = sh3.Range("b" & Rows.Count).End(xlUp).Row
min = 100000#
For i = 2 To sh2EndCell
  diff = Abs(W1Enddate - sh2.Range("b" & i).Value)
  If diff < min Then
    min = diff
    j2 = i
  End If
Next i
min = 100000#
For i = 2 To sh3EndCell
  diff = Abs(W1Enddate - sh3.Range("b" & i).Value)
  If diff < min Then
    min = diff
    j3 = i
  End If
Next i

sh2.Range("A1", "K" & j2).PrintPreview
sh3.Range("A1", "K" & j3).PrintPreview

Application.ScreenUpdating = False

sh2.PageSetup.PrintArea = ("A1:K" & j2)
sh3.PageSetup.PrintArea = ("A1:K" & j3)
Sheets(Array("sheet2", "sheet3")).Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="", _
OpenAfterPublish:=True
Application.ScreenUpdating = True

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2015-12-06
    相关资源
    最近更新 更多