4

Below is the code which I am using to copy cells from one sheet and paste in to another.

Sheets("codes").Select
Range("A5:A100").Select
Selection.Copy
Sheets("Sheet2").Select
Range("B28").Select
ActiveSheet.Paste

The problem with this is some cells in this range are blank but I do not want them to be copied to Sheet2. I have got some idea from here but this method is too long. Is there a way I can iterate on the selection and check if the value is non-empty and paste. This way I can also paste some other text (eg #NA) in the blank cells.

4

3 に答える 3

12

あなたはここでいくつかの一般的な新人の間違いを犯しているようです(私たち全員がそれをしたのは大丈夫です)。


行ごとの説明を含むVBAの例

ヒント:「選択」または「コピー」は使用しないでください。セル自体を参照するだけでよいのに、なぜselectを使用するのですか?たとえば、を使用する代わりに

Sheets("codes").Select
Range("A5:A100").Select
Selection.Copy
Sheets("Sheet2").Select
Range("B28").Select
ActiveSheet.Paste

使用するだけ

dim mySheet as Worksheet, myOtherSheet as Worksheet, myBook as Workbook 'Define your workbooks and worksheets as variables
set myBook = Excel.ActiveWorkbook
set mySheet = myBook.Sheets("codes")
set myOtherSheet = myBook.Sheets("Sheet2")

dim i as integer, j as integer 'Define a couple integer variables for counting

j = 28 'This variable will keep track of which row we're on in Sheet2 (I'm assuming you want to start on line 28)
for i = 5 to 100 'This is the beginning the the loop which will repeat from 5 to 100 . . .
   if mySheet.Cells(i,1).value <> "" then ' . . . for each digit, it will check if the cell's value is blank. If it isn't then it will . . .
      myOtherSheet.Cells(j,2).value = mySheet.Cells(i,1).value ' . . . Copy that value into the cell on Sheet2 in the row specified by our "j" variable.
      j = j + 1 'Then we add one to the "j" variable so the next time it copies, we will be on the next available row in Sheet2.
   end if
next i 'This triggers the end of the loop and moves on to the next value of "i".

私が最初に始めたときはいつも同じことをしました、そしてそれは決して正しくうまくいきません。「選択」すると、左右にエラーが発生します。私のコードを使用して、コメントを読んでください。そうすれば大丈夫です。簡単な警告:このコンピューターにはExcelがないため、コードをテストできませんでした。なんらかの理由で機能しない場合は、コメントを残してください。明日は問題を修正します。

上記のコードは、データを2番目のシートにコピーするときに、空白のセルを完全に省略します。代わりに空白セルに特定のテキストを入力する場合(「N / A」など)、次を使用できます。

 dim mySheet as Worksheet, myOtherSheet as Worksheet, myBook as Workbook 'Define your workbooks and worksheets as variables
 set myBook = Excel.ActiveWorkbook
 set mySheet = myBook.Sheets("codes")
 set myOtherSheet = myBook.Sheets("Sheet2")

 dim i as integer, j as integer 'Define a couple integer variables for counting

 j = 28 'This variable will keep track of which row we're on in Sheet2 (I'm assuming you want to start on line 28)
 for i = 5 to 100 'This is the beginning the the loop which will repeat from 5 to 100 . . .
    if mySheet.Cells(i,1).value <> "" then ' . . . for each digit, it will check if the cell's value is blank. If it isn't then it will . . .
       myOtherSheet.Cells(j,2).value = mySheet.Cells(i,1).value ' . . . Copy that value into the cell on Sheet2 in the row specified by our "j" variable.
    else 'If the cell is blank, then . . .
       myOtherSheet.Cells(j,2).value = "N/A" ' . . . place the text "N/A" into the cell in row "j" in Sheet2.
    end if 'NOTICE we moved the "end if" statement up a line, so that it closes the "if" statement before the "j = j + 1" statement. _
      This is because now we want to add one to the "j" variable (i.e., move to the next available row in Sheet2) regardless of whether the cell in the "codes" sheet is blank or not.
       j = j + 1 'Then we add one to the "j" variable so the next time it copies, we will be on the next available row in Sheet2.
 next i 'This triggers the end of the loop and moves on to the next value of "i".
于 2013-01-24T07:49:20.053 に答える
3

簡単:

  Sheet1.Range("A1:a500").SpecialCells(xlCellTypeConstants).Copy Sheet2.Range("b2")

私は使用xlCellTypeConstantsしましたが、他にも多くの可能性があります。

Sheet1一般的にはと同等Sheets("Sheet1")です。1つ目はVBE(プログラマービュー)での名前で、2つ目はユーザーインターフェイス(ユーザービュー)での名前です。私は一般的に最初の構文を好みます。なぜなら、それは短く、コードに影響を与えることなく(ユーザーのために)シートの名前を変更できるからです。

于 2013-01-24T07:57:15.273 に答える
1

フォーマットが必要ない場合は、以下を使用します。ワークシートで指定した範囲を変数にコピーし、その変数をループして、空のセルをチェックし、好きな文字列を入力するだけです。それは素晴らしくて速いです。フォーマットを保持したい場合は、フォーマットだけを出力範囲に貼り付けることができます。

Sub CopyNonBlankCells(rFromRange As Range, rToCell As Range, sSubIn As String)
    'You have three inputs.  A range to copy from (rFromRange), a range to copy to (rToCell) and a string to put in the blank cells.        

    Dim vData As Variant, ii As Integer, jj As Integer

   'Set to a variable since it's quicker
    vData = rFromRange.Value

    'Loop through to find the blank cells
    For ii = LBound(vData, 1) To UBound(vData, 1)   'Loop the rows
        For jj = LBound(vData, 2) To UBound(vData, 2)    'Loop the columns
            'Check for empty cell.  Quicker to use Len function then check for empty string
            If VBA.Len(vData(ii, jj)) = 0 Then vData(ii, jj) = sSubIn
        Next jj
    Next ii

    'Output to target cell.  Use the 'With' statement because it makes the code easier to read and is more efficient
    With rToCell.Parent
        .Range(.Cells(rToCell.Row, rToCell.Column), .Cells(rToCell.Row + UBound(vData, 1) - 1, rToCell.Column + UBound(vData, 2) - 1)).Value = vData
    End With

End Sub

そしてそれを次のように呼びます:

Call CopyNonBlankCells(Sheets("codes").Range("A5:A100"), Sheets("Sheet2").Range("B28"), "Non-blank")
于 2013-01-24T07:56:19.520 に答える