2012-09-17 48 views
0

運行時出現1004錯誤。該錯誤是在這一行:我的宏顯示運行時錯誤1004.請檢查

如果則IsNumeric(wkbCurr.Sheets(CTRYname).Range(列& X).value的)= true,那麼

我想要做的是選擇表( CTRYNAME),然後搜索列5,7,9等,並按照代碼格式化數字。

Public Sub MoM_Check() 
Dim inti As Integer 
Dim intj As Integer 
Dim k As Integer 
Dim mnth As Integer 
Dim currSALE As Double 
Dim prevSALE As Double 
Dim diffpercent As Double 
Dim CTRYname As String 
Dim x As Integer 
Dim column As String 

    'Find Difference percentage between sales of 24 common months in present month's extarct and previous month's extract 

    For n = 1 To 13 
    Application.SheetsInNewWorkbook = 4 
Set wkbTemp = Workbooks.Add 

CTRYname = ThisWorkbook.Sheets("Country lookup").Range("A1").Offset(n, 0).Value 

'Open a temporary workbook to do all the Calculations 
'First the current month's extract is copied to the first sheet 

'We now copy sheets for range from wkbout to wkbtemp using usedrange 
wkbCurr.Sheets(CTRYname).Activate 
wkbCurr.Sheets(CTRYname).UsedRange.Copy 
wkbTemp.Sheets("Sheet1").Range("A1").PasteSpecial 
wkbprev.Sheets(CTRYname).Activate 
wkbprev.Sheets(CTRYname).UsedRange.Copy 

wkbTemp.Sheets("Sheet2").Range("A1").PasteSpecial 

'open the Previous month's Main Extract file as given in the lookup tab. This data is pasted on sheet2 of temporary workbook. 
'This sheet helps us to compare the country channels in current month's extract with the previous Month's Extract. 
'So the same process is followed for this sheet and similarly we get the country channels from the previous month's extract and paste them on 'sheet3 

'Prevcnt contains the number of country channels in the previous month's extract 
      k = 1 

    For mnth = 0 To 22 

    currSALE = wkbTemp.Sheets("Sheet1").Range("AB10").Offset(0, mnth).Value 
    prevSALE = wkbTemp.Sheets("Sheet2").Range("AC10").Offset(0, mnth).Value 
    If prevSALE = 0 And currSALE <> 0 Then 
    diffpercent = 1 
    ElseIf prevSALE = 0 And currSALE = 0 Then 
    diffpercent = 0 
    Else: diffpercent = (currSALE - prevSALE)/prevSALE 
    End If 

    If diffpercent > 0.01 Or diffpercent < -0.01 Then 
    Set wkbRaw = Workbooks.Open(strOutputQCPath & "Errorlog.xlsx") 
    wkbRaw.Sheets("Sheet1").Activate 

    wkbRaw.Sheets("Sheet1").Range("A1").Offset(i, 1 + n).Value = CTRYname & " Incorrect" 

    Exit For 
    Else 
    Set wkbRaw = Workbooks.Open(strOutputQCPath & "Errorlog.xlsx") 
    wkbRaw.Sheets("Sheet1").Activate 
    wkbRaw.Sheets("Sheet1").Range("A1").Offset(i, 1 + n).Value = CTRYname & " Correct" 

    k = k + 1 
    wkbRaw.SaveAs Filename:=strOutputQCPath & "Errorlog.xlsx" 
    wkbRaw.Close 

     End If 
     Next mnth 

    For x = 1 To 15 

    If x = 1 Or x = 2 Or x = 3 Or x = 4 Or x = 6 Or x = 9 Or x = 10 Or x = 11 Or x = 13  Then 
    GoTo Name 
    Else 
    If IsNumeric(wkbCurr.Sheets(CTRYname).Range(column & x).Value) = True Then 
     If wkbCurr.Sheets(CTRYname).Range(column & x).Value > 9.99 Then 
      wkbCurr.Sheets(CTRYname).Range(column & x).Value = ">999%" 
     ElseIf wkbCurr.Sheets(CTRYname).Range(column & x).Value < -9.99 Then 
      wkbCurr.Sheets(CTRYname).Range(column & x).Value = "<-999%" 
     End If 
    End If 
    End If 
Name: 
    Next x 

    wkbTemp.Close savechanges:=False 
    Set wkbTemp = Nothing 

    Next n 
End Sub   

請幫忙!

回答

1

你還沒有給出字符串「column」的值,這就是爲什麼你在該行上得到錯誤1004的原因。

+0

因此,如果我想選擇第1列,它不會作爲範圍(第1列)工作? –

+1

'列'需要被賦予一個特定的值。它從一個空字符串開始'''' - 這意味着對於x = 1,您的'.Range(column&x)'最終爲'.Range(「1」)',這是無效的。如果你想查看B列,然後'.Range(column&x)'將會是'.Range(「B1」),那麼你需要有一行:'column =「B」'' – barrowc