2017-03-23 2 views
1

오류 라인은 다음과 같습니다런타임 오류 1004 엑셀 VBA를

If Sheets(1).Cells(6, i).Value = "AC POWER (LVL1)" Then 

코드 :

Function getParam(Parameter As String) 
    'For Testing Purposes 

    Dim paramList, columnVals 
    Dim lastRow, lastCol, currentRow, currentCol, lvl1, foundCol As Long 
    Dim role8Loc, acpowerLoc, paramLoc, role8for2 As Long 

    lastRow = Sheets(1).Range("A65536").End(xlUp).Row 
    lastCol = Sheets(1).Range("A6").CurrentRegion.columns.Count 
    currentRow = 4 'Sheets(2).Range("A65536").End(xlUp).Row + 1 
    currentCol = 60 

    paramList = Array("LESS100", "LNA200 COOL", _ 
         "LNA200 POWER", "MEGA100", _ 
         "MEGA1000", "MEGA200", _ 
         "MEGA500") 

    'Get Role(8) Location 
    For i = 1 To lastCol 
     If Sheets(1).Cells(6, i).Value = "Role (8)" Then 
      role8Loc = i 
     End If 
    Next i 

    'Get AC POWER (LVL1) Location 
    For i = role8Loc To lastCol 
     If Sheets(1).Cells(6, i).Value = "AC POWER (LVL1)" Then 
      acpowerLoc = i 
     End If 

나는 그것이 필요하지 생각 파일 원인에 대한 전체 코드를 포함하지 않았다. 당신이해야

+0

오류가 발생했을 때'role8Loc'의 값은 무엇입니까? –

+0

@Robin Mackenzie 비어 있습니다. – nubcoder17

+0

그게 문제입니다. 'role8Loc'은 Excel 버전에 따라 최대 열 수 1에서 최대 값 사이의 값이어야합니다. 이전 루프에서 'Role (8)'값을 가진 셀이 없었던 것 같습니다. 두 번째 루프를 실행하기 전에'role8Loc'을 검사하기 위해서는'If' 문이 필요합니다. HTH –

답변

0

먼저 업 : 순간

Dim role8Loc As Long 

Becuase IT는 Variant - 당신은 variabled 모든에 대한 Dim x As y을하고, 그렇지 않으면 xyVariant이 될 것이기 때문에 Dim x, y, z As Long을 할 필요가있다.

그런 다음이 루프 (당신이 Variant으로 떠나는 경우 또는 빈) 아무 셀 Role (8)의 가치가 없으며, 따라서 role8Loc가 0이 될 것이라는 점을 기회가있다.

'Get Role(8) Location 
For i = 1 To lastCol 
    If Sheets(1).Cells(6, i).Value = "Role (8)" Then 
     role8Loc = i 
    End If 
Next i 

role8Loc 경우에는 0 열이 없기 때문에 0 다음은 Cells 컬렉션에 대한 값으로 사용할 수 없다. 이 루프를 수행 할 때 다음

If IsEmpty(role8Loc) Or role8Loc < 1 Then 
    MsgBox "Error!" 
    Exit Sub 
End If 

: 그래서 다음 루프 전에, If 확인을

'Get AC POWER (LVL1) Location 
For i = role8Loc To lastCol 
    If Sheets(1).Cells(6, i).Value = "AC POWER (LVL1)" Then 
     acpowerLoc = i 
    End If 
Next i 

그런 다음 i 적어도 1이어야한다을 - 당신은이 라인에 오류가 발생하지 않아야 :

If Sheets(1).Cells(6, i).Value = "AC POWER (LVL1)" Then 

그래서 최종 코드는 다음과 같습니다

Function getParam(Parameter As String) 
    'For Testing Purposes 

    Dim paramList, columnVals 
    Dim lastRow, lastCol, currentRow, currentCol, lvl1, foundCol As Long 
    ' explicitly set these variables to Long 
    Dim role8Loc As Long, acpowerLoc As Long, paramLoc As Long, role8for2 As Long 

    lastRow = Sheets(1).Range("A65536").End(xlUp).Row 
    lastCol = Sheets(1).Range("A6").CurrentRegion.columns.Count 
    currentRow = 4 'Sheets(2).Range("A65536").End(xlUp).Row + 1 
    currentCol = 60 

    paramList = Array("LESS100", "LNA200 COOL", _ 
         "LNA200 POWER", "MEGA100", _ 
         "MEGA1000", "MEGA200", _ 
         "MEGA500") 

    'Get Role(8) Location 
    role8Loc = 0 '<~~ initialise variable to 0 
    For i = 1 To lastCol 
     If Sheets(1).Cells(6, i).Value = "Role (8)" Then 
      role8Loc = i 
     End If 
    Next i 

    ' validating role8Loc 
    If role8Loc < 1 Then '<~~ test if variable changed 
     MsgBox "Error!" 
     Exit Function '<~~ exit if variable has not changed 
    End If 

    'Get AC POWER (LVL1) Location 
    For i = role8Loc To lastCol 
     ' no error if i starts from a value >=1 
     If Sheets(1).Cells(6, i).Value = "AC POWER (LVL1)" Then 
      acpowerLoc = i 
     End If 
    Next i 
