Share via

VBA RunTimeError '91' - copy and pasting error

Anonymous
2014-05-22T14:48:51+00:00

Hello, I am new to this forum and was wondering if anyone could help me. I am using one workbook (1) to open a file (workbook (2)), search for a label, which is listed on a sheet in workbook (1), and copy all the data relating to that label into the sheet. However the issue that I am having is when the label does not appear in workbook (2) then I get a run-time error '91'. I have tried using "On Error Resume Next", but the wrong data gets copied into that column for the missing label. All I want is for the cells to be left blank corresponding to the missing label, and the code to continue copying all the other data correctly.

The code below looks for label name and then copies it and loops through.... Please will someone help me out! Will be very much appreciated.

Cheers

Do While 1

        'get the next variable name

        thisBookCol = ActiveCell.Address

        thisBookCol = Right(thisBookCol, Len(thisBookCol) - 1)

        thisBookCol = Left(thisBookCol, InStr(thisBookCol, "$") - 1)

        findName = Range(thisBookCol + "1")

        'Find the variable column position and copy to template

        Windows(str_fileName).Activate

        ActiveSheet.Range("B2").Select

        Selection.Copy

        Windows(thisBook).Activate

        Range("A2").Select

        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

            :=False, Transpose:=False

        Windows(str_fileName).Activate

        Columns("A:A").Select

        Selection.Find(What:="TimeStamp", After:=ActiveCell, LookIn:=xlFormulas, _

            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _

            MatchCase:=False, SearchFormat:=False).Activate

        'On Error Resume Next

        labelnamerow = ActiveCell.Row

        Rows(labelnamerow).Select

        Selection.Find(What:=findName, After:=ActiveCell, LookIn:=xlFormulas, _

            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _

            MatchCase:=False, SearchFormat:=False).Activate

        currPos = Right(ActiveCell.Address, Len(ActiveCell.Address) - 1) 'Remove first $

        colRef = Left(currPos, InStr(currPos, "$") - 1)

        'Range(colRef & 38).Select

        Range(colRef & (labelnamerow + 2)).Select

        Range(Selection, Selection.End(xlDown)).Select

        Selection.Copy

        Windows(thisBook).Activate

        Range(thisBookCol + "2").Select

        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

            :=False, Transpose:=False

        ActiveCell.Offset(-1, 1).Select

        'Check to see if this was the last variable required

        If thisBookCol = maxCol Then

            Exit Do

        End If

    Loop

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2014-05-22T23:11:48+00:00

    We've probably forgotten some very minor point.

    Download this file and take a look in it

    Edit: eMail received with test files. 5/23/2014.

    Alternatively, you could upload a copy of the file with any confidential/personal information obscured to one of these free file hosting sites and then post back here with the link to the file that they give you:

    MediaFire: http://www.mediafire.com

    Windows OneDrive: https://onedrive.live.com/

    FileFactory: http://www.filefactory.com

    FileSavr: http://www.filesavr.com

    FileDropper: http://www.filedropper.com

    RapidShare: http://www.rapidshare.com

    Box.Net: http://www.box.net/files

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-05-22T21:48:56+00:00

    Hello mate, again, thank you very much for your help. Now however the code just loops without picking up the next label. If I sent you the workbooks would you please have a look at where I am going wrong? I have spent so many hours on this, I am losing my mind…. Can I send it to you remotely?

    Thanks,

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-05-22T21:07:38+00:00

    My bad, I think - if you are getting the error here:

         If Not FoundLabel Is Nothing Then

            FoundCell.Select ' so it becomes the ActiveCell

    Change FoundCell to FoundLabel

    apologies...

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2014-05-22T20:10:51+00:00

    Thank you very much for your quick reply. There is however a runtime error with the FoundCell.Select. I want to insert blanks into these columns.

    If I remove FoundCell.Select then there is an error at ActiveCell.Offset(-1, 1).Select.

    Thank you very much for your help.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2014-05-22T15:17:59+00:00

    Look at the Bold code I've added - it should help you.  When there is no match, "nothing" gets found, and that's the basic problem here.  This will help handle the no-label-found issue - although I may have the End If statements in slightly wrong place(s) - I'll leave it to you to determine what to do when there is a match and when there isn't one.

    Dim FoundLabel As Range

    Dim FoundCell2 As Range

    Do While 1

            'get the next variable name

            thisBookCol = ActiveCell.Address

            thisBookCol = Right(thisBookCol, Len(thisBookCol) - 1)

            thisBookCol = Left(thisBookCol, InStr(thisBookCol, "$") - 1)

            findName = Range(thisBookCol + "1")

            'Find the variable column position and copy to template

            Windows(str_fileName).Activate

            ActiveSheet.Range("B2").Select

            Selection.Copy

            Windows(thisBook).Activate

            Range("A2").Select

            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

                :=False, Transpose:=False

            Windows(str_fileName).Activate

            Columns("A:A").Select

    'note that I removed .Select from the end of the next statement

          Set FoundLabel  = Selection.Find(What:="TimeStamp", After:=ActiveCell, LookIn:=xlFormulas, _

    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _

    MatchCase:=False, SearchFormat:=False)

             'On Error Resume Next

          If Not FoundLabel Is Nothing Then

            FoundCell.Select ' so it becomes the ActiveCell

            labelnamerow =  **** ActiveCell.Row

            Rows(labelnamerow).Select

            Set FoundCell2 = Selection.Find(What:=findName, After:=ActiveCell, LookIn:=xlFormulas, _

                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _

                MatchCase:=False, SearchFormat:=False) ' .Activate deleted!

    If Not FoundCell2 Is Nothing Then

            FoundCell.Select ' so it can become the ActiveCell for the rest of your code here

            currPos = Right(ActiveCell **** .Address, Len(ActiveCell.Address) - 1) 'Remove first $

            colRef = Left(currPos, InStr(currPos, "$") - 1)

            'Range(colRef & 38).Select

            Range(colRef & (labelnamerow + 2)).Select

            Range(Selection, Selection.End(xlDown)).Select

            Selection.Copy

            Windows(thisBook).Activate

            Range(thisBookCol + "2").Select

            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

                :=False, Transpose:=False

    End If ' end test for FoundCell2 is nothing

            ActiveCell.Offset(-1, 1).Select

            End If ' end test for FoundCell is Nothing

            'Check to see if this was the last variable required

            If thisBookCol = maxCol Then

                Exit Do

            End If

        Loop

    Was this answer helpful?

    0 comments No comments