+ Reply to Thread
Results 1 to 8 of 8

Run Time Error in coding - Can someone help me debug please?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-10-2012
    Location
    Edinburgh
    MS-Off Ver
    Excel 2003
    Posts
    41

    Run Time Error in coding - Can someone help me debug please?

    I have attached an example file with a macro that strips data found in one column from matching data in another column.

    I get a run time error on the line:
    arrVariant = rngVariant.Cells(1, 1).Offset(1, 0).Resize(lngLastRow - 1, 1).Value

    Could someone help me fix the script please?

    Option Compare Text
    
    Public Sub StripVariants()
    
        Call StripVariant(Worksheets("Sheet1").Range("D:D"), Worksheets("Sheet1").Range("G:G"))
        Call StripVariant(Worksheets("Sheet1").Range("E:E"), Worksheets("Sheet1").Range("G:G"))
    
    End Sub
    
    Public Sub StripVariant(ByVal rngVariant As Excel.Range, ByVal rngText As Excel.Range)
    
        Dim arrVariant          As Variant
        Dim arrText             As Variant
    
        Dim lngLastRow          As Long
        Dim lngRow              As Long
        Dim intCol              As Integer
        Dim intPos              As Integer
    
        intCol = rngVariant.Column
        lngLastRow = rngVariant.Parent.Columns(intCol).Find(What:="*", After:=Cells(1, intCol), _
            SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
            LookAt:=xlPart, LookIn:=xlValues).Row
        arrVariant = rngVariant.Cells(1, 1).Offset(1, 0).Resize(lngLastRow - 1, 1).Value
        arrText = rngText.Cells(1, 1).Offset(1, 0).Resize(lngLastRow - 1, 1).Value
    
        For lngRow = LBound(arrVariant) To UBound(arrVariant)
            If Len(Trim(arrVariant(lngRow, 1))) > 0 Then
                intPos = InStr(1, arrText(lngRow, 1), Trim(arrVariant(lngRow, 1)))
                If intPos > 0 Then
                    arrText(lngRow, 1) = Left(arrText(lngRow, 1), intPos - 1) _
                        & Mid(arrText(lngRow, 1), intPos + Len(arrVariant(lngRow, 1)))
                    If Mid(arrText(lngRow, 1), intPos - 1, 2) = "  " Then
                        arrText(lngRow, 1) = Left(arrText(lngRow, 1), intPos - 1) _
                            & Mid(arrText(lngRow, 1), intPos + 1)
                    ElseIf Mid(arrText(lngRow, 1), intPos - 1, 3) = " , " Then
                        arrText(lngRow, 1) = Left(arrText(lngRow, 1), intPos - 2) _
                            & Mid(arrText(lngRow, 1), intPos + 1)
                    End If
                End If
            End If
        Next lngRow
    
        rngText.Cells(2, 1).Resize(UBound(arrText), 1).Value = arrText
    
    End Sub
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    07-10-2012
    Location
    Edinburgh
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Run Time Error in coding - Can someone help me debug please?

    I'm pretty sure its the blanks in column E causing it. Sometimes, like column D, column E will have data and sometimes not. Any ideas?

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Run Time Error in coding - Can someone help me debug please?

    The variable lngLastRow contains a value of 1, so you're trying to resize the range to have zero rows.

    I'm not quite sure what you're trying to do, so I can't really tell you how to fix it, but if want to give me some more detail I'll have a go.

  4. #4
    Registered User
    Join Date
    07-10-2012
    Location
    Edinburgh
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Run Time Error in coding - Can someone help me debug please?

    So there are two columns that contain variant information, size and colour, columns D and E. The product model name, column G, also sometimes contains this data but I need to strip these words out where it does find a match in the variant columns. If you open the example attached you will see what I mean. It works if I populate row 2 & 3 in column E with some text.

    Thanks very much!

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Run Time Error in coding - Can someone help me debug please?

    OK, I think I understand.

    This is how I'd tackle that:

    Sub StripVariant2(rngCheck As Range, rngMaster As Range)
    
    Dim rngLoop As Range
    Dim lOffset As Long
    
    lOffset = rngCheck.Columns(1).Column - rngMaster.Columns(1).Column
    
    For Each rngLoop In Range(rngMaster.Cells(1), Cells(Rows.Count, rngMaster.Columns(1).Column).End(xlUp)).Cells
      If rngLoop.Value <> "" And rngLoop.Offset(0, lOffset).Value <> "" Then
        rngLoop.Value = Replace(rngLoop.Value, rngLoop.Offset(0, lOffset).Value, "")
      End If
    Next rngLoop
    
    End Sub
    Does that help you out?

  6. #6
    Registered User
    Join Date
    07-10-2012
    Location
    Edinburgh
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Run Time Error in coding - Can someone help me debug please?

    That almost works a treat.

    Where there is no size or colour it removes the spaces from the product model name. Any way to modify it for that?

  7. #7
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Run Time Error in coding - Can someone help me debug please?

    Try changing this line:

    If rngLoop.Value <> "" And rngLoop.Offset(0, lOffset).Value <> "" Then
    To:

    If rngLoop.Value <> "" And Trim(rngLoop.Offset(0, lOffset).Value) <> "" Then

  8. #8
    Registered User
    Join Date
    07-10-2012
    Location
    Edinburgh
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Run Time Error in coding - Can someone help me debug please?

    That works great. Thank you very much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1