1

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 

ええ、その仕事ですが、それはただ1つの単一のセルのためだけです。だから私はエラーが構文にあると思います:

  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 に答える