+ Reply to Thread
Results 1 to 9 of 9

A shorter way to write this macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-16-2004
    MS-Off Ver
    2016
    Posts
    175

    A shorter way to write this macro

    I have this macro but it seems like there must be a shorter way to do this. I have to write it for "If Cells(X, 161).Value = 1,2,3,4,5,6,7,8,9,10". Note (in the example below) as the if value increases by 1 on the left, the cell values increase by 1 as well on the right.

    Sub Moan()
    finalrow = Cells(Rows.Count, 160).End(xlUp).Row
    For X = 4 To finalrow
    If Cells(X, 161).Value = 1 Then Cells(X, 162).Value = Cells(X, 6).Value
    If Cells(X, 161).Value = 1 Then Cells(X, 163).Value = Cells(X, 17).Value
    If Cells(X, 161).Value = 1 Then Cells(X, 164).Value = Cells(X, 50).Value

    If Cells(X, 161).Value = 1 Then Cells(X, 165).Value = Cells(X, 61).Value
    If Cells(X, 161).Value = 1 Then Cells(X, 166).Value = Cells(X, 72).Value
    If Cells(X, 161).Value = 1 Then Cells(X, 167).Value = Cells(X, 83).Value

    If Cells(X, 161).Value = 2 Then Cells(X, 162).Value = Cells(X, 7).Value
    If Cells(X, 161).Value = 2 Then Cells(X, 163).Value = Cells(X, 18).Value
    If Cells(X, 161).Value = 2 Then Cells(X, 164).Value = Cells(X, 51).Value

    If Cells(X, 161).Value = 2 Then Cells(X, 165).Value = Cells(X, 62).Value
    If Cells(X, 161).Value = 2 Then Cells(X, 166).Value = Cells(X, 73).Value
    If Cells(X, 161).Value = 2 Then Cells(X, 167).Value = Cells(X, 84).Value


    Next X
    End Sub
    Last edited by light; 08-20-2023 at 04:53 PM.

  2. #2
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,461

    Re: A shorter way to write this macro

    Sub Moan()
         finalrow = Cells(Rows.Count, 160).End(xlUp).Row
         For x = 4 To finalrow
              Select Case Cells(x, 161).Value
                   Case 1, 2
                        i =  Cells(x, 161).Value - 1
                        Cells(x, 162).Resize(, 6).Value = Array(Cells(x, 6 + i).Value, , Cells(x, 17 + i).Value, Cells(x, 50 + i).Value, Cells(x, 61 + i).Value, Cells(x, 72 + i).Value, Cells(x, 83 + i).Value)
              End Select
         Next
    End Sub
    Last edited by bsalv; 08-20-2023 at 03:27 PM.
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  3. #3
    Forum Contributor
    Join Date
    03-16-2004
    MS-Off Ver
    2016
    Posts
    175

    Re: A shorter way to write this macro

    Thanks but your macro deletes all data in column 163 and changes values in column 168 which I did not mention has values in it but is not part of my macro.

  4. #4
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,293

    Re: A shorter way to write this macro

    Try

    Sub Moan()
    finalrow = Cells(Rows.Count, 160).End(xlUp).Row
    col = Array(6, 17, 50, 61, 72, 83)
     j = Cells(x, 161) - 1
    For x = 4 To finalrow   
        For k = 0 To 5
            Cells(x, 162 + k).Value = Cells(x, col(k) + j).Value
        Next k
    Next x
    End Sub
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  5. #5
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,461

    Re: A shorter way to write this macro

    i repeat the macro from above, with a small modification for the "Case" because it's 1 to 10
    Column 168 ? the macro writes from column 162 for 6 columns, so 162-167, so it modifies(change/delete?) column 163 but column 168, no !

    Sub Moan()
         finalrow = Cells(Rows.Count, 160).End(xlUp).Row
         For x = 4 To finalrow
              Select Case Cells(x, 161).Value
                   Case 1, 2,3,4,5,6,7,8,9,10
                        i =  Cells(x, 161).Value - 1
                        Cells(x, 162).Resize(, 6).Value = Array(Cells(x, 6 + i).Value, , Cells(x, 17 + i).Value, Cells(x, 50 + i).Value, Cells(x, 61 + i).Value, Cells(x, 72 + i).Value, Cells(x, 83 + i).Value)
              End Select
         Next
    End Sub
    Can you post a file with non-sensitive data ?

  6. #6
    Forum Contributor
    Join Date
    03-16-2004
    MS-Off Ver
    2016
    Posts
    175

    Re: A shorter way to write this macro

    bsalv still does the same thing as mentioned above

    JohnTopley Thanks but I'm getting an 1004 error trying to run your macro

  7. #7
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,293

    Re: A shorter way to write this macro

    Sub Moan()
    finalrow = Cells(Rows.Count, 160).End(xlUp).Row
    col = Array(6, 17, 50, 61, 72, 83)
    For x = 4 To finalrow
        j = Cells(x, 161) - 1
        For k = 0 To 5
            Cells(x, 162 + k).Value = Cells(x, col(k) + j).Value
        Next k
    Next x
    End Sub
    !

  8. #8
    Forum Contributor
    Join Date
    03-16-2004
    MS-Off Ver
    2016
    Posts
    175

    Re: A shorter way to write this macro

    Thank you very much JohnTopley. Works great.

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,726

    Re: A shorter way to write this macro

    End Sub
    Not much different from John's.
    John was a lot faster with his (I assume it is "his") suggestion.
    Sub How_About_So()
    Dim colArr, j As Long, c As Range
    colArr = Array(6, 17, 50, 61, 72, 83)
        For Each c In Range("FE4:FE" & Cells(Rows.Count, 160).End(xlUp).Row)
            For j = 1 To 6
                c.Offset(, j).Value = Cells(c.Row, colArr(j - 1)).Value
            Next j
        Next c
    End Sub
    The inherent weakness of the liberal society: a too rosy view of humanity.

+ 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. [SOLVED] can i write this formula in a shorter way
    By sharky1978 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 01-09-2023, 10:18 AM
  2. [SOLVED] is there a shorter way to write this else if code that compares 2 combo box values?
    By kevinu in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-24-2018, 07:48 PM
  3. [SOLVED] Shorter or better write CF formula
    By HeyInKy in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-19-2017, 02:09 PM
  4. [SOLVED] how to make a code shorter to write a paragraph
    By eddyrcabrera79 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2015, 01:10 PM
  5. Replies: 3
    Last Post: 09-06-2005, 07:05 AM
  6. Replies: 0
    Last Post: 09-05-2005, 10:05 PM
  7. Replies: 2
    Last Post: 06-23-2005, 07:05 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