You are here:

Excel/Getting Data into Consecutive Cells

Advertisement


Question
Hi Tom, could you please help me with a possible vba solution to getting data into consecutive cells in the same row. The first bit of data is always in column AA however there may be 1 or none or up to 10 empty cells before the next bit of data and so on.So instead of A    BC D     EFG etc I want to get ABCDEFG from col AA.
I cannot use delete columns as there maybe data in that column in a subsequent row.
The data isn't critical and doesn't affect any other part of the spread sheet, I was just hoping to tidy it up, maximum rows in the spread sheet is 30.I hope this makes sense
I use Excel 2007
Regards Robert

Answer
Robert,

==========<revised response>============
After sending my response I noticed your subject said that you wanted data in consecutive cells and I missed that fact in your problem statement and misinterpreted your example.  My bad.    I have revised my code to operate in  a much different fashion from what I first thought you were asking for.  


If you want to delete blanks cells beyond column AA then assume your data starts in AA2

Sub Deleteblankcells()
Dim r As Range, cell As Range
Dim r1 As Range, cell1 As Range
Dim s As String
 Set r = Range("AA2", Cells(Rows.Count, "AA").End(xlUp))
 For Each cell In r
  On Error Resume Next
  Set r1 = Range(cell, Cells(cell.Row, Columns.Count).End(xlToLeft)).SpecialCells(xlBlanks)
  r1.Delete Shift:=xlShiftToLeft
  On Error GoTo 0
 Next
End Sub


This assumes that your cells are really blank and not contain some formula that makes the cell appear blank or that the cell contains a space or spaces.  

That worked for me (but that assessment is based on my understanding of the situation/requirement you describe).  Since it changes your data you MUST test this on a COPY of your worksheet to make sure it actually does what you want.

You said 30 rows, but I assume the data starts in AA2 and I find the number of rows by coming up from the bottom in column AA.  This assumes there will be data in column AA in the last row that needs to be process.  If that is not the case, then the macro would need to be altered.  

Perhaps replace this line

Set r = Range("AA2", Cells(Rows.Count, "AA").End(xlUp))

with

Set r = Range("AA2:AA31")   

or set it to the range you actually want to process.

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