[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.8

VBA module to find max characters for each row by column in Excel sheet

Asked by b0lsc0tt in Microsoft Excel Spreadsheet Software, Visual Basic Programming, VB Script

Tags: VBA, VB, Excel, VB for Applications, module, sub

I am really asking this for feedback but do have a question or two that I will try to include.  I had a CSV file that I imported into Excel to be able to read and review.  The data will actually be going to a MYSQL DB but that probably doesn't matter.  There are about 15 "fields" or columns (if you think about it as Excel shows it) and 170K records or rows.

To get ready to work with the data in the DB I wanted an idea of how many characters were in each field.  Specifically I wanted to know the max length I would need for a DB field.  Excel spaced the columns nicely but I wasn't sure how to find out how many characters each column needed or required.  So I used the VB Editor and made a little routine.  The code is below (see snippet).  It actually appears to work so I am feeling pretty proud (VBA is something I don't like to use at all and find very frustrating to work in even though I like VB, or at least vbscript).

My main question is do you see any problems or a way to do it better?  Specifically how I reference the cells, columns, rows and "range"?  I think the logic is about as good as can be but working with the "object" of a sheet and all of its elements, properties, etc is where I just lack expertise (and MS' Help seems especially hard to use as reference).

I had to use "CurrentRegion" to get the part of the sheet that was used (i.e. ignore the blank or unused columns to the right and rows below) but I didn't think I had to use it before.  I have only made couple but thought there was an easier way to select just the used cells and was frustrated until I found the CurrentRegion property(?).  Is there another that is often used or is the only other option specifically using cell references to set the range?

The only "bug" that I found when running it was the max character amount was place on the second row down, not the very lowest row.  Any idea why looking at my For loop and the line below?

Finally why couldn't I use For Each to loop through the Rows or Columns "collection"?  Specifically the columns since I changed the For Each loop for the rows because I wanted a "counter."  I was getting errors though and didn't understand why but they left when I changed to a "For i = 1" loop.

BTW, was I correct to start the loop at 1?  I couldn't find that for sure but thought the count of columns/rows started at 1.  Just was hoping to verify that.

Thanks!  Let me know if there is a question about this or you need any more info.

bol
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
Attribute VB_Name = "Module1"
Sub getMaxLen()
    Dim col, i, line
    Dim myRange As Range
    Worksheets("Sheet1").Activate
    Set myRange = ActiveCell.CurrentRegion
    'MsgBox (myRange.Rows.Count & " - " & myRange.Columns.Count)
    'Exit Sub
    For col = 1 To myRange.Columns.Count
        If col > 16 Then Exit Sub
        'MsgBox (Cells(1, col).Value)
        'If Cells(1, col).Value = "" Then Exit Sub
        i = 0
        For line = 1 To myRange.Rows.Count
            If Len(Cells(line, col).Value) > i Then i = Len(Cells(line, col).Value)
        Next
        Worksheets("Sheet1").Cells((line + 1), col).Value = i
    Next
End Sub
[+][-]10/08/09 08:55 PM, ID: 25532208Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Microsoft Excel Spreadsheet Software, Visual Basic Programming, VB Script
Tags: VBA, VB, Excel, VB for Applications, module, sub
Sign Up Now!
Solution Provided By: zorvek
Participating Experts: 2
Solution Grade: A
 
[+][-]10/08/09 04:15 PM, ID: 25531201Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10/08/09 04:30 PM, ID: 25531289Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/08/09 07:56 PM, ID: 25532060Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 06:18 AM, ID: 25534623Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10/09/09 01:54 PM, ID: 25538799Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/09/09 02:36 PM, ID: 25539092Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/12/09 07:00 PM, ID: 25556464Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/12/09 09:27 PM, ID: 25556954Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_EXPERT_20070906