简体   繁体   中英

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

Somewhat new to VBA excel. 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.

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. 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. 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.

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:

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. 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 :)

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 . For example 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.

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. Change the formula in it to be this: =IF(L8 <> "", VLOOKUP(K8,A28:B58,2,FALSE), "")

This formula will make L11 appear blank as long as L8 has no data in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM