0
我有在Excel片,像創建多到一個表:練成 - 從一到多片
| Value1 | Data1 | Data1b | 1,3,4,8 |
| Value2 | Data2 | Data2b | 2 |
| Value3 | Data3 | Data3b | 6,7,8 |
我想採取這一頁,然後另一種將最後一列分成不同的行並保持其他數據同步。所以當第一張表被更新時,第二張也被更新。如果一個數字被添加到第一個工作表中的最後一列,則會在第二個工作表中添加一個新行。
第二片應該是這樣的:
| Value1 | Data1 | Data1b | 1 |
| Value2 | Data2 | Data2b | 2 |
| Value1 | Data1 | Data1b | 3 |
| Value1 | Data1 | Data1b | 4 |
| Value3 | Data3 | Data3b | 6 |
| Value3 | Data3 | Data3b | 7 |
| Value1 | Data1 | Data1b | 8 |
| Value3 | Data3 | Data3b | 8 |
UPDATE:下面是我嘗試使用的代碼。首先,我有最好的辦法嗎?並且正在清理然後重新填充更新第二張紙的正確方法?最後,如何在更新第一張紙時自動運行?
UPDATE:唯一還沒有工作的是最後的排序,有沒有人有任何想法爲什麼?
Private FROM_SHEET As String
Private TO_SHEET As String
Private START_ROW As Long
Private NUM_COL As Long
Sub oneToMany()
FROM_SHEET = "Sheet1"
TO_SHEET = "Sheet2"
START_ROW = 2
NUM_COL = 4
Dim fromSheet As Worksheet
Dim toSheet As Worksheet
Dim newRow As Long
Set fromSheet = Sheets(FROM_SHEET)
Set toSheet = Sheets(TO_SHEET)
toSheet.UsedRange.ClearContents
newRow = START_ROW
For i = START_ROW To fromSheet.Cells(fromSheet.Rows.Count, 1).End(xlUp).Row
Dim col As String
Dim nums() As String
col = fromSheet.Cells(i, NUM_COL)
nums = Split(col, ",")
For Each num In nums
fromSheet.Rows(i).Copy toSheet.Rows(newRow)
toSheet.Cells(newRow, NUM_COL) = Trim(num) 'Should copy then overwrite?
newRow = newRow + 1
Next num
Next
'Sort not working
toSheet.Range(toSheet.Cells(START_ROW, START_COL), toSheet.Cells(lastRow, lastCol)).Sort _
key1:=toSheet.Range(toSheet.Cells(START_ROW, NUM_COL), toSheet.Cells(lastRow, NUM_COL)), _
order1:=xlAscending, Header:=xlNo
End Sub
您將需要爲這個VBA,你嘗試過什麼? –
我對VBA很新,所以我甚至不知道從哪裏開始完成這樣的任務。 – user3499973
Google循環並分成vba。然後嘗試寫下一些東西,然後回頭嘗試並解釋發生了什麼問題。 –