简体   繁体   English

Excel 2013宏表到列(用于文本分析)

[英]Excel 2013 Macro- Table to Columns (for Text Analysis)

I am trying to move some data around to make it easier to do some basic text mining. 我正在尝试移动一些数据,以便更轻松地进行一些基本的文本挖掘。 I have a table with a row for each sentence, with the first column as the identifier and the following "N" columns with the words. 我有一个表格,每个句子都有一行,第一列为标识符,接下来的“ N”列为单词。 Example: 例:

Record  Word1   Word2   Word3   Word N
1       The     quick   brown   fox
2       jumps   over    the 
3       lazy    white       
4       dog         

I need to move the data from that table format to a list, with a word per row, with the record in which that word is located. 我需要将数据从该表格式移动到列表,每行一个单词,并带有该单词所在的记录。

Example: 例:

Record  Word
1       the
1       quick
1       brown
1       fox
2       jumps
2       over
2       the
3       lazy
3       white
4       dog

I have found macros to put the entire table in one column, but not in the way that I would need to identify in which record that word appears in. ( Excel Macros: From Table to Column ) 我已经找到了将整个表格放在一列中的宏,但并不是以一种需要识别该单词出现在哪条记录中的方式。( Excel宏:从表到列

I also found the following code here: http://community.spiceworks.com/scripts/show/1169-excel-table-to-single-column 我还在这里找到了以下代码: http : //community.spiceworks.com/scripts/show/1169-excel-table-to-single-column

Option Explicit

Public Sub DoCopies()
Dim lRowIdx As Long
Dim lColIdx As Long
Dim lRowStart As Long
Dim lRowOut As Long

Dim s1 As Worksheet
Dim s2 As Worksheet

Dim oBook As Workbook

Dim r As Range
Dim lRows As Long
Dim lCols As Long

  On Error GoTo errorExit

  Application.DisplayAlerts = False
  Set oBook = ThisWorkbook
  Set s1 = Worksheets(1)

  ' remove other tabs
  While (oBook.Sheets.Count > 1)
    oBook.Sheets(oBook.Sheets.Count).Delete
  Wend

  ' create the new tab
  Set s2 = oBook.Worksheets.Add(After:=oBook.Worksheets(oBook.Worksheets.Count))
  s2.Name = "Result"

  Set r = s1.UsedRange
  lCols = r.Columns.Count
  lRows = r.Rows.Count

  'skip header
  lRowStart = 1
  While (Trim$(s1.Cells(lRowStart, 1) = ""))
    lRowStart = lRowStart + 1
  Wend

  lRowStart = lRowStart + 1

  ' Take each row, put on tab 2
  For lRowIdx = lRowStart To lRows

    If (Trim$(s1.Cells(lRowIdx, 1)) <> "") Then

      For lColIdx = 1 To lCols
        lRowOut = lRowOut + 1
        s2.Cells(lRowOut, 1) = s1.Cells(lRowIdx, lColIdx)
      Next lColIdx

    End If

  Next lRowIdx

  s2.Activate

  Application.DisplayAlerts = True
  Exit Sub

errorExit:
  Application.DisplayAlerts = True
  Call MsgBox(CStr(Err.Number) & ": " & Err.Description, vbCritical Or vbOKOnly, "Unexpected Error")

End Sub

But that macro returns the data like this: 但是该宏返回的数据如下:

1
The
quick
brown
fox
2
jumps
over
the
<null>
3
lazy
white
<null>
<null>
4
dog
<null>
<null>
<null>

I've tried playing with the code, but can't figure it out. 我试过玩代码,但无法弄清楚。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

Microsoft has effectively written most of the code for you. Microsoft有效地为您编写了大多数代码。 All that is missing is to filter column Value to select (Blanks) and then delete those rows - and change the column labels, delete a column. 缺少的只是对“ Value列进行筛选以选择(Blanks) ,然后删除这些行-并更改列标签,删除列。 Details here . 详细信息在这里

Thanks to pnuts for pointing me in the right direction. 多亏了pnuts为我指明了正确的方向。 Your link had a comment from Pankaj Jaju that provided the exact script that I needed: 您的链接中有Pankaj Jaju的评论,其中提供了我需要的确切脚本:

Sub NormaliseTable() 
' start with the cursor in the table 
  Dim rTab As Range, C As Range, rNext As Range 
  Set rTab = ActiveCell.CurrentRegion 
  If rTab.Rows.Count=1 Or rTab.Columns.Count = 1 Then 
    MsgBox "Not a well-formed table!" 
    Exit Sub 
  End If 
  Worksheets.Add  ' the sheet for the results 
  Range("A1:C1") = Array("Row","Column","Value") 
  Set rNext = Range("A2") 
  For Each C In rTab.Offset(1,1).Resize(rTab.Rows.Count-1, _ 
         rTab.Columns.Count-1).Cells 
    If Not IsEmpty(C.Value) Then 
      rNext.Value = rTab.Cells(C.Row-rTab.Row+1,1) 
      rNext.Offset(0,1).Value = rTab.Cells(1,C.Column-rTab.Column+1)     
      rNext.Offset(0,2).Value = C.Value 
      Set rNext = rNext.Offset(1,0) 
    End If 
  Next 
End Sub

Thanks again for your guidance! 再次感谢您的指导!

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

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