2013-02-26 54 views
1

我有幾個工作簿從一個主工作簿中獲取副本。我想要做的是當我將數據輸入到主工作簿中時,我想將其複製到另一個工作簿中,該工作簿基於從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)) 

但我不知道如何使它整行

+0

我會評價這種類型的Excel-VBA問題,因爲這裏最常見的問題類別。你只能說你有一個問題將數據複製到另一個表單。*,什麼是確切的問題? '1.'錯誤? '''''只是不確定代碼片段? – bonCodigo 2013-02-26 07:21:35

+0

@bonCodigo我已經更新了我所做的問題和編碼。但仍然會出錯。或者你仍然不清楚我的問題是什麼? – 2013-02-26 08:45:48

+0

你有什麼錯誤? – glh 2013-02-26 10:54:46

回答

1
Workbooks("Master.xlsm").Worksheets("Input").Range(cells(i,B).cells(i,K)).Value = _ 
    Workbooks(prod & ".xlsm").).Worksheets("Sheet1").Range(Cells(CountRow, B). Cells(CountRow, K)).Value 

此代碼將更新主簿,我懷疑你想複製這個。還有一個語法錯誤.).然後一些。

我想這是你所需要的:

Dim sht1 As Worksheet, sht2 As Worksheet 
Set sht1 = Workbooks(prod & ".xlsm").Worksheets("Sheet1") 
Set sht2 = Workbooks("Master.xlsm").Worksheets("Input") 

sht1.Range(sht1.Cells(CountRow, 2), sht1.Cells(CountRow, 11)).Value = _ 
    sht2.Range(sht2.Cells(i, 2), sht2.Cells(i, 11)).Value 

Imroved代碼:使用resize(<row>, <column>)

Workbooks(prod & ".xlsm").Worksheets("Sheet1").Cells(CountRow, 2).resize(, 11).Value = _ 
    Workbooks("Master.xlsm").Worksheets("Input").Cells(i, 2).resize(, 11).Value 

對於一些額外的信息,則Cells(<Row>, <Column>)只需要在整數對於<Row><Column>。因此列B表示爲2

+0

是的,我的錯誤。我試過你的答案,但仍然出現錯誤。 (「Input」)。Cells(Count1,2).Value = Workbooks(「Master.xlsm」)。Worksheets(「Input」)。Cells(i ,2)。值和是的,它可以從主工作簿複製到另一個工作簿,但僅適用於一個單元格。你不認爲這個錯誤是在Range(Cells(i,2),Cells(i,11)) – 2013-02-27 03:05:47

+0

wb打開了嗎? – glh 2013-02-27 06:18:11

+0

如果你刪除了'.value'? – glh 2013-02-27 06:33:02

相關問題