Excel/Shorten code

Advertisement


Question
Hello Tom,I have some code which works fine but i'm hoping there is a more efficient, shorter way of writing it. I have experimented with using a counter loop, but to be honest I got in a right pickle. Below is part of my working code.
I can send sample data if required
I use Excel 2007

Kind Regards
Robert

Sub ArrangeVerticalResultsHorizontal()

lastrow = Cells(Rows.count, "a").End(xlUp).Row
Set RngB = Range("B2:b" & lastrow)

For Each c In RngB

' RngB Data is Names sorted A to Z
' RngB can be upto 40 consequtive team names x 20 teams
' Offset 6 contains football results either W,D,L for Win Lose Draw
' Column A contains match dates
' Purpose of macro to list all scores horizontally
' For each occurrence of the team name
' For each Date

'Some of the code reduced for clarity

If c.Offset(15, 0).Value = c.Value Then

c.Offset(0, 7).Value = c.Offset(1, 6).Value
c.Offset(0, 8).Value = c.Offset(2, 6).Value
c.Offset(0, 9).Value = c.Offset(3, 6).Value
c.Offset(0, 10).Value = c.Offset(4, 6).Value
c.Offset(0, 11).Value = c.Offset(5, 6).Value
c.Offset(0, 12).Value = c.Offset(6, 6).Value
c.Offset(0, 13).Value = c.Offset(7, 6).Value
c.Offset(0, 14).Value = c.Offset(8, 6).Value
c.Offset(0, 15).Value = c.Offset(9, 6).Value
c.Offset(0, 16).Value = c.Offset(10, 6).Value
c.Offset(0, 17).Value = c.Offset(11, 6).Value
c.Offset(0, 18).Value = c.Offset(12, 6).Value
c.Offset(0, 19).Value = c.Offset(13, 6).Value
c.Offset(0, 20).Value = c.Offset(14, 6).Value
c.Offset(0, 21).Value = c.Offset(15, 6).Value

Else

If c.Offset(14, 0).Value = c.Value Then

c.Offset(0, 7).Value = c.Offset(1, 6).Value
c.Offset(0, 8).Value = c.Offset(2, 6).Value
etc
etc
etc

Else


If c.Offset(13, 0).Value = c.Value Then

c.Offset(0, 7).Value = c.Offset(1, 6).Value
c.Offset(0, 8).Value = c.Offset(2, 6).Value
etc
etc
etc

Else

' All the way down to

Else
If c.Offset(2, 0).Value = c.Value Then

c.Offset(0, 7).Value = c.Offset(1, 6).Value
c.Offset(0, 8).Value = c.Offset(2, 6).Value

 Else
   If c.Offset(1, 0).Value = c.Value Then

c.Offset(0, 7).Value = c.Offset(1, 6).Value

End If
   End If
  End if
       etc
        etc

Next

End Sub

Answer
Robert,


How about sending me a workbook with your source data and another sheet with the output of your macro run against your source data so I can see what you are trying to do.  
Include your macro as well.

twogilvy@msn.com

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.