Solved

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

Posted on 2011-02-22
6
7,519 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

13 Experts available now in Live!

Get 1:1 Help Now