【发布时间】:2021-09-23 07:55:32
【问题描述】:
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
但即使输入了日期,它也只会循环,并且不会继续使用代码或类型不匹配的错误。
任何指导将不胜感激,谢谢
【问题讨论】: