复制,计数和排序特定列的所有单词并将其移动到其他工作表

时间:2015-11-17 12:29:31

标签: excel excel-vba vba

我想要实现的是复制工作表"数据"的独特单词(重复几次)。列A(忽略标题)到表"国家"列A然后在此工作表中添加第二列,并计算找到的每个单词的出现次数。同时将列表从较高到较小排序。请参阅下面的打印作为示例。

表"数据":

enter image description here

Sheet" Country"和我想要完成的输出:

enter image description here

到目前为止,我没有工作(givin'错误):

Sub Count_Sort()
Dim lastRow As Integer
Dim ws As String
Dim c As Range

ws = ActiveSheet.Name
lastRow = LastUsedRow
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$A$" & lastRow).RemoveDuplicates Columns:=1, Header:=xlNo
ActiveSheet.Name = "Country"
Sheets(ws).Activate
Set c = Range("A1")
Set d = Sheets("Country").Range("A1")
Do While Not IsEmpty(c)
    Do While Not IsEmpty(d)

        If c.Value = d.Value Then

            d.Offset(0, 1).Value = d.Offset(0, 1).Value + 1
            Set d = d.Offset(1, 0)
            Exit Do
        End If
        Set d = d.Offset(1, 0)
    Loop
    Set c = c.Offset(1, 0)
    Set d = Sheets("Country").Range("A1")
Loop
End Sub


Public Function LastUsedRow()
LastUsedRow = [A65536].End(xlUp).Row
End Function

欢迎任何帮助......

聚苯乙烯。我打算对表格的所有栏目做同样的事情"数据" (大约20),复制到另一张纸,然后计算和排序每个单词。但如果我在一个人中设法做到这一点,我想我会接触到其他人。再次感谢。

2 个答案:

答案 0 :(得分:0)

使用excel的内置函数和技术,在没有任何VBA的情况下很容易做到。但是,由于您似乎有很多工作要做,我建议使用VBA来利用Excel现有的工具来帮助您更快地完成工作(并且代码更少):

此外,最佳做法是尽量避免使用.Select.Active语句。

Sub Count_Sort()

Dim lastRow As Integer
Dim ws As Worksheet, wsA As Worksheet

Set ws = Sheets("Data") 'ActiveSheet.Name ... better to use actual sheet name
Set wsA = Sheets.Add(After:=Sheets(Sheets.Count))

With ws

    lastRow = LastUsedRow
    .Range("B2:A" & lastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsA.Range("A1"), Unique:=True

End With

With wsA

    .Name = "Country"

    With .Range("B2")
        .Formula = "=Countif(" & ws.Name & "!A:A,A2)"
        .AutoFill wsA.Range("A1").End(xlDown).Offset(, 1)
    End With

End With

End Sub

Public Function LastUsedRow()
LastUsedRow = [A65536].End(xlUp).Row
End Function

答案 1 :(得分:0)

保持代码的一般结构:

Sub Count_Sort()

Dim i As Integer
Dim ws As Worksheet, cs As Worksheet

Set ws = Sheets("Data")
ws.Select

ws.Range("A2", ws.Range("A2").End(xlDown)).Select     'Update for different data column
Selection.Copy

Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Country"               'Update for different data column
Set cs = Sheets("Country")                 'Update for different data column

cs.Range("A2").Select
cs.Paste
Application.CutCopyMode = False
cs.Range("A2", cs.Range("A2").End(xlDown)).RemoveDuplicates Columns:=1, Header:=xlNo
cs.Range("A1") = ws.Range("A1").Value          'Update for different data column (only ws.Range("A1").Value) (this is just the column heading)
cs.Range("B1") = "X times"

For i = 1 To cs.Range("A2", cs.Range("A2").End(xlDown).End(xlUp)).Rows.Count         
    cs.Cells(1 + i, 2) = Application.CountIf(ws.Range("A2", ws.Range("A2").End(xlDown)), cs.Cells(1 + i, 1))          'Update for different data column
Next i

cs.Range(cs.Cells(2, 1), cs.Cells(cs.Range("A2").End(xlDown).Row, 2)).Sort Key1:=cs.Range("B1"), order1:=xlDescending, Header:=xlNo

End Sub

然后,您只需更改不同列和/或工作表的引用。

此外,您应该考虑添加一些错误处理或检查,以确保在您添加的工作表存在时代码不会崩溃。