2016-08-20 4 views
-1

나는 현재 날짜가있는 테이블의 열을 비교하고 참이면 강조 표시하는 VBA를 작성하려고합니다.엑셀 VBA의 현재 날짜가있는 테이블 열을 비교하십시오.

enter image description here

내가 일하고 있어요 코드는 다음과 같습니다 :

Private Sub Workbook_Open() 
    Dim tbl As Excel.ListObject 'Table name 
    Dim lr As Excel.ListRow 'Row index 
    Dim ws As Excel.Worksheet 'Work sheet 
    'column names 
    Dim keepInTouch As Range, invite As Range, present As Range, follow As Range 

    Set ws = ThisWorkbook.Worksheets(1)         'select work book index 1 
    Set tbl = ws.ListObjects("ContactList")        'set ContactList to tbl 
    Set keepInTouch = tbl.ListColumns("Keep in Touch").DataBodyRange 'Select the appropreate header 
    Set invite = tbl.ListColumns("Invite").DataBodyRange 
    Set present = tbl.ListColumns("Present").DataBodyRange 
    Set follow = tbl.ListColumns("Follow").DataBodyRange 
    'MsgBox tbl 
    For Each lr In tbl.ListRows 
     If lr.Range(1, tbl.ListColumns("Keep in Touch").Index).Value <> Date Then 
      keepInTouch.Interior.ColorIndex = xlNone 
      keepInTouch.Font.ColorIndex = 1 
      keepInTouch.Font.Bold = False 
     'If keepInTouch(1).Value = Date And keepInTouch(1).Value <> "" Then 
     ElseIf lr.Range(1, tbl.ListColumns("Keep in Touch").Index).Value = Date Then 
      keepInTouch.Interior.ColorIndex = 3 
      keepInTouch.Font.ColorIndex = 2 
      keepInTouch.Font.Bold = True 
     End If 
     Next lr 
End Sub 

행 19 :

Run time error '438': 
Object doesn't support this property or method. 

무엇

원인 If keepInTouch.Index = Date And keepInTouch.Index <> "" Then 여기

는 예를 들어 테이블 이렇게하는 올바른 방법? 예를 들어 lr.Range(1, 2)

+0

두 번째 열의 셀 [조건부 서식 (https://www.ablebits.com/office-addins-blog/와 쉬울 말아야 2014/06/17/excel-conditional-formatting-dates/# based-current-date) Range는'.Index'를 가지지 않습니다 .. 아마도'.Value'를 의미 할 것입니다 – Slai

+0

@Slai'.Value'를 시도했지만 실행 시간 오류 '13': 유형 불일치' – Amir

+0

'If keepInTouch (1) .Value = Date Then '을 사용하십시오. 이미 길이가 0인지 확인하기 위해 길이가 0 인 문자열을 확인할 필요가 없습니다. 현재 날짜. – Jeeped

답변

0
If lr.Range(1, tbl.ListColumns("Keep in Touch").Index).Value = Date Then 

는 ListRow 범위

keepInTouchIndex = tbl.ListColumns("Keep in Touch").Index 
NameIndex = tbl.ListColumns("Name").Index 

For Each lr In tbl.ListRows 
    With lr.Range.Cells(1, NameIndex) 
     If lr.Range.Cells(1, keepInTouchIndex).Value <> Date Then 
      .Interior.ColorIndex = 3 
      .Font.ColorIndex = 2 
      .Font.Bold = True 
     Else 
      .Interior.ColorIndex = xlNone 
      .Font.ColorIndex = 1 
      .Font.Bold = False 
     End If 
    End With 
Next lr 
+0

동일한 오류 메시지가 나타납니다. '438' :( – Amir

+0

@Amir 변경된 내용을 적용한 후 – Slai

+0

을 조금 변경했습니다. 각 루프가 끝나면 각 셀의 서식이 기본값으로 설정됩니다. * 주 코드가 변경되어 – Amir

관련 문제