oopss...take out the message box...that was for testing..
:-)
Albert
Main Topics
Browse All TopicsI 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.
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.
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.
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.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
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.
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).Li
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.
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
Here's the file: http://www.ee-stuff.com/Ex
Let me know what you see.
Thanks!
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
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-exchang
:-)
Albert
everythingbutthemoo:
I have uploaded a video in url: http://www.exciter.gr/exci
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.
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).Li
Columns(i - 1).Select
Selection.Copy
Cells(1, i).Select
ActiveSheet.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.PasteSpecial Paste:=xlPasteColumnWidths
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!
Business Accounts
Answer for Membership
by: ahammarPosted on 2007-12-13 at 11:56:51ID: 20467072
I think this will do that for you...
:-)
Albert
Select allOpen in new window