简体   繁体   English

Excel VBA按钮可使用日期条件将数据从一个单元格粘贴到另一个单元格

[英]Excel vba button to paste data from one cell into another cell using date criteria

Somewhat new to VBA excel. VBA有一些新功能。 I've spent a good 4 days trying to figure this out and playing around with the code and I'm finally reaching out for help. 我花了整整4天的时间试图弄清楚这一点,并尝试使用代码,终于可以寻求帮助了。

The gist of what I'm trying to do is use a button that when clicked it takes data that has already been generated and sits in cell L11. 我要执行的操作的要点是使用一个按钮,单击该按钮将获取已生成的数据,并将其放在单元格L11中。 The way the data is supposed to flow is by day in a month. 数据流的方式应该是一个月中的一天。 When data is entered in L11, the day changes each entry. 在L11中输入数据时,日期会更改每个条目。 So when I push the button, I'd like the code to say - based on the day (already generated) in cell L8, take the data in L11 and cut and paste it under Sheet2 and column for that date. 因此,当我按下按钮时,我想让代码说-基于L8单元格中的日期(已生成),获取L11中的数据,并将其剪切并粘贴到该日期的Sheet2和列下。

I've built the following code that moves the data from L11 to sheet 2 cell A2 and then add the data to the next line on sheet2 if data already exists, but to make what I'm trying to do even faster, I'd like to have the code know what date is in L8 and find that date in a column on sheet2 and paste it under that date while removing the copied data from sheet1: 我建立了以下代码,将数据从L11移动到工作表2单元格A2,然后将数据添加到工作表2的下一行(如果已有数据),但是为了使我想做的更快,我希望想要让代码知道L8中的日期,并在sheet2的列中找到该日期并将其粘贴在该日期下,同时从sheet1删除复制的数据:

Sub copypastetosheets2()

    For Each cell In Range("L8:L8")
    If cell.Value = "Monday, April 15" Then
        Range("L11:L11").Select
        Selection.Copy
        Sheets("sheet2").Select
        Range("A2").Select
        ActiveSheet.Paste
        Sheets("sheet1").Select
        Range("L11").Select
        Application.CutCopyMode = False
        Sheets("sheet2").Select
        Range("A2").Select
        Selection.Insert shift:=xlDown, copyorigin:=xlFormatFromLeftOrAbove
        Sheets("sheet1").Select
        Range("L11").Select

I've also used this code on a different button which works and does what I'd like it to do, but it still doesn't recognize the date and to put it in a different column: 我还在不同的按钮上使用了此代码,该按钮可以工作并且可以执行我想要的操作,但是它仍然无法识别日期并将其放在其他列中:

Sub copypastetosheets()
Sheets("sheet1").Range("L11").Copy


With Sheets("April 15").Range("A" & Rows.Count).End(xlUp).Offset(1)
    .PasteSpecial Paste:=xlPasteColumnWidths
    .PasteSpecial Paste:=xlPasteValues
    Worksheets("sheet1").Range("L11:L20").ClearContents
End With

End Sub

Don't mind the Sheets("April 15").... I was using that while I was testing. 不要介意Sheets(“ April 15”)....我在测试时正在使用它。 This code works well, but the only thing holding me back is the recognition of the date. 这段代码很好用,但是唯一让我退缩的是日期的识别。 If it knew to look at the date as changing variable and then based on that specific date pasted the data from L11 on Sheet1 under 1 of the 30 or 31 columns in accordance to that date, I'd be done for now :) 如果它知道将日期视为更改变量,然后根据该特定日期将来自L11的数据按照该日期粘贴到Sheet1上30或31列中的1列下,那么我现在就可以完成:)

Any help or guidance is greatly appreciated. 任何帮助或指导,我们将不胜感激。 Thank you. 谢谢。

Dave 戴夫

Ok, Given what you have told me, here is a pretty basic method, for the dates, use Month day . 好的,根据您告诉我的内容,这是一个非常基本的方法,对于日期,请使用Month day For example L8 = April 3 . 例如L8 = April 3

Sheet2
April 1   April 2   April 3   April 4   April 5   ...
                    Data is
                    copied
                    here

I hope I understand you properly. 我希望我能正确理解你。 this will find the matching date in sheet2, copy the data over and delete the data in L8. 这将在sheet2中找到匹配的日期,将数据复制过来并删除L8中的数据。

Option Explicit

Dim x As Integer

Sub Button1_Click()

x = 1

'this will find the column that matches the date and stores that as the copy location.
While Sheets("Sheet2").Cells(1, x).Value <> Sheets("Sheet1").Range("L8")
    x = x + 1
Wend


'this portion copies the data to the designated coordinates found by the first portion and delete the information from L8 and L11.
Sheets("Sheet2").Cells(2, x).Value = Sheets("Sheet1").Range("L11").Value
Sheets("Sheet1").Range("L8").Value = ""


End Sub

Because L11 has a formula in it, you don't want to delete the cell. 由于L11中包含公式,因此您不想删除该单元格。 Change the formula in it to be this: =IF(L8 <> "", VLOOKUP(K8,A28:B58,2,FALSE), "") 更改公式为: =IF(L8 <> "", VLOOKUP(K8,A28:B58,2,FALSE), "")

This formula will make L11 appear blank as long as L8 has no data in it. 只要L8中没有数据,此公式将使L11显得空白。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用VBA在Excel中逐个单元格地将信息从一个工作表剪切并粘贴到另一个工作表 - How to cut and paste information cell by cell from one worksheet to another in Excel using VBA Excel 使用 VBA 将单元格数据从一张纸复制到另一张纸 - Excel Copy Cell Data from one sheet to another using VBA 使用粘贴在某些条件下将单元格值从一张纸复制到另一张纸 - Copy Cell values from One Sheet to Another on certain criteria with the paste Excel VBA 从一个工作表中复制范围并将其一次一个单元格地粘贴到另一张工作表中 - Excel VBA copy range from one sheet and paste it one cell at a time in another sheet 在 Excel 中找到一个特定的日期(使用 VBA)并粘贴到它旁边的单元格中 - Find a specific date in Excel (using VBA) and paste into the cell next to it Excel VBA - 如何在一个单元格中识别年份(日期),然后检查另一个单元格中的数据,然后计算 - Excel VBA - how to identify the year (date) in one cell then check another cell for the data there and then count 使用vba将单元格从一个工作簿复制到excel中的另一个工作簿 - Copying the cell from One workbook to another workbook in excel using vba Excel VBA尝试将单元格值设置为来自另一个单元格的日期 - Excel VBA Attempting to set cell value as date from another cell Excel VBA:从另一个工作簿循环中复制和粘贴特定单元格 - Excel VBA: Copy and Paste Specific Cell from Another Workbook Loop Excel 2007-VBA将粘贴值从一个单元格剪切到另一个下方 - Excel 2007 - VBA Cut paste value from one cell to another below each other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM