Question

Copy and paste a column based on a cell's formatting (VBA)

I want to write some code that allows me to copy a column based on the formatting of a cell.  Here is an example of what I am trying to do.

Starting at cell G6, I want the code to continue searching Row G until it comes to a cell that does NOT have a border on the right side of the cell.  When this cell is found, I want to copy the column to the left of this cell and paste the entire column in the row where this cell is located.  So for example if I start at G6, I will find cell K6 that does not have a border on the right side of the cell.  In this scenario I would want Column J to be copied and then pasted into Column K.  How can I do this?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-12-13 at 11:39:52ID23021931
Tags

Microsoft

,

Excel

,

2003

,

VBA Code

Topics

Microsoft Excel Spreadsheet Software

,

VB Controls

,

VB Script

Participating Experts
2
Points
500
Comments
16

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Cell borders
    I am using Front Page 98 and I am importing an Excel 97 file into it. When the table is created it looks fine in te design mode, but when I go to preview it using a web browser the empty cells have no borders. How can I have borders in empty cells?
  2. border in cell
    How do I make a table cell border colour? I tried .abc { border width: 1px; border-color: #000000; } <TD CLASS="abc">Testing</TD> but it doesn't work. :(
  3. Cell borders
    I have an access database that creates reports using VBA and the output is in Excel. I would like to shade the borders of the cells within the report from the VBA code. what is the correct syntax for this?
  4. VBA set Cell Borders
    hi I create Excel Spreadsheets from VBA Code in Access. Although it will let me format near everything in the Excel spreadsheet, I am having trouble simply setting cell borders, the code would work in Excel, but does not from Access. Does anyone know how I can remedy this. I...
  5. borders
    Hello: I need vba code to draw the borders around the cells of first row? thanks
  6. Cell borders
    I pasted some text from a list on a website into Excel and after pasting it the borders around the individual cells disappeared. I would prefer not to use the gridline function as the borders it creates are too dark. Is there someway to get the original Excel borders around t...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: ahammarPosted on 2007-12-13 at 11:56:51ID: 20467072

I think this will do that for you...

:-)
Albert


Sub Test()
Dim r As Range
    Set r = Range("G6")
        Do Until r.Borders(xlEdgeRight).LineStyle = xlNone
            Set r = r.Offset(0, 1)
        Loop
    MsgBox r.Address
    
    r.Offset(0, -1).EntireColumn.Copy r.EntireColumn
End Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: ahammarPosted on 2007-12-13 at 11:57:36ID: 20467079

oopss...take out the message box...that was for testing..

:-)
Albert

 

by: exciter_grPosted on 2007-12-13 at 12:06:42ID: 20467159

Try this code:

Sub CheckCells()

Dim RowToSearch, StartingCol, EndingCol As Long

RowToSearch = 6
StartingCol = 7
EndingCol = 22

For i = StartingCol To EndingCol
Cells(RowToSearch, i).Select
If Cells(RowToSearch, i).Borders(xlEdgeRight).LineStyle = xlNone Then
Columns(i + 1).Select
Selection.Copy
Cells(1, i).Select
ActiveSheet.Paste
End If
Next i
   
End Sub


This code will start searching to the line that you want (just input your desired row number instead of 6 in the 'RowToSearch = 6' line), from column G (also, you can change this in the line 'StartingCol = 7') to the column you specify (in the above code it is column V, again you can change this to any column number you prefer). When a cell with no right border is found, then it copies the column in the left of the cell and paste it on the cell's column.

Hope it helps

Exciter.

 

by: everythingbutthemooPosted on 2007-12-13 at 12:13:33ID: 20467220

I like exciter's code better, but when I execute it, nothing pastes as the worksheet looks just as it did before the code ran, except Column V is selected.  Any thoughts?

 

by: exciter_grPosted on 2007-12-13 at 12:22:12ID: 20467283

everythingbutthemoo:
The only way this can happen is if no cell in the specific row and column range you specified in my code has right border.

If you are sure that at least a cell in the range has right border, then could you please upload a sample of your file in a free hosting site so i can check?

Best,
Exciter

 

by: everythingbutthemooPosted on 2007-12-13 at 12:28:54ID: 20467335

Here's the file:  http://www.ee-stuff.com/Expert/Upload/getFile.php?fid=6150

Let me know what you see.

Thanks!

 

by: ahammarPosted on 2007-12-13 at 12:42:08ID: 20467473

Here is another one...very simple...it prompts you on what cell to start on (enter it as g6 or whatever), then what column to end on (enter it as k or whatever column you want it to end on:

It runs until it reaches the column you told it to end on

Maybe it will help..

:-)
Albert

Sub Test()
    Dim r As Range
    Dim StartRange As String
    Dim ColsToCheck As String
    Dim StartCol As Long
    
        StartRange = InputBox("What cell do you want to start on?")
            If StartRange = "" Then Exit Sub
        ColsToCheck = InputBox("What column do you want to end on?")
            If ColsToCheck = "" Then Exit Sub
            
    Set r = Range(StartRange)
     
    Do Until r.Column > Columns(ColsToCheck).Column
        Set r = r.Offset(0, 1)
        If r.Borders(xlEdgeRight).LineStyle = xlNone Then
            r.Offset(0, -1).EntireColumn.Copy r.EntireColumn
        End If
    Loop
End Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:

Select allOpen in new window

 

by: everythingbutthemooPosted on 2007-12-13 at 12:45:30ID: 20467499

I'm not interested in inputing any information into a Box at this time.  Thank you anyway.

 

by: exciter_grPosted on 2007-12-13 at 12:45:38ID: 20467501

everythingbutthemoo:
It worked fine for me.

The code searched from cell G6 to V6 (RowToSearch = 6, StartingCol = 7, EndingCol = 22) and when it reached cell Q6 it found that it has no right border. It copied column P and pasted it into Column Q. Then it continued copying and pasting columns until it reached cell V6 and stopped. Note that all columns had formatting only and no actual data, because copying started from Column P which is empty.

Let me know if you want me to upload a video of the macro running.

Exciter

 

by: everythingbutthemooPosted on 2007-12-13 at 12:49:24ID: 20467538

A video would be very helpful.  I cannot understand why the code does not copy P and paste it into Q like you say when I run it.  By the way, once the column gets pasted, I do not want any more columns to be pasted.

Thanks!

 

by: ahammarPosted on 2007-12-13 at 12:57:28ID: 20467615

everythingbutthemoo:
Ok...no problem...I'll let Exciter take over from here...:-)

ps...If either of you upload very many files to EE...here is an automated app that simplifies it a gread deal in case you want to check it out.  It will zip the file for you...log you on, upload the file, then paste the direct link directly in this window (if this window is the last active window)

http://www.experts-exchange.com/Community_Support/Input/Q_22990788.html

:-)
Albert

 

by: exciter_grPosted on 2007-12-13 at 13:08:27ID: 20467703

everythingbutthemoo:
I have uploaded a video in url: http://www.exciter.gr/exciter/temp/untitled.html (it is flash streaming format, 1Mb).

if you want to stop code execution after 1st paste, add the line 'Exit Sub' before the 'End If' line.

Sending complete code below.

Exciter.

Sub CheckCells()
 
Dim RowToSearch, StartingCol, EndingCol As Long
 
RowToSearch = 6
StartingCol = 7
EndingCol = 22
 
For i = StartingCol To EndingCol
Cells(RowToSearch, i).Select
If Cells(RowToSearch, i).Borders(xlEdgeRight).LineStyle = xlNone Then
Columns(i - 1).Select
Selection.Copy
Cells(1, i).Select
ActiveSheet.Paste
Exit Sub
End If
Next i
    
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:

Select allOpen in new window

 

by: exciter_grPosted on 2007-12-13 at 13:16:58ID: 20467773

ahammar:
Great work with the uploader. Thank you very much!

Best
Exciter

 

by: everythingbutthemooPosted on 2007-12-13 at 13:58:42ID: 31414840

Great help!  Thank you so much!

 

by: everythingbutthemooPosted on 2007-12-13 at 14:01:14ID: 20468123

Thank you so much!  That really helped.  If I wanted to PASTE SPECIAL Formats, ColumnWidths, and Fomulas, how would I do that?  Here is the code I am using but for some reason I'm getting an error...

Sub AddNewPredictor()
'
' AddNewPredictor Macro
' Macro recorded 12/13/2007 by Christopher Vesta
'
' Keyboard Shortcut: Ctrl+Shift+A
'
    ActiveSheet.Unprotect ("DBDS11")
   
    Dim RowToSearch, StartingCol, EndingCol As Long
 
    RowToSearch = 6
    StartingCol = 7
    EndingCol = 22
 
    For i = StartingCol To EndingCol
        Cells(RowToSearch, i).Select
        If Cells(RowToSearch, i).Borders(xlEdgeRight).LineStyle = xlNone Then
            Columns(i - 1).Select
            Selection.Copy
            Cells(1, i).Select
            ActiveSheet.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
            ActiveSheet.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
            ActiveSheet.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
            Cells(5, i).Select
            Application.SendKeys ("{ESC}"), True
            ActiveSheet.Protect Password:="DBDS11", DrawingObjects:=True, Contents:=True, Scenarios:=True
            Exit Sub
        End If
    Next i
   
       
    ActiveSheet.Protect Password:="DBDS11", DrawingObjects:=True, Contents:=True, Scenarios:=True
   
End Sub

Any last words of wisdom would be very much appreciated!

Thanks again!

 

by: exciter_grPosted on 2007-12-13 at 14:21:13ID: 20468268

everythingbutthemoo:
I am glad I helped to solve a problem. Thank you for accepting my solution.

About your last question:
Try using Selection.PasteSpecial instead of Activesheet.PasteSpecial

See code below.

Best,
Exciter.

Sub AddNewPredictor()
'
' AddNewPredictor Macro
' Macro recorded 12/13/2007 by Christopher Vesta
'
' Keyboard Shortcut: Ctrl+Shift+A
'
    ActiveSheet.Unprotect ("DBDS11")
    
    Dim RowToSearch, StartingCol, EndingCol As Long
 
    RowToSearch = 6
    StartingCol = 7
    EndingCol = 22
 
    For i = StartingCol To EndingCol
        Cells(RowToSearch, i).Select
        If Cells(RowToSearch, i).Borders(xlEdgeRight).LineStyle = xlNone Then
            Columns(i - 1).Select
            Selection.Copy
            Cells(1, i).Select
            Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
            Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
            Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
            Cells(5, i).Select
            Application.SendKeys ("{ESC}"), True
            ActiveSheet.Protect Password:="DBDS11", DrawingObjects:=True, Contents:=True, Scenarios:=True
            Exit Sub
        End If
    Next i
    
        
    ActiveSheet.Protect Password:="DBDS11", DrawingObjects:=True, Contents:=True, Scenarios:=True
    
End Sub
                                              
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:

Select allOpen in new window

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...