0

私が現在抱えている問題は、1 つのワークシートから A1:A10 などのセルをコピーするサブルーチンを作成しようとしていることです。A1、A2、B1、B2、B3、B5、C1、C2、C4、C5などの特定のセルを含む別のシートに。問題は、ワークシート名と範囲値を読み取れない現在のサブです。ここにコードがあります

'User will be able to copy cell data from one to another.
Public Sub CopyCell(ByVal pv_worksheet_source As Worksheet, _
                    ByVal pv_range_source_cells As Range, _
                    ByVal pv_worksheet_destination As Worksheet, _
                    ByVal pv_range_destination_cells As Range, _
                    ByRef pr_str_error_message As String)

'first the cells are compared for the data that is in them.
If pv_range_source_cells.Cells.Count <> pv_range_destination_cells.Cells.Count Then
     pr_str_error_message = pr_str_error_message & "The cell count " & pv_range_source_cells.Cells.Count & " and " & _
        pv_range_destination_cells.Cells.Count & " do not match. The cells of Initial Optics Input and Output cannot be copied!"
    Exit Sub
End If


Dim source_cells As Range
Dim i As Integer
Dim ArrOut() As String
Dim str_source_worksheet_name As String
Dim str_destination_worksheet_name As String



ArrOut() = Split(pv_range_destination_cells.Address, ",")

i = 0


For Each source_cells In pv_worksheet_source
    pv_worksheet_destination.Range(ArrOut(i)).Value = source_cells.Value
    i = i + 1
Next
End Sub

ご覧のとおり、コードはコピー元とコピー先のシート名で読み取られ、それらのセル範囲がコピーされると想定されています。型の不一致があり、さらに VBA でデータをコピーする他の方法がわかりません。このサブルーチンは、ワークブック全体で他のシートに対して呼び出されるため、モジュール形式で保持する必要があります。ソース ワークシートで呼び出される例を次に示します。

Private Sub CopyButton_Click()
'User gets data copied over to specific cells
   Dim str_error_message As String
    Call CopyCell(Sheet1, _
                  "A1:A10", _
                  Sheet2, _
                  "B1,B2,B3,C1,C2,C3,C4,D1,D2,D3", _
                  pr_str_error_message:=str_error_message)


End Sub

これは、ボタンをクリックしたときにエラーが発生する場所です。私はこの問題に何日も取り組んでいるので、助けてください!私は本当にそれをアピールします!:)

4

1 に答える 1

2

ArrOut は、文字列ではなく配列である必要があります。

変化

    Dim ArrOut as String 

    Dim Arrout as Variant

次に、次のように配列を設定します。

   ArrOut = Split(pv_range_destination_cells, ",")

次に、「貼り付け」を呼び出します

   For Each source_cells In pv_worksheet_source
       for i = lbound(arrout) to ubound(arrout)
          pv_worksheet_destination.Range(ArrOut(i)).Value = source_cells.Value
       next i
   Next

編集:コピーボタンのクリックで呼び出しで pv_destination_cells を文字列として設定していますが、サブの範囲として宣言しています。例のように文字列として呼び出す場合は、元のサブで文字列として設定し、上記のコードに反映されているように、配列宣言から「.address」を除外する必要があります

于 2012-10-23T14:43:31.957 に答える