+ Reply to Thread
Results 1 to 6 of 6

VBA cleanup

Hybrid View

  1. #1
    Registered User
    Join Date
    01-23-2010
    Location
    Brisbane
    MS-Off Ver
    Excel 2003
    Posts
    20

    VBA cleanup

    Hi
    I had a query in the Excel General area and have come up with an solution that works reasonably well with some basic VBA coding.
    Is this coding OKAY or can it be tidied up a bit ?

    I would like to think I was on the right path in coding this.

    Here is the original posting....




    http://www.excelforum.com/excel-gene...match-vba.html

  2. #2
    Registered User
    Join Date
    11-13-2006
    Posts
    87

    Re: VBA cleanup

    Let us assume for the sake of argument that you are referring to the code i post #8. Then no I can't clear it up because it is not the entire procedure.

    Post the whole thing here, from Sub to End sub, inclusive.

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: VBA cleanup

    First, you don't need a colon at the end of every line!

    Second, avoid selecting things - it's unnecessary 99.86% of the time.

    Third, always specify the property you want - even .Value

    Untested:
        Dim wsAN                        As Excel.Worksheet
    
        R_COUNT = Worksheets("AN XML").Cells(1, 4).Value    ' count occupied rows
    
        '---------------------------------------------
        For Cycle = 1 To 6    ' Do 6 SORT passes to capture 6 identical multi entries (change this if more is req'd) (LIKE data types for comparison)
            Set wsAN = Sheets("AN SOURCE")
            With wsAN
                With .Sort
                    With .SortFields
                        .Clear
                        .Add Key:=wsAN.Range(wsAN.Cells(3, 1), wsAN.Cells(R_COUNT, 1)), SortOn:=xlSortOnValues, _
                             Order:=xlAscending, DataOption:=xlSortNormal
                        .Add Key:=wsAN.Range(wsAN.Cells(3, 6), wsAN.Cells(R_COUNT, 6)), SortOn:=xlSortOnValues, _
                             Order:=xlAscending, DataOption:=xlSortNormal
                        .Add Key:=wsAN.Range(wsAN.Cells(3, 5), wsAN.Cells(R_COUNT, 5)), SortOn:=xlSortOnValues, _
                             Order:=xlAscending, DataOption:=xlSortNormal
                    End With
                    .SetRange wsAN.Range(wsAN.Cells(3, 1), wsAN.Cells(R_COUNT, 25))
                    .Header = xlGuess
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
                '====================================
                For R_COUNTER = 3 To R_COUNT    ' count from row 3 to last occupied row
    
                    R_STRNG = ""    'Null out any legacy data
                    R_STRNG = Right(.Cells(R_COUNTER, 5).Value, 4)    'Assigns new data - Returns .xls, xlsm, .pdf etc
    
                    EX_STRNG = ""    'Null out any legacy data
                    EX_STRNG = (.Cells(R_COUNTER, 6).Value)    'Returns EXIST string
    
                    NEW_STRNG = ""    'Null out any legacy data
                    NEW_STRNG = EX_STRNG & " " & R_STRNG    'Create NEW string
    
                    FLAGGED = ""
                    If (.Cells(R_COUNTER, 1).Value) = (.Cells(R_COUNTER - 1, 1).Value) And (.Cells(R_COUNTER, 6).Value) = (.Cells(R_COUNTER - 1, 6).Value) Then FLAGGED = "WRITE"
                    If FLAGGED = "WRITE" Then .Cells(3, 1).Offset(R_COUNTER - 3, 5) = NEW_STRNG
                Next R_COUNTER
                '====================================
            Next Cycle
        End With
    I am not suggesting this is the most efficient way of achieving your ends, just amending the existing code.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Registered User
    Join Date
    11-13-2006
    Posts
    87

    Re: VBA cleanup

    A picture may be worth a thousand words but a workbook is worth a thousand pictures!
    Can I steal that? I'll credit you.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: VBA cleanup

    Feel free.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,167

    Re: VBA cleanup

    Maybe this:

    Option Explicit
    
    Sub sModifyColumnA()
    
    Dim lLR As Long, i As Long
    Dim sString As String
    Dim vArray
    
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    ReDim vArray(1 To lLR)
    
    For i = 1 To lLR
        sString = "=IF($A" & i & "="""","""",COUNTIFS($A$2:$A" & i & ",$A" & i & ",$F$2:$F" & i & ",$F" & i & "))"
        If Evaluate(sString) = "" Then
            vArray(i) = ""
        Else
            vArray(i) = IIf((Evaluate(sString)) > 1, Range("A" & i).Value & "-" & Evaluate(sString), Range("A" & i).Value)
        End If
    Next 'i
    
    Application.ScreenUpdating = False
    Range("A1").Resize(lLR, 1) = Application.Transpose(vArray)
    Application.ScreenUpdating = True
    
    End Sub

    Regards, TMS
    Attached Files Attached Files
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA Cleanup
    By specialk9203 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-20-2014, 09:50 AM
  2. [SOLVED] Code Cleanup Help
    By maddog9486 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-13-2011, 02:19 PM
  3. Code cleanup
    By timlocke in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-10-2011, 08:01 PM
  4. String Cleanup
    By TheAndarious in forum Excel General
    Replies: 1
    Last Post: 07-21-2010, 03:59 PM
  5. if (formula cleanup)
    By simpson in forum Excel General
    Replies: 8
    Last Post: 05-19-2010, 08:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1