Results 1 to 5 of 5

How to apply 'for' loop for repetitive tasks in multiple column

Threaded View

  1. #1
    Registered User
    Join Date
    01-04-2021
    Location
    India
    MS-Off Ver
    2016
    Posts
    2

    How to apply 'for' loop for repetitive tasks in multiple column

    Hi All,

    I am new to VBA coding and would be great if anyone can help me with the below two points:

    1. I have written a code however not able to put it into a for loop, so that i can stop rewriting the same code for multiple columns, secondly i am not able to add piece of code to replace single backslash with double backslash in the same code, below is the code i am using:

    Dim addr As String
            
    Sheets("ROM Catalog Collection").Unprotect
    
    With Worksheets("ROM Catalog Collection")
    Row2 = .range("C4").End(xlDown).Row
    For i = 4 To Row2
     
    If .Cells(i, "C").value <> "" Then
    .Cells(i, "C").value = Replace(Application.Trim(.Cells(i, "C").value), "  ", " ")
    End If
    If .Cells(i, "C").value <> "" Then
    .Cells(i, "C").value = Replace(Application.Trim(.Cells(i, "C").value), """", "'")
    End If
    If .Cells(i, "C").value <> "" Then
    .Cells(i, "C").value = Replace(Application.Trim(.Cells(i, "C").value), "chr(151) or chr (150)", "-")
    End If
    
    
    Next i
    
    End With
    
    With Worksheets("ROM Catalog Collection")
    Row2 = .range("D4").End(xlDown).Row
    For i = 4 To Row2
     
    If .Cells(i, "D").value <> "" Then
    .Cells(i, "D").value = Replace(Application.Trim(.Cells(i, "D").value), "  ", " ")
    End If
    If .Cells(i, "D").value <> "" Then
    .Cells(i, "D").value = Replace(Application.Trim(.Cells(i, "D").value), """", "'")
    End If
    If .Cells(i, "D").value <> "" Then
    .Cells(i, "D").value = Replace(Application.Trim(.Cells(i, "D").value), "chr(151) or chr (150)", "-")
    End If
    Next i
    
    End With
    
    With Worksheets("ROM Catalog Collection")
    Row2 = .range("G4").End(xlDown).Row
    For i = 4 To Row2
     
    If .Cells(i, "G").value <> "" Then
    .Cells(i, "G").value = Replace(Application.Trim(.Cells(i, "G").value), "  ", " ")
    End If
    If .Cells(i, "G").value <> "" Then
    .Cells(i, "G").value = Replace(Application.Trim(.Cells(i, "G").value), """", "'")
    End If
    If .Cells(i, "G").value <> "" Then
    .Cells(i, "G").value = Replace(Application.Trim(.Cells(i, "G").value), "chr(151) or chr (150)", "-")
    End If
    Next i
    
    End With
    
    With Worksheets("ROM Catalog Collection")
    Row2 = .range("H4").End(xlDown).Row
    For i = 4 To Row2
     
    If .Cells(i, "H").value <> "" Then
    .Cells(i, "H").value = Replace(Application.Trim(.Cells(i, "H").value), "  ", " ")
    End If
    If .Cells(i, "H").value <> "" Then
    .Cells(i, "H").value = Replace(Application.Trim(.Cells(i, "H").value), """", "'")
    End If
    If .Cells(i, "H").value <> "" Then
    .Cells(i, "H").value = Replace(Application.Trim(.Cells(i, "H").value), "chr(151) or chr (150)", "-")
    End If
    Next i
    
    End With
    
    (continued for 8 more columns..)
    2. Another question i have is, if in a particular cell i have 2 values with '.' (dot) separator i can replace it with ',' (comma) or if there are multiple spaces i can trim it to one space however, if there is 1 value in the same cell its still replacing it with ',' as per the above rule, as for single value i just want to trim with single space or remove the dot if any.

    EX:

    'ABC. DEF' should get replaced as 'ABC,DEF' by removing space and replace the dot'.' with comma',' (below code is working till here)
    if it's only 'ABC .' its converting it as 'ABC,'
    What amendment i can make here so it becomes only 'ABC' (trim the space and remove any special character) in case of single value in that cell.

    Using the below code:

    Sub AppMnemonics()
    Dim c As range, lr As Long
    Sheets("ROM Catalog Collection").Unprotect
    
    lr = Cells(Rows.Count, "J").End(xlUp).Row
    With Worksheets("ROM Catalog Collection")
        lRow = .range("J4").End(xlDown).Row
        
        For i = 4 To lRow
        If .Cells(i, "J").value <> "" Then
                    .Cells(i, "J").value = WorksheetFunction.Trim(.Cells(i, "J").value)
                    End If
        Next i
    End With
    For Each c In range("J4:J" & lr)
    
        c.value = Replace(Application.Trim(c), ".", ",")
        c.value = Replace(Application.Trim(c), " ", "")
    Next c
    Thanks!
    Last edited by Ronith; 01-06-2021 at 04:47 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Repetitive tasks on forms
    By NickNunes in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-19-2018, 04:22 AM
  2. Daily Excel Reports, repetitive daily tasks
    By Lisa_NYC in forum Excel General
    Replies: 13
    Last Post: 10-15-2014, 02:46 PM
  3. Creating loop to apply to multiple textboxes
    By acwhite42 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-04-2013, 11:53 AM
  4. Loop for repetitive code
    By jackandjill in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-18-2010, 10:18 PM
  5. automating repetitive tasks
    By hoshino in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-09-2009, 01:43 AM
  6. [SOLVED] Macro -- repetitive tasks
    By OTS in forum Excel General
    Replies: 1
    Last Post: 10-07-2005, 12:05 PM
  7. Using macro to run repetitive tasks
    By OTS in forum Excel General
    Replies: 1
    Last Post: 10-07-2005, 09:05 AM

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