【发布时间】:2015-05-29 19:40:59
【问题描述】:
我有一个工作数据的 Excel 电子表格,需要在 VBA 中拆分。几列有多行文本,而其他列则没有。我已经想出了如何拆分多行文本,我的问题是用单行文本获取列并将其复制下来。例如:
Company_Name Drug_1 Phase_2 USA
Drug_2 Discontinued
Drug_3 Phase_1 Europe
Drug_4 Discontinued
下面是我用来拆分列 B 和 C 的代码,然后我可以手动处理 D,但是我需要将列 A 复制到第 2-4 行。有超过 600 行这样的行,否则我只会手动执行。 (注意:我将 B 列放入下面的 A 中,将 C 列放入 C 中)
Sub Splitter()
Dim iPtr1 As Integer
Dim iPtr2 As Integer
Dim iBreak As Integer
Dim myVar As Integer
Dim strTemp As String
Dim iRow As Integer
'column A loop
iRow = 0
For iPtr1 = 1 To Cells(Rows.Count, 1).End(xlUp).Row
strTemp = Cells(iPtr1, 1)
iBreak = InStr(strTemp, vbLf)
Range("C1").Value = iBreak
Do Until iBreak = 0
If Len(Trim(Left(strTemp, iBreak - 1))) > 0 Then
iRow = iRow + 1
Cells(iRow, 2) = Left(strTemp, iBreak - 1)
End If
strTemp = Mid(strTemp, iBreak + 1)
iBreak = InStr(strTemp, vbLf)
Loop
If Len(Trim(strTemp)) > 0 Then
iRow = iRow + 1
Cells(iRow, 2) = strTemp
End If
Next iPtr1
'column C loop
iRow = 0
For iPtr2 = 1 To Cells(Rows.Count, 3).End(xlUp).Row
strTemp = Cells(iPtr2, 3)
iBreak = InStr(strTemp, vbLf)
Do Until iBreak = 0
If Len(Trim(Left(strTemp, iBreak - 1))) > 0 Then
iRow = iRow + 1
Cells(iRow, 4) = Left(strTemp, iBreak - 1)
End If
strTemp = Mid(strTemp, iBreak + 1)
iBreak = InStr(strTemp, vbLf)
Loop
If Len(Trim(strTemp)) > 0 Then
iRow = iRow + 1
Cells(iRow, 4) = strTemp
End If
Next iPtr2
End Sub
【问题讨论】:
-
好吧,我显然搞砸了这篇文章。抱歉,我回家后会解决的
-
请澄清您的问题并突出显示有问题的部分(您只是在问如何在 Excel VBA 中复制单元格范围或其他?)。问候,
-
从您的示例中不清楚单元格边界在哪里。