+ Reply to Thread
Results 1 to 3 of 3

Shortening Code

Hybrid View

T De Villiers Shortening Code 02-20-2007, 10:49 AM
Stuie what r u trying to acheive... 02-20-2007, 10:52 AM
antoka05 You can try with: Sub... 02-20-2007, 11:46 AM
  1. #1
    Forum Contributor
    Join Date
    08-20-2005
    Posts
    173

    Shortening Code

    Hi,

    How do I shorten the following:

    sr.Cells(23, 6) = ActiveCell.Offset(1, 0)
    sr.Cells(24, 6) = ActiveCell.Offset(2, 0)
    sr.Cells(25, 7) = ActiveCell.Offset(3, 0)
    sr.Cells(26, 8) = ActiveCell.Offset(4, 0)
    sr.Cells(27, 6) = ActiveCell.Offset(5, 0)
    sr.Cells(28, 6) = ActiveCell.Offset(9, 0)
    sr.Cells(29, 7) = ActiveCell.Offset(12, 0)
    sr.Cells(30, 8) = ActiveCell.Offset(13, 0)
    sr.Cells(31, 6) = ActiveCell.Offset(14, 0)
    sr.Cells(32, 8) = ActiveCell.Offset(15, 0)
    sr.Cells(33, 6) = ActiveCell.Offset(16, 0)
    sr.Cells(34, 6) = ActiveCell.Offset(26, 0)
    sr.Cells(35, 6) = ActiveCell.Offset(24, 0)
    sr.Cells(36, 6) = ActiveCell.Offset(28, 0)
    sr.Cells(37, 6) = ActiveCell.Offset(29, 0)
    sr.Cells(48, 6) = ActiveCell.Offset(6, 0)
    sr.Cells(49, 8) = ActiveCell.Offset(7, 0)
    sr.Cells(50, 7) = ActiveCell.Offset(8, 0)
    sr.Cells(60, 8) = ActiveCell.Offset(17, 0)
    sr.Cells(64, 6) = ActiveCell.Offset(27, 0)
    sr.Cells(72, 6) = ActiveCell.Offset(30, 0)
    sr.Cells(74, 6) = ActiveCell.Offset(10, 0)
    sr.Cells(75, 6) = ActiveCell.Offset(11, 0)
    sr.Cells(164, 8) = ActiveCell.Offset(18, 0)
    sr.Cells(170, 8) = ActiveCell.Offset(21, 0)
    sr.Cells(171, 7) = ActiveCell.Offset(22, 0)


    Regards

  2. #2
    Forum Contributor Stuie's Avatar
    Join Date
    09-17-2006
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003/2007
    Posts
    432
    what r u trying to acheive with code?

    coz depending on exactly what your doing you could prob use a loop

  3. #3
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    You can try with:
    Sub Macro1()
       strMove = Array("023,06 01,0", "024,06 02,0", "025,07 03,0", "026,08 04,0", _
                       "027,06 05,0", "028,06 09,0", "029,07 12,0", "030,08 13,0", _
                       "031,06 14,0", "032,08 15,0", "033,06 16,0", "034,06 26,0", _
                       "035,06 24,0", "036,06 28,0", "037,06 29,0", "048,06 06,0", _
                       "049,08 07,0", "050,07 08,0", "060,08 17,0", "064,06 27,0", _
                       "072,06 30,0", "074,06 10,0", "075,06 11,0", "164,08 18,0", _
                       "170,08 21,0", "171,07 22,0")
    
       For Each elem In strMove
          Row1 = Left(elem, 3)
          Col1 = Mid(elem, 5, 2)
          Row2 = Mid(elem, 8, 2)
          Col2 = Mid(elem, 11, 1)
          
          sr.Cells(Row1, Col1) = ActiveCell.Offset(Row2, Col2)
       Next
    End Sub
    or with:
    Sub Macro2()
       Dim strMove As New Collection
       
       strMove.Add "023,06-01,0"
       strMove.Add "024,06-02,0"
       strMove.Add "025,07-03,0"
       strMove.Add "026,08-04,0"
       strMove.Add "027,06-05,0"
       strMove.Add "028,06-09,0"
       strMove.Add "029,07-12,0"
       strMove.Add "030,08-13,0"
       strMove.Add "031,06-14,0"
       strMove.Add "032,08-15,0"
       strMove.Add "033,06-16,0"
       strMove.Add "034,06-26,0"
       strMove.Add "035,06-24,0"
       strMove.Add "036,06-28,0"
       strMove.Add "037,06-29,0"
       strMove.Add "048,06-06,0"
       strMove.Add "049,08-07,0"
       strMove.Add "050,07-08,0"
       strMove.Add "060,08-17,0"
       strMove.Add "064,06-27,0"
       strMove.Add "072,06-30,0"
       strMove.Add "074,06-10,0"
       strMove.Add "075,06-11,0"
       strMove.Add "164,08-18,0"
       strMove.Add "170,08-21,0"
       strMove.Add "171,07-22,0"
    
       For Each elem In strMove
          Row1 = Left(elem, 3)
          Col1 = Mid(elem, 5, 2)
          Row2 = Mid(elem, 8, 2)
          Col2 = Mid(elem, 11, 1)
          
          sr.Cells(Row1, Col1) = ActiveCell.Offset(Row2, Col2)
       Next
    
    End Sub
    May be many other shorter ways as:
    "A1-C25, Z10-F22, AZ5-AB18" etc.

    Regards,
    Antonio

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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