2017-07-06 117 views
0

我在表格「From」和「To」中有2個日期選擇器。我想將這些日期選擇器中選擇的日期記錄在變量中,然後參考給定範圍和提取數據之間的輸入數據表。我寫了一些代碼。從datepickers中提取日期

Sub PITimeseries() 
Dim a As Object 
Dim b As Object 
Dim c As Date 
Set a = DTPicker1.Value 
Set b = DTPicker2.Value 

Sheet9.Select 
Dim rng As Range, cell As Range, rng1 As Range 
Set rng = Range("B5:B71") 
Set rng1 = Range("H5:H71") 
For Each cell In rng 
If cell.Value = a Or cell.Value = b Then 

example 現在我堅持爲我將如何進行相應的日期一致單元真或假。

回答

0

嘗試了這一點:

Private Sub CommandButton1_Click() 
Dim a As Date, b As Date, c As Date 
Dim sht As Worksheet 
Dim rng As Range, cell As Range, rng1 As Range 

Set sht = Worksheets("Sheet9") 
Set rng = sht.Range("B5:B71") 
Set rng1 = sht.Range("H5:H71") 

a = DTPicker1.Value 
b = DTPicker2.Value 

For Each cell In rng 
c = CDate(cell.Value) 
If c >= a And c <= b Then 
    sht.Cells(cell.Row, "H").Value = "True" 
Else 
    sht.Cells(cell.Row, "H").Value = "False" 
End If 
Next cell 
End Sub 

所以,你的變量類型是一種錯誤和OR只有兩個條件之一必須適應。請不要使用.Select,始終物化.Range, .Cells etc.

+0

謝謝隊友。我繼續使用簡單的月份下拉選擇器而不是日期選擇器。但是這段代碼肯定幫了我。非常感謝! –