【问题标题】:VBA Selecting the item indicated in the inputboxVBA 选择输入框中指示的项目
【发布时间】:2021-11-16 23:01:20
【问题描述】:

我想在输入框中输入一个日期后,我想它选择一个日期或date_wb中的一个条目。但是,我的宏不仅没有错误地完成它


'Open file
file_name = Application.GetOpenFilename(Title:="Choose a target Workbook")
If file_name <> False Then
   'Set data file
   Set data_wb = Application.Workbooks.Open(file_name)
   
   'Input box

   Do
       inputbx = InputBox("Enter Date, FORMAT; YYY-MM-DD", , Format(VBA.Now, "YYYY-MM-DD"))
       If inputbx = vbNullString Then Exit Sub
       inputstr = Split(inputbx, "-")
       On Error Resume Next
       InputDate = DateSerial(inputstr(2), inputstr(0), inputstr(1))
       On Error GoTo 0
       DateIsValid = IsDate(InputDate)
       If Not DateIsValid Then MsgBox "Please enter a valid date.", vbExclamation
   Loop Until DateIsValid

【问题讨论】:

  • DateSerial 获取参数年、月、日(按此顺序)。您要求用户输入日期 yyyy-mm-dd,在这种情况下您需要 DateSerial(inputstr(0), inputstr(1), inputstr(2))
  • 好的,谢谢,但问题仍未解决:(
  • 您遇到了什么错误?
  • 如果您描述问题可能会有所帮助。会发生什么,您是否收到运行时错误,最终结果是不是您期望的结果还是空白?
  • 它没有从data_wb的输入框中选择结果

标签: excel vba excel-formula


【解决方案1】:

我认为选择部分缺少你。如果您想在另一本书中查找并选择某个日期,您可以执行以下操作:

Dim vDate As Date
Dim Loc As Range

'Open file
file_name = Application.GetOpenFilename(Title:="Choose a target Workbook")
If file_name <> False Then
   'Set data file
   Set data_wb = Application.Workbooks.Open(file_name)
       
    Do
        inputbx = InputBox("Enter Date, FORMAT; YYYY-MM-DD", , format(VBA.Now, "YYYY-MM-DD"))
        If inputbx = vbNullString Then Exit Sub
        On Error Resume Next
        vDate = DateValue(inputbx)
        On Error GoTo 0
        DateIsValid = IsDate(vDate)
        If Not DateIsValid Then MsgBox "Please enter a valid date.", vbExclamation
    Loop Until DateIsValid
       
     
    For Each ws In data_wb.Worksheets
     ws.Activate
     With ws
        Set Loc = .Cells.Find(What:=vDate) 'It will find the firs cell that cointains the date as a date like (09/23/2021)
        If Not Loc Is Nothing Then
            Loc.Select
            Exit For
        End If
        
        Set Loc = .Cells.Find(What:=format(vDate, "yyyy-mm-dd")) 'It will find the date stored as a text like ('2021-09-23)
        If Not Loc Is Nothing Then
            Loc.Select
            Exit For
        End If
      End With
    Next

只需确定日期的存储方式,因为它会作为值找到。

更新代码。

这会找到日期并从找到的列中复制第 109 行到第 123 行的列范围。

Public Sub Main()

Dim vDate As Date
Dim Loc As Range

'Open file
file_name = Application.GetOpenFilename(Title:="Choose a target Workbook")
If file_name <> False Then
   'Set data file
   Set data_wb = Application.Workbooks.Open(file_name)
       
    Do
        inputbx = InputBox("Enter Date, FORMAT; YYYY-MM-DD", , format(VBA.Now, "YYYY-MM-DD"))
        If inputbx = vbNullString Then Exit Sub
        On Error Resume Next
        vDate = DateValue(inputbx)
        On Error GoTo 0
        DateIsValid = IsDate(vDate)
        If Not DateIsValid Then MsgBox "Please enter a valid date.", vbExclamation
    Loop Until DateIsValid
       
     'The previous For each means will look for into all book sheet by sheet
     
     'If you want to look for into an sprecific sheet you have to do on this way
     data_wb.Worksheets("Final").Activate
     With data_wb.Worksheets("Final")
        Set Loc = .Cells.Find(What:=vDate) 'It will find the firs cell that cointains the date as a date like (09/23/2021)
        If Not Loc Is Nothing Then
            'This part just copy the range of 109 to 123 to the found column
            vArr = Split(Cells(1, Loc.Column).Address(True, False), "$")
            colLoc = vArr(0)
            data_wb.Worksheets("Final").Range(colLoc & "109:" & colLoc & "123").Copy
        End If
        
        Set Loc = .Cells.Find(What:=format(vDate, "yyyy-mm-dd")) 'It will find the date stored as a text like ('2021-09-23)
        If Not Loc Is Nothing Then
            'This code just select the range found
            Loc.Select
        End If
    End With
    
    
End If
End Sub

【讨论】:

  • 非常感谢!你很棒。如果我想在输入框中选择的日期列中始终复制相同范围的第 109:123 行,您可以告诉我代码会是什么样子
  • 我的第二个问题是,如果我只查看 data_wb 中的一张纸,例如我的纸名是 Final“对于 Data_wb.Sheets("Final") 中的每个 ws 不起作用
  • 我更新了代码,如果这个答案有帮助,请标记为正确,请
猜你喜欢
  • 2022-11-11
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
相关资源
最近更新 更多