あなたはここでいくつかの一般的な新人の間違いを犯しているようです(私たち全員がそれをしたのは大丈夫です)。
行ごとの説明を含む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".