2017-08-24 8 views
1

シートから毎日最も混雑した時間を記録し、別のものに追加する次のコードがあります。1枚のシートから別のシートにデータをコピーするExcel VBAコードのデータ複製を停止する

Sub DailySales() 
Dim dailySht As Worksheet 'worksheet storing latest   store activity 
Dim recordSht As Worksheet 'worksheet to store the highest period of each day 
Dim lColDaily As Integer ' Last column of data in the store activity sheet 
Dim lCol As Integer ' Last column of data in the record sheet 
Dim maxCustomerRng As Range ' Cell containing the highest number of customers 
Dim CheckForDups As Range ' Used to find duplicate dates on the record Sheet 
Dim maxCustomerCnt As Long ' value of highest customer count 

Set dailySht = ThisWorkbook.Sheets("Supermarket Data") 

Set recordSht = ThisWorkbook.Sheets("Record Data") 
With recordSht 
    lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 
End With 
With dailySht 
    lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column 
    maxCustomerCnt = Application.Max(.Range(.Cells(2, 1), .Cells(2, lColDaily))) 
    Set maxCustomerRng = .Range(.Cells(7, 1), .Cells(7, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues) 
    If Not maxCustomerRng Is Nothing Then 
     Set CheckForDups = .Range(.Cells(1, 1), .Cells(1, .Columns.Count).End(x1ToLeft).Column).Find(What:=maxCustomerRng.Offset(-1, 0).Value, LookIn:x1Values) 
     If CheckForDups Is Nothing Then maxCustomerRng.EntireColumn.Copy recordSht.Cells(1, lCol + 1) 
    End If 
End With 

Set maxCustomerRng = Nothing 
Set dailySht = Nothing 
Set recordSht = Nothing 

End Sub 

データの重複を防ぐために、1日に複数のデータを記録しないでください。

Set CheckForDups = .Range(.Cells(1, 1), .Cells(1, .Columns.Count).End(xlToLeft).Column).Find(What:=maxCustomerRng.Offset(-1, 0).Value, LookIn:=xlValues) 
      If CheckForDups Is Nothing Then maxCustomerRng.EntireColumn.Copy recordSht.Cells(1, lCol + 1) 

私はコードを実行すると、 "コンパイルエラー:構文エラー"が表示されます。以下の行が強調表示されています:

Set CheckForDups = .Range(.Cells(1, 1), .Cells(1, .Columns.Count).End(x1ToLeft).Column).Find(What:=maxCustomerRng.Offset(-1, 0).Value, LookIn:x1Values) 

これは、1枚目のテーブルです:

Customer data 7:00:00 AM 7:30:00 AM 8:00:00 AM 8:30:00 AM 9:00:00 AM 
Number of customers 33   37   110   250  84 
Amount spent  65   50   70   85  60 
Average time spent 12   10   8   17  10 

誰かが私が間違ってやっているものを私に教えてくださいことはできますか?

答えて

3

あなたはあなたのコード内のいくつかの誤植を持っている:x1LeftxlLeftする必要があり、x1ValuesxlValuesであるべきであり、名前付きパラメータは:=代わりの:を使用して指定する必要があります。二Rangeパラメータで

Set CheckForDups = .Range(.Cells(1, 1), .Cells(1, .Columns.Count).End(xlToLeft)).Find(What:=maxCustomerRng.Offset(-1, 0).Value, LookIn:=xlValues) 

はあなたがセルに合格することを目的と列の数を渡さ:

しかし、全体の文が正しくない、これはそれがあるべき姿です。したがって、.Columnプロパティを取得する必要はありません。セル自体は.Columnを削除して取得します。

+0

@ YowE3K - 申し訳ありません、この言語を習得したばかりです。私は提案された変更を行い、それは動作しますが、データの重複を防ぎません。また、Rangeパラメータを参照しているかどうかはわかりません。.Columnを削除するとコンパイルされません。 – aab

+0

行内に強調表示されています。 –

+0

それは私に 'メソッドまたはデータメンバーが見つかりません'を与えます。これは私がやったことです:Set CheckForDups = .Range(.Cells(1,1)、.Cells(1、.Count).End(xlToLeft))Find(What:= maxCustomerRng.Offset(-1、0)。値、LookIn:= xlValues) – aab

関連する問題