简体   繁体   中英

runtime error 1004 excel-vba

The error line is:

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

Code:

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

I didn't include full code on the file cause I think it's not necessary.

First-up you should do:

Dim role8Loc As Long

Becuase at the moment it is a Variant - you need to do Dim x As y for every variabled and not do Dim x, y, z As Long because otherwise x and y will be Variant .

Then, in this loop there is a chance that no cell has the value of Role (8) and therefore role8Loc will be 0 (or empty if you left as a Variant ).

'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

If role8Loc is 0 then you can't use it as a value for the Cells collection because there is no 0 column. So before the next loop, do an If check:

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

Then when you do this loop:

'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

Then i should be at least 1 - and you should not get an error on this line:

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

So your final code will be:

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

You could avoid loops by using Find() function to look a given range (some headers range I guess) for a cell with a given value and get back found cell Range object (if found) or Nothing (if not found)

Finally check for possible typos: your code has a If Sheets(1).Cells(6, i).Value = "Role (8)" Then line but its introducing comment says 'Get Role(8) Location -> the two strings differ for a space

For all what above the following code could help you in getting started again (I just commented out your code lines not strictly necessary for what I'm showing you)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM