gpt4 book ai didi

excel - 输入框避免空白条目

转载 作者:行者123 更新时间:2023-12-04 22:16:42 25 4
gpt4 key购买 nike

vba 根据用户输入的日期将数据从“源”复制到“最终”选项卡中标签。下面的 vba 有效,但我想收紧流程并避免用户简单地单击确定或取消,因为这会导致 全部 正在复制的源电子表格中的数据

 Public Sub Copydata()

Dim CopySheet As Worksheet
Dim PasteSheet As Worksheet
Dim FinalSheet As Worksheet
Dim nextRow As Long
Dim FinalRow As Long
Dim lastRow As Long
Dim thisRow As Long
Dim myValue As Date
Set ws = ThisWorkbook.Sheets.Add(After:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))

ws.Name = "Export"
' Get the sheet references
Set CopySheet = Sheets("Source")
Set PasteSheet = Sheets("Export")
Set FinalSheet = Sheets("Final")


lastRow = CopySheet.Cells(CopySheet.Rows.Count, "B").End(xlUp).Row
nextRow = PasteSheet.Cells(PasteSheet.Rows.Count, "A").End(xlUp).Row + 1

myValue = InputBox("Enter start date to transfer", "Input Date")

For thisRow = 1 To lastRow

If CopySheet.Cells(thisRow, "B").Value >= myValue Then

CopySheet.Cells(thisRow, "B").EntireRow.Copy Destination:=PasteSheet.Cells(nextRow, "A")


nextRow = nextRow + 1
End If
Next thisRow""
在输入日期之前,我曾考虑过一个循环,例如:
    Do
myValue = InputBox("Enter start date to transfer", "Input Date")

If myValue = "" Then
MsgBox "You must enter a date as dd/mm/yyyy", vbOKOnly, "Invalid Date"
Else
Exit Do
End If
Loop
但即使输入了日期,它也只会循环,并且不会继续使用代码或类型不匹配的错误。
任何指导将不胜感激谢谢

最佳答案

也许这对你有用?一个问题是您声明 myValueDate因此,如果未输入日期,则会出现不匹配的情况。

Sub x()

Dim myValue As Variant

Do
myValue = Application.InputBox("Enter start date to transfer", "Input Date")
If myValue = False Then Exit Sub 'cancel
If IsDate(myValue) Then
Exit Do
Else
MsgBox "You must enter a date as dd/mm/yyyy", vbOKOnly, "Invalid Date"
End If
Loop

End Sub

关于excel - 输入框避免空白条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68376338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com