我试图通过多种方式解决这个问题并阅读了很多不同的帖子,但我仍然感到困惑。我的数据仅在Sheet1上的A列作为几百个条目的列表,但是不均匀,因此有时投诉是几行或没有解决方案(甚至没有抱怨。)但是,每组信息都以单词开头“新条目”。因此,我希望Sheet2从B列开始,每次遇到New Entry时都会创建一个新列,其中包含下面的所有数据,直到遇到下一个“New Entry”。如果你能提供帮助,请多谢一百万次。
目前数据:
+----+--------------------+
| | A |
+----+--------------------+
| 1 | New Entry |
| 2 | Smith, Joe |
| 3 | 15362 |
| 4 | 123-456-7890 |
| 5 | Company1 |
| 6 | Complaint |
| 7 | Resolution |
+----+--------------------+
| 8 | New Entry |
| 9 | Doe, Joe |
| 10 | 15361 |
| 11 | 234-567-8901 |
| 12 | Company2 |
+----+--------------------+
| 13 | New Entry |
| 14 | Mary, Joe |
| 15 | 15360 |
| 16 | 123-097-8641 |
| 17 | Company3 |
| 18 | Complaint |
| 19 | 2nd line complaint |
| 20 | Resolution |
+----+--------------------+
| 21 | New Entry |
| 22 | Doe, Jane |
| 23 | 15366 |
| 24 | 234-567-8910 |
| 25 | Company4 |
| 26 | Complaint |
| 27 | Resolution |
+----+--------------------+
要:
+---+--------------+--------------+--------------------+--------------+
| | A | B | C | D |
+---+--------------+--------------+--------------------+--------------+
| 1 | New Entry | New Entry | New Entry | New Entry |
| 2 | Smith, Joe | Doe, Joe | Mary, Joe | Doe, Jane |
| 3 | 15362 | 15361 | 15360 | 15366 |
| 4 | 123-456-7890 | 234-567-8901 | 123-097-8641 | 234-567-8910 |
| 5 | Company1 | Company2 | Company3 | Company4 |
| 6 | Complaint | | Complaint | Complaint |
| 7 | Resolution | | 2nd line complaint | Resolution |
| 8 | | | Resolution | |
+---+--------------+--------------+--------------------+--------------+
代码尝试:(我的尝试首先是行,所以我做了以下代码,由于数据不均匀导致问题。我认为如果使用New Entry来启动新列,列可能会更好。)
Private Sub CommandButton4_Click()
Dim i As Range
Dim actualRange As Range
Dim tmpString As String
Dim dob As Range
Dim idn As Range
Dim comp As Range
Dim dt As Range
Dim rsn As Range
Dim rsn2 As Range
Dim rsn3 As Range
For Each i In Sheet1.Range("A1:A21303")
i.Replace "Name ", "Name :"
tmpString = i.Value
If InStr(i.Value, "Name :") > 0 Then
i.Offset(0, 0).Value = Split(tmpString, ":")(0)
i.Offset(1, 0).Value = Split(tmpString, ":")(1)
Sheet2.Range("A" & Sheet2.Rows.Count).End(xlUp).Offset(1, 0).EntireRow.Value = i.Offset(1, 0).EntireRow.Value
ElseIf i.Value = "Name" Then
Sheet2.Range("A" & Sheet2.Rows.Count).End(xlUp).Offset(1, 0).EntireRow.Value = i.Offset(1, 0).EntireRow.Value
End If
Next i
For Each dob In Sheet1.Range("A1:A21303")
If dob.Value = "DOB" Then
Sheet2.Range("B" & Sheet2.Rows.Count).End(xlUp).Offset(1, 0).Value = dob.Offset(1, 0).EntireRow.Value
End If
Next dob
For Each idn In Sheet1.Range("A1:A21303")
If idn.Value = "ID Number" Then
Sheet2.Range("C" & Sheet2.Rows.Count).End(xlUp).Offset(1, 0).Value = idn.Offset(1, 0).EntireRow.Value
End If
Next idn
For Each comp In Sheet1.Range("A1:A21303")
If comp.Value = "Company" Then
Sheet2.Range("D" & Sheet2.Rows.Count).End(xlUp).Offset(1, 0).Value = comp.Offset(1, 0).EntireRow.Value
End If
Next comp
For Each dt In Sheet1.Range("A1:A21303")
If dt.Value = "Date/Time" Then
Sheet2.Range("E" & Sheet2.Rows.Count).End(xlUp).Offset(1, 0).Value = dt.Offset(1, 0).EntireRow.Value
End If
Next dt
For Each rsn In Sheet1.Range("A1:A21303")
If rsn.Value = "Complaint" Then
Sheet2.Range("F" & Sheet2.Rows.Count).End(xlUp).Offset(1, 0).Value = rsn.Offset(1, 0).EntireRow.Value
End If
Next rsn
For Each rsn2 In Sheet1.Range("A1:A21303")
If rsn2.Value = "Complaint" Then
Sheet2.Range("G" & Sheet2.Rows.Count).End(xlUp).Offset(1, 0).Value = rsn2.Offset(2, 0).EntireRow.Value
End If
Next rsn2
For Each rsn3 In Sheet1.Range("A1:A21303")
If rsn3.Value = "Complaint" Then
Sheet2.Range("H" & Sheet2.Rows.Count).End(xlUp).Offset(1, 0).Value = rsn3.Offset(3, 0).EntireRow.Value
End If
Next rsn3
End Sub
答案 0 :(得分:0)
试试这个:
Sub DoIt()
Dim nur As Long: nur = 1
Dim r As Long
Dim c As Long: c = 1
Dim lROW As Long
With Sheet1
lROW = .Cells(.Rows.Count, 1).End(xlUp).Row
For r = 1 To lROW
If .Cells(r, 1).Value2 = "New entry" Then
c = c + 1
nur = 1
End If
Sheet2.Cells(nur, c).Value2 = .Cells(r, 1).Value2
nur = nur + 1
Next r
End With
End Sub
答案 1 :(得分:0)
透视是关键。 你想要达到的目的是分割和转置一个范围。
此解决方案按新工作表中的列发布每个投诉,还可以选择将投诉作为列表(按行)发布。
它结合了PRAGMA main.page_size = 4096;
PRAGMA main.cache_size=10000;
PRAGMA main.locking_mode=EXCLUSIVE;
PRAGMA main.synchronous=NORMAL;
PRAGMA main.journal_mode=WAL;
PRAGMA main.cache_size=5000;
对象的AutoFilter
和SpecialCells
方法,以便为每个投诉设置Range
一个Range
。
步骤:
Area
答案 2 :(得分:0)
我会这样做。
Sub Basic_Example_1()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1
'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
With mybook.Worksheets(1)
Set sourceRange = .Range("A1:C1")
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
'Copy the file name in column A
With sourceRange
BaseWks.cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(Fnum)
End With
'Set the destrange
Set destrange = BaseWks.Range("B" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Next Fnum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
另外,请考虑使用下面列出的AddIn,特别是如果您需要经常这样做。