0
我已經看到很多這個問題的答案,但它們似乎不能正常工作。我有這樣的數據集,表1:將行復制到基於值的新工作表中,乘以值
Animal 1 | Cat | 5 | Male
Animal 2 | Mouse| 3 | Female
Animal 3 | Dog | 1 | Male
而且我想通過按表1的命令按鈕讓這個在第2頁:
Animal 1 | Cat | 5 | Male
Animal 1 | Cat | 5 | Male
Animal 1 | Cat | 5 | Male
Animal 1 | Cat | 5 | Male
Animal 1 | Cat | 5 | Male
Animal 2 | Mouse| 3 | Female
Animal 2 | Mouse| 3 | Female
Animal 2 | Mouse| 3 | Female
Animal 3 | Dog | 1 | Male
請記住,這是一個樣品,我數據集作爲40列和1500行,我要複製的值在C列
,我到目前爲止已經完成的代碼是這樣的工作:
Private Sub CommandButton1_Click()
Dim currentRow As Long
Dim currentNewSheetRow As Long: currentNewSheetRow = 1
For currentRow = 1 To 1547 'The last row of your data
Dim timesToDuplicate As Integer
timesToDuplicate = CInt(Worksheets("Sheet1").Range("C" & currentRow).Value)
Dim i As Integer
For i = 1 To timesToDuplicate
Sheet2.Range("A" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("A" & currentRow).EntireRow.Value2
Sheet2.Range("B" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("B" & currentRow).EntireRow.Value2
Sheet2.Range("C" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("C" & currentRow).EntireRow.Value2
Sheet2.Range("D" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("D" & currentRow).EntireRow.Value2
Sheet2.Range("E" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("E" & currentRow).EntireRow.Value2
Sheet2.Range("F" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("F" & currentRow).EntireRow.Value2
Sheet2.Range("G" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("G" & currentRow).EntireRow.Value2
Sheet2.Range("H" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("H" & currentRow).EntireRow.Value2
Sheet2.Range("I" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("I" & currentRow).EntireRow.Value2
Sheet2.Range("J" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("J" & currentRow).EntireRow.Value2
Sheet2.Range("K" & currentNewSheetRow).EntireRow.Value2 = Sheet1.Range("K" & currentRow).EntireRow.Value2
'Continuous
currentNewSheetRow = currentNewSheetRow + 1
Next i
Next currentRow
End Sub
謝謝!
不明白你的問題是什麼? – Zac
我的問題是我的代碼有什麼問題?我只是想構建一個命令按鈕,將工作表1中的起始行復制到工作表2中,並且我希望複製發生的次數等於C列中該行的單元格值。 –
什麼是問題?提出的任何錯誤或代碼不符合您的預期? – avb