Excel/Dynamic Offset

Advertisement


Question
Hello Tom, Is there a VBA way of making the Offset in the following code "dynamic"? I have to alter the C.Offset(0,19) and C.offset(0,20)manually each week as data occupies more columns. Basically I want to put the formulas into the next data free columns. Or if there is a better solution i'd gladly accept your advice.
Data typically occupies 500 rows
I  Use Excel 2007
Kind Regards Robert


ScoreTester()

'Column B are names
'Column C are Football results
'Column C Typical Data = 1-2, 3-0, 0-0 etc
'Columns D,E,F,G etc etc contains other data which expands weekly
'Offset 19 and 20 splits Col C scores into 2 columns

lastrow = Cells(Rows.Count, "b").End(xlUp).Row

Set RngB = Range("b2:b" & lastrow)
For Each C In RngB

C.Offset(0, 19).Value = Trim(Left(C.Offset(0, 1).Value, 1)) C.Offset(0, 20).Value = Trim(Right(C.Offset(0, 1).Value, 1))
Next

End Sub

Answer
Robert,

If you need the last column that has a value say in row 10 then you could do  

cells(10,columns.count).End(xltoLeft).column

so assuming that row 2 would be good for this

ScoreTester()

'Column B are names
'Column C are Football results
'Column C Typical Data = 1-2, 3-0, 0-0 etc
'Columns D,E,F,G etc etc contains other data which expands weekly
'Offset 19 and 20 splits Col C scores into 2 columns

lastrow = Cells(Rows.Count, "b").End(xlUp).Row
lastcol = Cells(2, columns.count).End(xltoLeft).Column

Set RngB = Range("b2:b" & lastrow)
For Each C In RngB

cells(c.row, lastcol + 1).Value = Trim(Left(C.Offset(0, 1).Value, 1))
cells(c.row, lastcol + 2).Value = Trim(Right(C.Offset(0, 1).Value, 1))
Next

End Sub

--
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.