+0

여전히 동일한 오류가 발생합니다 :/ – nubcoder17

+0

전체 코드 블록으로 편집하십시오 - 약간의 변경을했습니다 –

+0

이제 오류가 발생합니다 : 함수 또는 속성에서 종료 하위가 허용되지 않습니다 :/ – nubcoder17

0

당신은 주어진 값으로 셀에 대한 지정된 범위를보고 (I 추측 일부 헤더 범위) Find() 기능을 사용하여 루프를 방지하고 (없는 경우) 돌아갈 세포에게 Range 개체를 발견 (발견 된 경우) 또는 Nothing 수 마지막으로

가능 오타를 확인 : 코드가 If Sheets(1).Cells(6, i).Value = "Role (8)" Then 라인을 가지고 있지만 도입 주석이 'Get Role(8) Location 말한다 -> 두 문자열이 (다시 시작에서 당신을 도울 수있는 것을 다음 코드 위의 모든 내용은 공간

다릅니다 나는 방금 내가 당신에게 보여줄 것에 대해 당신의 코드 라인을 엄격히 요구하지 않았다고 주석을 달았습니다.)

Function getParam(Parameter As String) '<--| where is 'Parameter' used? 

    Dim paramList ', columnVals 
    'Dim lastRow As Long, currentRow As Long, currentCol As Long, lvl1 As Long, foundCol As Long 
    Dim role8Loc As Long, acpowerLoc As Long ', paramLoc As Long, role8for2 As Long 
    Dim headers As Range, found As Range 

    With Sheets(1) '<--| reference relevant sheet 
'  lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 
     Set headers = .Range("A6", .Cells(6, .Columns.Count).End(xlToLeft)) '<--| set the range with headers to search through 
    End With 

' With Sheets(2) '<--| reference relevant sheet 
'  currentRow = 4 '.Cells(.Rows.Count, 1).End(xlUp).Row + 1 
'  currentCol = 60 '.Range("A6", .Cells(6, .Columns.Count).End(xlToLeft)) 
' End With 

' paramList = Array("LESS100", "LNA200 COOL", _ 
'      "LNA200 POWER", "MEGA100", _ 
'      "MEGA1000", "MEGA200", _ 
'      "MEGA500") '<--| where is this used? 


    Set found = headers.Find(what:="Role (8)", after:=headers(headers.Columns.Count), LookIn:=xlValues, lookat:=xlWhole) '<--| try and get "Role (8)" Location (check it should not be "Role(8)"!) 

    If found Is Nothing Then '<--| if not found 
     MsgBox "No matches for 'Role (8)' keyword in " & headers.Address(False, False) 
    Else '<--| if found 
     role8Loc = found.Column 
     Set found = headers.Resize(, headers.Columns.Count - role8Loc).Offset(, role8Loc).Find(what:="AC POWER (LVL1)", after:=headers(headers.Columns.Count), LookIn:=xlValues, lookat:=xlWhole) '<--| try and get "AC POWER (LVL1)" Location 
     If found Is Nothing Then '<--| if not found 
      MsgBox "No matches for 'AC POWER (LVL1)' keyword in " & headers.Address(False, False) 
     Else '<--| if found 
      acpowerLoc = found.Column 
     End If 
    End If 

    getParam = role8Loc & " - " & acpowerLoc '<--| just to have a return value to show in caller function 


End Function 
+0

안녕하세요! 다른 코드 블록에서 사용됩니다. lastRow = 시트 ("매개 변수") 범위 ("A65536"). 끝 (xlUp). 로우 – nubcoder17

+0

또는 전체 코드를 게시 하시겠습니까? – nubcoder17

+0

''Parameter '는 문자열''literal' (항상 같은 값을가집니다 :''Parameter "')이기 때문에 작동하지 않아야합니다.''Parameter'는 문자열 변수입니다.). 아마 당신은'lastRow = Sheets (Parameter) .Range ("A65536"). End (xlUp) .Row'로 사용하기를 원할 것입니다. 하지만 그것은 또 다른 이야기입니다 ... 귀하의 진부한 문제에 대한 제안 된 코드는 무엇입니까? – user3598756