2016-07-18 8 views
0

私は、日付の範囲に基づいてデータをコピー&ペーストしようとする、まったく新しいVBAユーザーです。 1列目には日付があり、2列目にはコピー&ペーストしたいデータがあります。 CurYearは、私が探している範囲の終了日を表し、StatDateは、探している範囲の開始日を表します。このコードを実行すると、Excelがクラッシュします。私は非常に(サブの前に)あなたのコードの先頭にVBAを使用する場合データをコピーして貼り付ける文

Worksheets("Weekly").Select 

Dim nRows As Integer 
Dim CurYear As Date 
Dim StartDate As Date 

nRows=Range("A1").CurrentRegions.Count.Rows 
CurYear=Range("I265").Value 
StartDate=Range("M5").Value 

Do While Cells(nRows,1)<>"" 

if Cells(nRows,1).Value< CurYear & Cells(nRows,1)> StartDate Then 

Cells(nRows,1).Offset(0,1).Copy 
Worksheets("Weekly").Range("H41").Paste 

Loop 
End If 
+1

私はちょうど細胞(NROWS、1).Offset(0,1).VALUE =ワークシート( "週刊")させます。レンジ( "H41")。値、また – Lowpar

+0

をコピー&ペーストする必要はありません、ループの前に終了する必要があります、私はcurrentregions.count.rowsでエラーが発生しています。 ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Address) – Lowpar

+0

デバッガでF8キーを押すと、どのセルがどのセルに表示されますか?クラッシュしていますか? – Lowpar

答えて

0

多くのメモリを使用してプログラムを遅くする代わりに、Selectを使用するよりも速く、コードまたはRodgerの目的と同じ目的を果たす次のコードを検討したいことがあります。コピー/ペーストの構文。

Sub Test() 
Dim nRows As Long, LastRow As Long 'Declare as Long instead of Integer to avoid overflow 
Dim CurYear As Date, StartDate As Date 

LastRow = Cells(Rows.Count, 1).End(xlUp).Row 'Count the last used row in column 1 where you put the first data (dates) 
nRows = 2 'Set the starting point of row where you put the first data (dates). In this example I use 2 
CurYear = Range("I265").Value 
StartDate = Range("M5").Value 

Do 
    If Cells(nRows, 1).Value < CurYear And Cells(nRows, 1) > StartDate Then 'Use And not & 
     Cells(nRows, 5).Value = Cells(nRows, 2).Value 'This is essentially a "copy/ paste" syntax. Change the value (5) to the column you want to paste the value in column 2 
    End If 
    nRows = nRows + 1 'Set an increment value so each looping the nRows will increase by 1 
Loop Until nRows = LastRow + 1 'Added by 1 so that the data in LastRow will keep being processed 

End Sub 
+0

これは素晴らしいです!ありがとうございました! – Reisenrich

1

PUT「明示的なオプション」を失っています助け、それはあなたに解決する事を教えてくれますしてください。そうすることで、エラーの一部が修正され、ループの内側ではなくループの外側にある場合でも、ループカウンタを変更していないということはありません。代わりにこのコードを試してください。それは実際にはあなたがいくつかの小さな変更を加えたものとほとんど同じです。

Option Explicit 
Sub test() 
Dim sht As Worksheet, i As Long, l As Long, j 

Dim nRows As Integer 
Dim CurYear As Date 
Dim StartDate As Date 

Set sht = Worksheets("Test1") ' set the sheet as object isntead of selecting it for faster code and avoiding other issues 

nRows = Cells(sht.Rows.Count, "B").End(xlUp).Row 'Last used row in column B - current region lastrow gets twitchy in some circumstances and should be avoided unless there is a reason to use it 
l = 41 

CurYear = range("I265").Value 
StartDate = range("M5").Value 

For i = 1 To nRows 
    If Cells(i, 1).Value < CurYear And Cells(i, 1).Value > StartDate Then 'for If statements you use "and" not "&" 
    Cells(l, 15) = Cells(i, 2) 'you will want something like this line and the next if you don't want to overwrite H41 if there is more than one match 
    l = l + 1 
    End If 
Next i 

End Sub 

また、デバッグに役立つように、ローカルウィンドウを開きます(VBEで表示)。 F8を使用してコードをステップ実行し、ローカルウィンドウで変数を監視して、スクリプトのそのステップで期待されるものであることを確認します。

コードでこれを行うと、ループの変数にカウンタの変更がないことがわかります。だから、最終的には "nRow"を探していましたが、それは何に設定されていたとしても残っています。無限ループ。私はそれを次のフォーマットのために変更しましたが、1つは6つ、もう1つはあなたのコードに変更しました。

VBAへようこそ。目を逸らさないでください。 :-)

+0

FYI - コードの他の部分に問題があり、F8を押してローカルのウィンドウを見て並べ替えることができない場合は、必ず新しい回答を探してください必要に応じて新しい質問を開始してください。 – Rodger

+0

Rodger、 私はまだそれに少し問題があります。私はもはや驚異的ではない、それは素晴らしいです!上のコードは値をコピーして貼り付けているわけではありません。私がコードを実行した後、 "H41"は空白のままです。私は潜在的に私の日付の間違いであるかもしれないと思ったが、すべての日付が正しく入力された。PS上記のコードで何も変更されていませんでしたが、それも参照したシートを除いて – Reisenrich

+0

私は上記のコードを編集してコメントを追加しました。試してみます。ああ、列(Column)Oの代わりに列(Column)Hを必要とするならば、セル(l、15)行は15から8に変更する必要があります。それがあなたの世話をするならば、上向きの矢印で「答えを受け入れる」ことを忘れないでください。 :) – Rodger

関連する問題