VLOOKUP并填充date范围内的所有单元格

我有两张床单:

预订从表单( car nrregistration nrcar modelstarting datereturn date )获取数据。 registration nrcar model是通过查找来自dispo)
dispo实际上只是一天的所有date在第一行和前四列的日历: car nrregistration nrcar modeltoday的date。

我的程序必须将表单中的数据注册到“预订”(即工作),然后从这个单元中find“dispo”对应的starting datecar nr并在其中放置一个“x”(其实我想要在starting datereturn date)之间的所有单元格中都有一个“x” return date)

现在, car nr固定为1.这是我的代码:

 Private Sub cmdajout_Click() Dim dispo As Worksheet Dim booking As Worksheet Dim db As Range Dim ligne As Integer Dim l As Long Dim c As Long Dim maxc As Long Dim maxl As Long Set dispo = ActiveWorkbook.Sheets("Dispo") Set booking = ActiveWorkbook.Sheets("booking") Set db = dispo.Range("A2:C35") vehicule = 1 ' find the first empty row If Sheets("booking").Range("A2").Value = "" Then ligne = 2 Else: ligne = Sheets("booking").Range("A1").End(xlDown).Row + 1 End If ' copy the data from the form to "booking" and get by vlookup the missing data booking.Range("A" & ligne).Value = vehicule imma = Application.WorksheetFunction.VLookup(booking.Range("A" & ligne), db, 2, False) booking.Range("B" & ligne).Value = imma model = Application.WorksheetFunction.VLookup(booking.Range("A" & ligne), db, 3, False) booking.Range("C" & ligne).Value = model booking.Range("D" & ligne).Value = Format(txtdepart, "short date") booking.Range("E" & ligne).Value = Format(txtfin, "short date") booking.Range("F" & ligne).Value = txtclient booking.Range("G" & ligne).Value = txtlocation Unload Me ' book the date in the calendar "dispo" dispo.Select maxc = dispo.Range("A1").End(xlToRight).Column maxl = dispo.Range("A1").End(xlDown).Row For c = 5 To maxc If dispo.Cells(1, c).Value = txtdepart Then 'it's here that i don't understant ... if i write = "05-01-2013" it finds it, but if i write = txtdepart (wich is where in the form i entered the date), it doesnt find For l = 2 To maxl If dispo.Range("A" & l).Value = vehicule Then 'same proble here ... it finds if i enter ="1" but not if i write = vehicule dispo.Cells(l, c).Value = "x" Else: End If Next l Else: End If Next c End Sub 
  1. 有人帮我解决我的问题吗?
  2. 任何人也可以给我的代码,以便在starting datereturn date之间的所有单元格中放置一个“x”?

你在不同的types/值之间创build条件(逻辑上不保留): dispo.Cells(1, c).Value = txtdepart在两个variables具有相同types(和内容)的情况下工作,即string =string或date=date。 显然你期待date,因此你可以使用下面的代码来完全确保:

 If Format(CDate(dispo.Cells(1, c).Value), "dd-MM-yyyy") = Format(CDate(txtdepart), "dd-MM-yyyy") Then 

等价的问题

 If dispo.Range("A" & l).Value = vehicule Then 

两个成员必须是相同的。 在上面,您正在编写vehicule = 1 ,因此这个条件只会validationdispo.Range("A" & l).Value等于1。

不知道你想写在"x"部分。