根据条件“日期”复制一行数据从sheet2到sheet1的值
使用Excel 2007开发,但需要兼容2003。
问题:
工作簿有两张表。第二张表包含数据,从 A 列到 M。C 列的格式设置为日期值。并非所有行都包含 C 列中的值。
表 1 有 3 个选项按钮(表单控件),标记为“合同日期”、“生效日期”和“结束日期”。当选择合同日期时,需要使用条件过滤器查询第二张 C 列(此处包含日期)上的数据...今天的日期 + 14 天...如果为 true,则将该行的 C 列到 M 列复制到第一张工作表的单元格 C13 开始。继续,直到测试完所有数据行。
如果选择另一个“选项按钮”,则第一个查询的结果将替换为第二个查询的结果。
这是我一直在编写的代码,但它不起作用。
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 = Sheets(Database_sheet).Range("C2:C1000") Count_Row = 13
If Range(mycompany) <> "" Then
If Range(mydate) <> "" Then
'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
Else
End If
End If
Application.ScreenUpdating = True
End Sub
修改后的代码仍然不起作用...不知道是什么挂了这个...
Sub CopyRowConditional()
Application.ScreenUpdating = False
Srownumber = 2 'source sheet row number "Data_Input"
Trownumber = 13 'target sheet row number "Carrier"
Do
Srownumber = Srownumber + 1
Trownumber = Trownumber + 1
If Cells(Srownumber, 3).Value = "" Then Exit Do
If Cells(Srownumber, 4).Value < DateAdd("d", 14, "Today()") Then
For Column = 3 To 13
Sheets(template_sheet).Cells(Trownumber, Column).Value = >Sheets(Database_sheet).Cells(Srownumber, Column).Value
Next Column
End If
End If
Loop
Application.ScreenUpdating = True
End Sub
Developing using Excel 2007, but need to be compatible with 2003.
Problem:
Workbook has two sheets. Sheet two contains data, columns A thru M. Column C is formatted for Date values. Not all rows contain a value in column C.
Sheet One has 3 'Option Buttons (form Control), labeled Contract date, Effective Date, and End Date. When contract date is selected, Need data on sheet two, column C (Date is contained here) to be queried with a conditional filter... If date < today's date + 14 days ... If true, copy column C thru M of that row to Sheet One beginning at cell C13. Continue until all data rows have been tested.
If another 'Option Button' is selected, results from first query are replaced by results from second query.
Here is the code I have been working on, but it won't work.
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 = Sheets(Database_sheet).Range("C2:C1000") Count_Row = 13
If Range(mycompany) <> "" Then
If Range(mydate) <> "" Then
'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
Else
End If
End If
Application.ScreenUpdating = True
End Sub
This revised code still doesn't work... not sure what is hanging this up...
Sub CopyRowConditional()
Application.ScreenUpdating = False
Srownumber = 2 'source sheet row number "Data_Input"
Trownumber = 13 'target sheet row number "Carrier"
Do
Srownumber = Srownumber + 1
Trownumber = Trownumber + 1
If Cells(Srownumber, 3).Value = "" Then Exit Do
If Cells(Srownumber, 4).Value < DateAdd("d", 14, "Today()") Then
For Column = 3 To 13
Sheets(template_sheet).Cells(Trownumber, Column).Value = >Sheets(Database_sheet).Cells(Srownumber, Column).Value
Next Column
End If
End If
Loop
Application.ScreenUpdating = True
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我对你的问题的想法。看看评论。您需要将按钮单击绑定到 CopyRowConditional。
This is what I have in mind for your problem. See the comments. You need to bind the button click to CopyRowConditional.
这看起来很容易做到,所以我猜你不太了解 VBA。正如其他人所说,该网站不是为了构建您的应用程序;而是为了帮助您构建应用程序。这是关于构建应用程序的人帮助其他构建应用程序的人。
作为指导,您应该能够在演出网站上发布您的问题,并在几个小时内完成您的项目。如果您想亲自会面或在线会面,或者您可以接受虚拟会议,请尝试 craigslist。
希望这有帮助。
This seems pretty easy to do so my guess is that you don't know VBA very well. Like others have said, the site is not about building your app; it's about people who build apps helping other people who build apps.
As a pointer, you should be able to post your question on a gigs site and get your project done in a matter of hours. Try craigslist if you want to meet in person or elance or if you're ok with virtual.
Hope this helps.