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").Range(Cells(Count1,  2), Cells(Count1 , 11)).Value = Workbooks("Master.xlsm").Worksheets("Input").Range(Cells(i, 2), Cells(i, 11)).Value

有了这个

  Workbooks(prod & ".xlsm").Worksheets("Input").Cells(Count1, 2).Value = Workbooks("Master.xlsm").Worksheets("Input").Cells(i, 2).Value 

是的,它的工作,但它只适用于一个单细胞。所以我认为错误在于语法:

  Range(Cells(Count1,2), Cells(Count1,11))

但我不知道如何让它复制整行

4

1 回答 1

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

改进的代码:使用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

于 2013-02-26T11:04:28.130 回答