简体   繁体   English

根据一列中的信息自动将一行从一张纸复制到另一张纸,并按日期(以月为单位)排序

[英]copying a row from one sheet to another automatically based on information in one column and sorted by dated (into Months)

Right, I'm having an issue that I'm hoping one (or more) of you will be able to help me with. 是的,我遇到一个问题,希望你们中的一个(或多个)能够为我提供帮助。

For a week now, I've been trying to work out how I can automatically copy and update rows from one sheet in my Workbook to separate sheets based on the month of a date in the second column. 一周以来,我一直在尝试找出如何自动复制和更新工作簿中一个工作表中的行,以根据第二列中日期的月份来分离工作表的方法。

I've tried everything I can think of, VLOOKUP doesn't seem to do it and I know little about VBA to be able to work out how it may work. 我已经尝试了所有我能想到的一切,VLOOKUP似乎没有做到,而且我对VBA知之甚少,因此无法确定它可能如何工作。

I did find a solution that looked promising Using VBA, that split all the varying rows based on the different values in one of the columns (I created an extra column and formatted it to text and then put JAN 15, FEB 15 etc.) then created new tabs and inserted the data into those. 我确实找到了一种使用VBA看起来很有希望的解决方案,该解决方案可以根据其中一列中的不同值拆分所有不同的行(我创建了一个额外的列,并将其格式化为文本,然后放入JAN 15,FEB 15等),然后创建新的标签页并将数据插入其中。 Unfortunately, for some reason this ended up creating excess tabs and wouldn't update the breakdown sheets when I changed the Master sheet. 不幸的是,由于某种原因,这最终导致创建了多余的标签,并且当我更改母版表时不会更新细分表。

The code I found was: 我发现的代码是:

Sub parse_data()
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Integer
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
vcol = 1        
Set ws = Sheets("Sheet1")        
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = "A1:C1"            
titlerow = ws.Range(title).Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"
For i = 2 To lr
On Error Resume Next
If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
End If
Next
myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
For i = 2 To UBound(myarr)
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
Else
Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
End If
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
Sheets(myarr(i) & "").Columns.AutoFit
Next
ws.AutoFilterMode = False
ws.Activate
End Sub

Now, what I'd really like is for excel to search through the dates in column C, and depending on month, move them into the relevant sheet, but in a way that if I update the main sheet, the monthly sheets are updated automatically. 现在,我真正想要的是excel在C列中搜索日期,并根据月份将它们移到相关的工作表中,但是如果我更新主工作表,则每月工作表会自动更新。 I don't know if this is possible, but surely it must be (it probably isn't even difficult). 我不知道这是否可行,但肯定可以(这可能并不困难)。 If necessary, I'd be happy to put in a further column with "Jan 15", "Feb 15" etc. or have a button that I can press to update everything. 如有必要,我很乐意在另一栏中添加“ Jan 15”,“ Feb 15”等内容,或者有一个按钮可以按一下以更新所有内容。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Your code looks like a bit of an overkill, here I wrote a piece of code that would do the job if extended a bit, you need to add some cases, secure from errors in case the sheet is already there, and adjust the paste location but its a start(will also have more learning value for ya) :) 您的代码看起来有些过头了,在这里我写了一段代码,如果扩展了一点,它就可以完成工作,您需要添加一些情况,以防万一有工作表存在时避免错误,并调整粘贴位置但它是一个开始(还将对您有更多的学习价值):)

Sub haha()

Dim ws As Worksheet
Dim i As Integer
Dim lastrow

Set ws = ActiveSheet
lastrow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row

For i = 1 To lastrow

Select Case Format(ws.Range("c" & i).Value, "mm")

Case "01"

Sheets.Add.Name = "Jan"
ws.Range("C" & i).EntireRow.Copy Sheets("Jan").Range("A1")


End Select
Next i
End Sub

cheers 干杯

If it were going to be a code that you wanted to put to a button, I would do something like: 如果这将是您想要放在按钮上的代码,我将执行以下操作:

dim b2 as Workbook
Set b2=ThisWorkbook

xrowx=1
datecol='whatever column that you have the "Feb15" "Jan 15" data in

Do While xrowx<=Worksheetfunction.CountA(b2.Sheets(1).Range("A:A"))


month=Left(b2.Sheet(1).cells(xrowx,datecol))

if month="Jan" then
    emptyrow=Worksheetfunction.CountA(b2.sheets(2).Range("A:A")+1

    col=1

    Do While col<=datecol
    b2.sheets(2).cells(emptyrow,col)=b2.Sheets(1).Cells(emptyrow,col)
    col=col+1

    Loop

elseif month="Feb" then
    emptyrow=Worksheetfunction.CountA(b2.sheets(3).Range("A:A")+1

    col=1

    Do While col<=datecol
    b2.sheets(3).cells(emptyrow,col)=b2.Sheets(1).Cells(emptyrow,col)
    col=col+1

    Loop

elseif ...

...'continue on in this manner for all months

xrowx=xrowx+1

Loop

It's not super pretty or the most optimized way to get it done, but it is easy to understand and should give you a decent framework to build something that suites your needs well (Note: this code could also be used to auto-update as part of a built in sheet macro, but due to the fact that with very large data sets it will be a bit sluggish, that' not recommended) 它不是超级漂亮,也不是完成它的最优化方法,但是它易于理解,应该为您提供一个不错的框架来构建可以很好满足您需求的内容(注意:此代码也可以作为一部分自动更新内置工作表宏的功能,但是由于数据集非常大,它会有点迟钝,因此不建议使用)

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

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