excel - VBA 选择输入框中指示的项目
问题描述
我想在输入框中输入一个日期后,我希望它选择一个日期或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
解决方案
我认为选择部分缺少你。如果您想在另一本书中查找并选择某个日期,您可以执行以下操作:
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
推荐阅读
- php - PHP 警告:session_set_save_handler():会话处于活动状态时无法更改保存处理程序
- javascript - ios 13中的字符串中缺少卢布货币符号
- javascript - 如何编写javascript来组合多个过滤器
- pandas - 在熊猫中将年份值扩展到月份
- c# - WHERE 子句中的 EF 硬编码值很快,字符串参数很慢
- python - 当我在 python 中使用 elasticsearch 的聚合时,为什么 size=0 不起作用?
- r - R PerformanceAnalytics CoVariance 函数 merge.zoo 错误
- javascript - 如果客户单击指定金额的按钮,如何在输入字段中显示的金额上自动添加逗号?
- reactjs - 在 Windows 10 中创建新的 React 应用程序失败
- android - Actionbar 和 statusbar 之间的间隙,非半透明