2016-11-02 4 views
-3

実行時エラー '91':オブジェクト変数またはブロック変数が設定されていません。findメソッドが一致するものを見つけられないが、私はできません問題を解決する方法を見つけるように見える、私はそうでない場合のxxxは何もありません試してみました...しかし、私はその後、別のエラーを取得する誰かが私を助けることができる願って一致するエラーのないメソッドを見つける

Sub SetUpFormulas50450() 

Dim cnt1 As Long 
Dim i As Long 
Dim lnRow As Long 
Dim lnCol As Long 
Dim lnCol1 As Long 
Dim lnCol4 As Long 
Dim lnRow1 As Long 
Dim tempA As Long 
Dim tempB As Long 
Dim a As Long 
Dim LR As Long 

Set Sh1 = ThisWorkbook.Worksheets("Pivot") 
Sh1.Select 

lnRow = 5 
lnCol = 2 

cnt1 = Sh1.Cells(Rows.Count, "A").End(xlUp).Row 

'Sh1.Cells(1, 10) = cnt1 

lnCol1 = Sheet1.Cells(lnRow, 1).EntireRow.Find(What:="Grand Total", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column 
'Sh1.Cells(1, 11) = lnCol1 

Sh1.Cells(cnt1, (lnCol1 + 2)).Formula = "=SUM(" & Range(Cells(6, (lnCol1 + 2)), Cells((cnt1 - 1), (lnCol1 + 2))).Address(False, False) & ")" 

'Calculate Frt Pct of Sales 
For i = 6 To cnt1 
    tempA = Sh1.Cells(i, lnCol1).Value 
    tempB = Sh1.Cells(i, (lnCol1 + 2)).Value 
    If Sh1.Cells(i, (lnCol1 + 2)).Value = 0 Then Sh1.Cells(i, (lnCol1 + 3)).Value = 0 Else Sh1.Cells(i, (lnCol1 + 3)).Value = tempA/tempB 
Next i 

'Calculate Frt Pd in Fut Mos 
For a = 2 To (lnCol1 - 2) 
    For i = 6 To (cnt1 - 2) 
     lnCol4 = Sheet1.Cells(lnRow, 1).EntireRow.Find(What:=Sh1.Cells(i, 1).Value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column 
     Sh1.Cells(i, (lnCol1 + 4)).Formula = "=SUM(" & Range(Cells(i, (lnCol4 + 1)), Cells(i, (lnCol1 - 1))).Address(False, False) & ")" 
    Next i 
Next a 
End Sub 

私はラインで問題を抱えています。。。: lnCol4 = Sheet1.Cells(lnRow、1).EntireRow.Find(What:= Sh1.Cells(i、1).Value、LookIn:= xlValues、LookAt:= xlPart、SearchOrder:= xlByColumns、SearchDirection:= xlNext、MatchCase := False)。列

+1

'Find'を呼び出した後、' Nothing'をテストする必要があります。この正確な質問は真剣に毎日ここで尋ねられます。 – Comintern

答えて

0
Dim f As Range '<-- declare a range to collect the result of 'Find()' method 
For a = 2 To (lnCol1 - 2) 
    For i = 6 To (cnt1 - 2) 
     Set f = Sheet1.Cells(lnRow, 1).EntireRow.Find(What:=Sh1.Cells(i, 1).Value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) '<-- try setting f to the 'Find()' method result 
     If Not f Is Nothing Then '<--| check for f to be a "real" range 
      lnCol4 = f.Column 
      Sh1.Cells(i, (lnCol1 + 4)).Formula = "=SUM(" & Range(Cells(i, (lnCol4 + 1)), Cells(i, (lnCol1 - 1))).Address(False, False) & ")" 
     End If 
    Next i 
Next a 
+0

ありがとうございました!私は他のすべての質問から提案を試みていました。私の問題は、私がラインの最後に.columnを取っていないものをテストしていたときでした。一度それをしてから、If文が魅力的に機能するように設定しました! –

+0

ようこそ。あなたは私の答えを受け入れたものとしてマークしたいかもしれません。ありがとうございました – user3598756

関連する問題