Solved

VB6 - Trim before upload in MSHFlexgrid1

Posted on 2012-12-29
15
537 Views
Last Modified: 2012-12-30
Hello all

I have this code that paste data from an excel sheet to my MSHFlexgrid1.

I would like to TRIM each text in MSHFlexgrid cell when data is in it.

How can i do this?

Thanks again for your help


Full code

    Dim ARRAYLINES As Variant    ' array with the lines
    Dim ARRAYCELLS As Variant    ' array with the cells of 1 line to count the cols needed
    Dim ARRAYLINESidx As Integer
    '§ put clipboard in textbox
    With Text2
        .Text = Clipboard.GetText
        If .Text = "" Then
            MsgBox "No data in clipboard"
            Exit Sub
        Else
            ARRAYLINES = Split(.Text, vbNewLine)
        End If
    End With
    '§ put textbox in grid
    If ARRAYLINES(0) = "" Then
        MsgBox "No data"
        Exit Sub
    Else
        ARRAYCELLS = Split(ARRAYLINES(0), vbTab)
        With MSHFlexGrid1
            .Cols = UBound(ARRAYCELLS) + 1
            .Rows = 0    '§ keep header
            For ARRAYLINESidx = LBound(ARRAYLINES) To UBound(ARRAYLINES) - 1
                .AddItem (ARRAYLINES(ARRAYLINESidx))
            Next
        End With
    End If
    '§ clear array
    ReDim ARRAYLINES(0)
    ReDim ARRAYCELLS(0)

Open in new window

0
Comment
Question by:Wilder1626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
15 Comments
 
LVL 48

Accepted Solution

by:
Martin Liss earned 300 total points
ID: 38730184
.AddItem Trim(ARRAYLINES(ARRAYLINESidx))
0
 
LVL 29

Assisted Solution

by:IrogSinta
IrogSinta earned 200 total points
ID: 38730185
How about .AddItem (Trim(ARRAYLINES(ARRAYLINESidx)))
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38730195
Hi again

I did try both option but still, it does not trim each cell.
.AddItem Trim(ARRAYLINES(ARRAYLINESidx))
.AddItem (Trim(ARRAYLINES(ARRAYLINESidx)))
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 48

Expert Comment

by:Martin Liss
ID: 38730204
Can you attach a sample WB that contains at least one of the cells that you are working with?
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38730220
Sure

If you upload from A1 to AA1 in the MSHFlexgrid using the code, you will see some cells not trim.

In excel, they are in yellow
trim-text-sample.xlsx
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 38730240
All you have in that cell is "wer" with spaces before and after it so Trim should work just fine. What do you see after you trim it?
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38730249
i still see the spaces in the cell.

I did 2 row, one with spaces in the "WER" and one with no spaces.

spaces issue


        ARRAYCELLS = Split(ARRAYLINES(0), vbTab)
        With MSHFlexGrid1
            .Cols = UBound(ARRAYCELLS) + 1
            .Rows = 0    '§ keep header
            For ARRAYLINESidx = LBound(ARRAYLINES) To UBound(ARRAYLINES) - 1
                '.AddItem (ARRAYLINES(ARRAYLINESidx))
                .AddItem (Trim(ARRAYLINES(ARRAYLINESidx)))
            Next
        End With
    End If
    '§ clear array
    ReDim ARRAYLINES(0)
    ReDim ARRAYCELLS(0)

Open in new window

0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 38730250
One thing you might want to do is to read my article on debugging so that you can determine what's in the array at various points in the code.
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 38730254
Are you saying that you see spaces in line 2? If so where?

Also (and this isn't the problem), you definitely don't need the outside parentheses around Trim. And unless there's code after line 30 where you want to reuse the two arrays, there's no need to Redim them since VB6 will initialize them the next time the code is run.

One thing to try.

Dim ARRAYLINES() As String
Dim ARRAYCELLS() As String
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 38730255
thanks again for your help.

What i did it to trafer the row in multiple text box and all textbox are trim

Thanks again
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38730271
I tried by updating:
Dim ARRAYLINES() As String
Dim ARRAYCELLS() As String

But still same result.

I have also deleted:
  '§ clear array
    ReDim ARRAYLINES(0)
    ReDim ARRAYCELLS(0)


And also updated:
.AddItem Trim(ARRAYLINES(ARRAYLINESidx))
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 38730276
What was the problem?
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38730670
well there is no error but it's just does not trim the text once in the grid.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38730889
After a good night of sleep, i decided to do it the easy way. Probably not the best way.
I put the code bellow after uploading the data in my MSHFlexgrid.
Dim lngRow As Long
Dim lngCol As Long

For lngRow = 0 To MSHFlexGrid1.Rows - 1
    For lngCol = 0 To MSHFlexGrid1.Cols - 1
        MSHFlexGrid1.TextMatrix(lngRow, lngCol) = Trim(MSHFlexGrid1.TextMatrix(lngRow, lngCol))
    Next
Next

Open in new window


Thanks again for your help
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 38731011
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

631 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