我有幾個工作簿從一個主工作簿中獲取副本。我想要做的是當我將數據輸入到主工作簿中時,我想將其複製到另一個工作簿中,該工作簿基於從Combobox1.Value獲得的產品類型。要更清楚,我想要複製數據的工作簿取決於ComboBox1.value。即如果ComboBox1.value等於「Penofix」,那麼我想將數據複製到工作簿「Penofix.xlsm」。我已經完成了關於如何根據某些條件將數據輸入到特定行的主輸入上的編碼,但是面臨將數據複製到其他工作簿的問題。如何將行和列複製到另一個工作簿上的另一個工作表
Private Sub CmdEnter_Click()
Dim CountRow As Long
Dim i As Long
Dim prod as string
Dim j As Long
Dim Ws As Worksheet
Dim Count1 as Long
'CountRow is number of row in master workbook
CountRow = Worksheets("Input").Range("B" & Rows.Count).End(xlUp).Row
'assign variable prod with combobox1 value
prod = ComboBox1.Value
'i=32 because my row start at 32
For i = 32 To countRow + 31
While ComboBox1.Value = Worksheets("Input").Cells(i, 2).Value
Rows(i).Select
Selection.Insert shift = xlDown
With Worksheets("Input")
'insert data into master workbook
.Range("B" & i) = ComboBox1.Text
.Range("C" & i) = TextBox1.Text
.Range("D" & i) = TextBox2.Text
.Range("E" & i) = TextBox3.Text
.Range("F" & i) = TextBox4.Text
.Range("G" & i) = TextBox5.Text
.Range("H" & i) = ComboBox2.Text
.Range("I" & i) = TextBox6.Text
.Range("J" & i) = TextBox7.Text
.Range("K" & i) = TextBox8.Text
End With
'activate other workbook to copy data,if prod = Penofix,the workbook will be "Penofix.xlsm"
workbooks(prod & ".xlsm").Activate
'count the number of row in workbooks(prod & ".xlsm").
' i specified cell (31,3) to calculate the number of row used
Count1 = Workbooks(prod & ".xlsm").Worksheets("Sheet1").Cells(31,3).Value
Count1 = Count1 + 31
'copy data into workbooks(prod & ".xlsm")
'THIS IS THE LINE WHICH ERROR
Workbooks(prod & ".xlsm").Worksheets("Input").Range(Cells(Count1, 2), Cells(Count1 , 11)).Value = Workbooks("Master.xlsm").Worksheets("Input").Range(Cells(i, 2), Cells(i, 11)).Value
If MsgBox("One record written to Input. Do you want to continue entering data?", vbYesNo)= vbYes Then
ComboBox1.Text = ""
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
ComboBox2.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
Else
Unload Me
End If
Exit Sub
Wend
Next
End Sub
我已經嘗試用這種
Workbooks(prod & ".xlsm").Worksheets("Input").Cells(Count1, 2).Value = Workbooks("Master.xlsm").Worksheets("Input").Cells(i, 2).Value
更換
Workbooks(prod & ".xlsm").Worksheets("Input").Range(Cells(Count1, 2), Cells(Count1 , 11)).Value = Workbooks("Master.xlsm").Worksheets("Input").Range(Cells(i, 2), Cells(i, 11)).Value
和是唯一隻爲一個燒毛細胞的工作,但它。所以,我認爲錯誤是語法:
Range(Cells(Count1,2), Cells(Count1,11))
但我不知道如何使它整行
我會評價這種類型的Excel-VBA問題,因爲這裏最常見的問題類別。你只能說你有一個問題將數據複製到另一個表單。*,什麼是確切的問題? '1.'錯誤? '''''只是不確定代碼片段? – bonCodigo 2013-02-26 07:21:35
@bonCodigo我已經更新了我所做的問題和編碼。但仍然會出錯。或者你仍然不清楚我的問題是什麼? – 2013-02-26 08:45:48
你有什麼錯誤? – glh 2013-02-26 10:54:46