Results 1 to 6 of 6
  1. #1
    shexe is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2010
    Posts
    71

    Trim vba

    I need some assistance creating a TRIM function in VBA. I export a table to Excel and run a macro that backfills primary member data for instances of secondary members. I'm hoping to just include a trim function in the Macro, but the function needs to run for the heading columns and content.

    I know this is an Access forum but I have found people on here to be very crafty with VBA as well.

    Below is my macro.

    Sub Macro()
    Dim oname 'Output File name
    Dim subssn_value
    Dim subssnb4_value
    Dim subfirst_value
    Dim subfirstb4_value
    Dim x As Integer
    Dim name As String
    x = 2 'counter for the row in colum B

    Sheets("MemloadMetsLite").Select
    For x = 2 To 445 '445 rows
    subssnb4_value = Cells(x, 6)
    subfirstb4_value = Cells(x, 2)
    If subfirstb4_value = "" Then
    subfirstb4_value = name
    End If
    If subssnb4_value = subssn_value Then
    Cells(x, 2) = name
    End If
    subssn_value = subssnb4_value
    name = subfirstb4_value
    'If subfirstb4_value = "" And name <> Null Then
    'Cells(x, 2) = name


    'End If

  2. #2
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481
    It sounds as if you are exporting data to Excel just to do some stuff that should be done in Access.
    1) You could put your Sub or Function in a module1 and call it from forms and queries.
    2) You are adding data to a table??? Is it already in an Access table?? Why not import the data into an Access table and run an update query?
    3) You can change headings and control labels with VBA. IMHO, you shouldn't plan on changing the field names of anything.

  3. #3
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    I've never used Excel VBA but Access VBA comes with a Trim Function built in. I would be surprised if Excel didn't also.

  4. #4
    shexe is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2010
    Posts
    71
    Ray - can you add some insight to the TRIM Access VBA function?

    HertfordKC - I agree but I cannot make any dramatic changes to this process yet as the group is very wary of any change. Ideally the Excel portion will be migrated to Access in February. As for right now I am patching leaks during their peak season.

  5. #5
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481
    The Trim function removes blanks at the beginning or end of a string, e.g.
    Trim(" 123 ") will return "123". You can use it in a calculated field of a query, in a control, and of course VBA.
    Also, you might look at the Replace function if you have blanks (or other characters) that you need to edit out or replace. Just google VBA Replace.

  6. #6
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    hertfordkc explained it very well.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Trim value
    By dada in forum Programming
    Replies: 5
    Last Post: 09-02-2010, 11:01 PM
  2. Trim Function
    By ZMac in forum Reports
    Replies: 9
    Last Post: 08-30-2010, 07:36 AM
  3. Need Help with TRIM command
    By rbfarley in forum Reports
    Replies: 3
    Last Post: 01-08-2010, 02:03 PM
  4. COPY DATA and TRIM
    By Ed H in forum Access
    Replies: 1
    Last Post: 01-01-2009, 07:01 AM
  5. Trim
    By JMantei in forum Forms
    Replies: 1
    Last Post: 06-20-2006, 02:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums