Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Microsoft Support > Microsoft Office support
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Microsoft Office support MS Office support forum

Reply
 
LinkBack Thread Tools
Old 05-21-2008, 09:45 AM   #1 (permalink)
Registered User
 
Join Date: May 2008
Posts: 4
OS: XP


Excel Macro to tighten cells in rows

I've found this macro (here: http://en.allexperts.com/q/Excel-105...ty-cells-2.htm) which can tighten cells in rows (it moves all non-empty cells to the left, deleting blank cells). The problem is, it works on one row at a time. Is there a way to ask it to run through an entire worksheet, stopping when it reaches a blank cell in the first column? I don't want any cells moved up, just to the left.

Public Sub tighten_cells()

Dim row_or_col As Long
Dim xlR As Excel.Range
Dim xls As Excel.Worksheet
Dim xlr2 As Excel.Range

Dim i As Long, new_i As Long
Dim start_col As Long, start_row As Long, end_col As Long, end_row As Long

row_or_col = MsgBox("Press Yes for Row Tighten, No for Column Tighten, Cancel for Cancelling", vbYesNoCancel)

If row_or_col = vbCancel Then
Exit Sub
End If


Set xls = Excel.ActiveSheet
Set xlR = Excel.ActiveCell
Set xlr2 = xls.Cells.SpecialCells(xlCellTypeLastCell)

start_row = xlR.Row
start_col = xlR.Column

If (row_or_col = vbYes) Then ' Row Tighten -> move across columns
new_i = start_col
For i = start_col To xlr2.Column
If (xls.Cells(start_row, i) <> "") Then
If (new_i <> i) Then
xls.Cells(start_row, new_i) = xls.Cells(start_row, i)
xls.Cells(start_row, i) = ""
End If
new_i = new_i + 1
End If
Next i
End If

If (row_or_col = vbNo) Then ' Col Tighten -> move across rows
new_i = start_row
For i = start_row To xlr2.Row
If (xls.Cells(i, start_col) <> "") Then
If (new_i <> i) Then
xls.Cells(new_i, start_col) = xls.Cells(i, start_col)
xls.Cells(i, start_col) = ""
End If
new_i = new_i + 1
End If
Next i

End If



End Sub
Gatika is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 05-21-2008, 03:46 PM   #2 (permalink)
Registered User
 
Join Date: May 2008
Location: Baltimore, Maryland
Posts: 160
OS: Windows XP SP3


Re: Excel Macro to tighten cells in rows

Here’s a suggestion that may help you get started. You could create another subroutine that uses a loop to repeatedly perform the tighten_cells() subroutine, running through the worksheet as you’ve described. Here’s some sample code.
Code:
Sub entire()
    Dim current_row As Long
    Dim acell As Excel.Range
    
    Do
        Set acell = Excel.ActiveCell
        If IsEmpty(acell) Then
            Exit Do
        End If
        
        tighten_cells
        
        If acell.Row < Excel.Rows.Count Then
            acell.Offset(1, 0).Select
        End If
    Loop Until acell.Row = Excel.Rows.Count
End Sub
To run this, you would place the cell pointer on a row where the cell in the first column contains data, and it will run until it reaches a blank cell in the first column, or until it runs out of rows. Modify as necessary.
David M58 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-21-2008, 07:39 PM   #3 (permalink)
Registered User
 
Join Date: May 2008
Posts: 4
OS: XP


Re: Excel Macro to tighten cells in rows

Wunderbar! It prompts me to run at every row, but that's better than nothing. :) And there are only about 65 rows.

Thanks very much!
Gatika is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 12:29 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85