VLOOKUP并填写日期范围内的所有单元格

时间:2013-08-01 10:08:25

标签: excel vba variables

我有两张纸:

- 预订从表单(car nrregistration nrcar modelstarting datereturn date获取数据。 registration nrcar model的值来自dispo by vlookup)
- dispo 实际上只是一个日历,其中包含第一行和前四列中的所有日期:car nrregistration nrcar modeltoday的日期。

我的程序必须将表单中的数据注册到“预订”(有效),然后从此处找到与“starting datecar nr对应的”dispo“中的单元格并输入”x “在其中(实际上我想在starting datereturn date)之间的所有单元格中都有一个”x“。

目前,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”吗?

1 个答案:

答案 0 :(得分:0)

您在不同类型/值之间创建条件(逻辑上,不成立): 如果两个变量具有相同的类型(和内容),即{String = string或date = date,则dispo.Cells(1, c).Value = txtdepart将起作用。显然你期待日期,因此你可以使用以下代码来完全确定:

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,因此这个条件只会验证dispo.Range("A" & l).Value是否等于1。

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