Solved

Excel 2010 VBA: Run-time error '32809': Application-Defined or object-Defined error

Posted on 2011-02-22
6
7,567 Views
Last Modified: 2012-05-11
hi,
i am a total newbie regarding writing macros in excel 2010 - the following code which was meant to concatenate the columns 28-30 into 26 for every row in the active sheet using excel 2010 gives the error in the title. i have tried google to find a solution, but to no avail. why is that error being generated in Transposedata()?
the error is displayed on line 8 every time the following code runs:
Sub Transposedata()
    Dim strNotes As String
    Dim r As Integer, c As Integer
    Dim intMax As Integer
    intMax = LastRow(ActiveSheet)
    For r = 2 To intMax Step 1
        For c = 28 To 30 Step 1
            strNotes = IIf(c = 28, ActiveSheet.Cells(r, c).Value, strNotes & strRepeat(vbCrLf, 2) & ActiveSheet.Cells(r, c).Value)
        Next c
        ActiveSheet.Cells(r, 26).Value = strNotes
    Next r
    MsgBox "Done!"
End Sub
Function LastRow(ws As Worksheet) As Single
    'uses worksheet object
    'returns last used row
    On Error Resume Next
    With ws
      LastRow = .Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row
    End With
End Function
Function strRepeat(str As String, cnt As Integer) As String
    For i = 1 To cnt
        str = str & str
    Next i
End Function

Open in new window

0
Comment
Question by:intellisource
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:shahzadbux
ID: 34953726
Is that a typo on line 8?

two i's for  the if...

strNotes = If(c = 28, ActiveSheet.Cells(r, c).Value, strNotes & strRepeat(vbCrLf, 2) & ActiveSheet.Cells(r, c).Value)
                   
0
 
LVL 39

Accepted Solution

by:
nutsch earned 25 total points
ID: 34953731
Define your variables as long instead of integer, integer maxes out after 32 some thousands.

You could speed up the code by avoiding the loop and letting formulas do their job

Sub Transposedata()
    Dim strNotes As String
Dim lastRow As Long
lastRow = Cells(Rows.Count, 28).End(xlUp).Row

Application.ScreenUpdating = False

With Range(Cells(2, 26), Cells(lastRow, 26))
    .FormulaR1C1 = "=RC[2] & char(10) & char(10) & rC[3]& char(10) & char(10) & rC[4] "
    .Value = .Value
End With

Application.ScreenUpdating = True

MsgBox "Done!"
End Sub

Open in new window


Thomas
0
 
LVL 39

Expert Comment

by:nutsch
ID: 34953741
@shazadbux: IIF function

T
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:shahzadbux
ID: 34953778
Cheers Thomas
0
 

Author Closing Comment

by:intellisource
ID: 34953996
thanks Thomas! this really did the trick ;)
0
 
LVL 39

Expert Comment

by:nutsch
ID: 34954131
Thanks for the grade.

For reference, because of the VBA knowledge involved, I'd probably price this type of questions around 125 points. It's not overly complicated, but it requires analysis of the code you posted. Since you have unlimited points to ask questions, it's also a way to get faster answers (not that you had to wait long on that one).

Thomas
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now