我有一份Excel電子表格數據,用於我需要在VBA中分離的工作。有幾列有多行文字,其他則沒有。我已經想出瞭如何分割多行文本,我的問題是使用單行文本並將其複製下來。例如:計算單元格中的文本行數
Company_Name Drug_1 Phase_2 USA
Drug_2 Discontinued
Drug_3 Phase_1 Europe
Drug_4 Discontinued
下面是我使用分裂乙& C柱的代碼,然後我可以處理ð手動,但是我需要塔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
好吧,我明顯搞砸了這篇文章。對不起,當我到家時我會修復它 – pheeper
請澄清你的問題並突出顯示有問題的部分(你只是問如何複製Excel VBA中的單元格範圍或其他?)。問候, –
從你的例子中不清楚細胞邊界在哪裏。 –