2

我有两张纸:

-预订从表单(car nrregistration nr、和)中car model获取数据。和的值来自 dispo 通过 vlookup) - dispo实际上只是一个日历,所有日期都在第一行和前四列中:、和.starting datereturn dateregistration nrcar model
car nrregistration nrcar modeltoday

我的程序必须将表单中的数据注册到“预订”(有效)中,然后从中找到“dispo”中对应于的单元格,starting datecar nr在其中放置一个“x”(实际上我想要一个“x " 在starting date和之间的所有单元格中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 date和之间的所有单元格中放置一个“x” return date
4

1 回答 1

0

您正在不同类型/值之间创建条件(从逻辑上讲,不成立): dispo.Cells(1, c).Value = txtdepart如果两个变量具有相同的类型(和内容),即 string = string 或 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,因此这个条件只会验证是否dispo.Range("A" & l).Value等于 1。

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

于 2013-08-01T10:51:05.807 回答