[英]Userform, how to add date filter range
I wish to use Userform option for date filters, user will enter "start time" & "end time" and all relevant data will be displayed according to this filter. 我希望对日期过滤器使用“用户窗体”选项,用户将输入“开始时间”和“结束时间”,所有相关数据将根据此过滤器显示。
I used local Macro that use two different cells for data input but file view is bad and this is the reason I want to use Useform option. 我使用了使用两个不同单元格进行数据输入的本地宏,但是文件视图很糟糕,这就是我要使用Useform选项的原因。
My code: 我的代码:
Public Sub MyFilter()
Dim lngStart As Date, lngEnd As Date
lngStart = Range("b2").Value 'assume this is the start date
lngEnd = Range("b3").Value 'assume this is the end date
Range("q:q").AutoFilter Field:=1, _
Criteria1:=">=" & lngStart, _
Operator:=xlAnd, _
Criteria2:="<=" & lngEnd
Range("A1:s3000").Select
Range("A:A").Activate
Selection.Copy
Sheets.Add After:=ActiveSheet
With ActiveSheet
.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Columns("A:A").EntireColumn.AutoFit
.Cells.Select
.Cells.EntireColumn.AutoFit
.Rows("1:1").Select
.Application.CutCopyMode = False
With Selection
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
.AutoFilter
Columns("Q:Q").Select
Selection.NumberFormat = "[$-409]m/d/yy h:mm AM/PM;@"
End With
.Columns("A:A").EntireColumn.AutoFit
.Range("A2").Select
End With
End Sub
The code also copy the data to a new sheet ( any idea how to copy it to a new file?) and changed some cells format. 该代码还将数据复制到新的工作表(您知道如何将其复制到新的文件吗?)并更改了一些单元格格式。
Thanks!! 谢谢!!
For copying paste buffer to a new file, add a new file (instead of a sheet): 要将粘贴缓冲区复制到新文件,请添加新文件(而不是工作表):
Set fNew = = Workbooks.Add(xlWBATWorksheet)
...
fNew.SaveAs Filename:=<file specification>
Paste:=xlPasteValues
literally copies values without any formatting, comments, borders, etc. For keeping source format, simply use ActiveSheet.Paste Destination:=Range("A1")
. Paste:=xlPasteValues
复制值,而没有任何格式,注释,边框等。要保持源格式,只需使用ActiveSheet.Paste Destination:=Range("A1")
。 If - for any reason - it does not work, you can try this: 如果-由于任何原因-它不起作用,您可以尝试以下操作:
.Range("A1").PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
For copying only the filtered rows, use this: 要仅复制过滤的行,请使用以下命令:
Range("A1:S3000").SpecialCells(xlCellTypeVisible).Copy
fNew.Sheets(1).Range("A1").PasteSpecial
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.