根据从sheet2到sheet1的条件“Date”值复制一行数据

时间:2011-06-21 16:15:28

标签: excel-vba excel-2007 excel-2003 worksheet-function vba

使用Excel 2007进行开发,但需要与2003兼容。

问题:

工作簿有两张。第二页包含数据,列A到M.列C的格式为日期值。并非所有行都包含C列中的值。

Sheet One有3个'选项按钮(表单控件),标记为合同日期,生效日期和结束日期。选择合同日期时,需要使用条件过滤器查询第2页C列(此处包含日期)的数据...如果日期<今天的日期+ 14天......如果为真,则从C13行开始将该行的C列从M行复制到Sheet One。继续,直到所有数据行都经过测试。

如果选择了另一个“选项按钮”,则第一个查询的结果将替换为第二个查询的结果。

以下是我一直在处理的代码,但它不起作用。

  

Sub OptionButton1_Click()

     

Application.ScreenUpdating = False

     

TEMPLATE_SHEET =“Data_Input”

     

Database_sheet =“Carrier”

     

myzerorange =“C”& ActiveWindow.RangeSelection.Row& “:”& “M”& ActiveWindow.RangeSelection.Row

     

mycompany =“C”& ActiveWindow.RangeSelection.Row

     

mydate =“D”& ActiveWindow.RangeSelection.Row

     

Database_sheet = ActiveSheet.Name

     

DATABASE_RECORDS =表格(Database_sheet)。范围(“C2:C1000”)   Count_Row = 13

     

如果Range(mycompany)<> “”那么

     

如果Range(mydate)<> “”那么

   'Range(mydate) = contractdate
       If mydate < DateAdd("d", 14, "Today()") Then

           Range(myzerorange).Copy
           Sheets(TEMPLATE_SHEET).Select

           'To identify the next blank row in the database sheet

           DATABASE_RECORDS = Sheets(TEMPLATE_SHEET).Range("C13:C1000")
           'To identify the next blank row in the data_Input sheet
           For Each DBRECORD In DATABASE_RECORDS
               If DBRECORD <> "" Then
                 Count_Row = Count_Row + 1
               Next DBRECORD

           Sheets(TEMPLATE_SHEET).Range("C" & Count_Row).Select
           ActiveSheet.Paste

           'Return to origin and check for another contract date
           Sheets(Database_sheet).Select
       Else

       End If
     

否则

     

结束如果

     

结束如果

     

Application.ScreenUpdating = True

     

End Sub

这个修改后的代码仍然不起作用......不确定是什么挂起来......

  

`Sub CopyRowConditional()

     

     

Application.ScreenUpdating = False

     

Srownumber = 2'源表格行号“Data_Input”

     

Trownumber = 13'目标表行号“Carrier”

     

待办事项

     

Srownumber = Srownumber + 1

     

Trownumber = Trownumber + 1

     

If Cells(Srownumber,3).Value =“”然后退出执行

     

If Cells(Srownumber,4).Value&lt; DateAdd(“d”,14,“Today()”)然后

   For Column = 3 To 13

   Sheets(template_sheet).Cells(Trownumber, Column).Value = >Sheets(Database_sheet).Cells(Srownumber, Column).Value

   Next Column
     

'结束如果

     

结束如果

     

循环

     

Application.ScreenUpdating = True

     

结束Sub`

2 个答案:

答案 0 :(得分:1)

这就是我对你的问题的想法。查看评论。您需要将按钮单击绑定到CopyRowConditional。

Sub CopyRowConditional()

Do

i = i + 1

    If Cells(i, 1).Value = "" Then Exit Do
                    ' this is to exit the loop when you reach an empty row

    If Cells(i, 1).Value = 10 Then ' this is where you put
                    ' the condition that triggers the copy
                    ' here I just put 10 as an example

        TargetRow = 4 ' this is where you need to determine how
                      ' you select the row that will receive the
                      ' data you're copying in the Target sheet
                      ' If you need to check for an empty row
                      ' you can add a Do ... Loop statement
                      ' that stops when the row is good

        For j = 1 To 14 ' this is where you loop in to the
                        'column of the Source sheet

        Sheets("Target").Cells(TargetRow, j).Value = Sheets("Source").Cells(i, j).Value
        ' this is the line that actually does the copying, cell by cell
        ' if you need to change the column index, just write .Cells(i, j+ n).value
        ' where n is any offeset you need


        Next j

    End If

Loop

End Sub

答案 1 :(得分:0)

这看起来很容易,所以我的猜测是你不太了解VBA。像其他人所说的那样,该网站不是关于构建您的应用程序;它是关于构建应用程序帮助构建应用程序的其他人的人。

作为指针,您应该能够在演出网站上发布您的问题,并在几个小时内完成您的项目。如果你想亲自见面,或者如果你对虚拟的确定,那就试试craigslist。

希望这有帮助。