Solved

conditional copy from column to another

Posted on 2014-02-21
5
289 Views
Last Modified: 2014-02-26
I have multiple Excel worksheets in a workbook in which I would like to, for each worksheet, inspect column C for the presence of only a hyphen "-".

  If there is only a hyphen present in the cell in Column C, then I want to copy what is in that same row in Column D into Column C.

  So, if row 10 of Column C has only a hyphen, then copy the contents of D10 into C10 and continue wherever there is only a hyphen in a cell in Column C.
0
Comment
Question by:vbaabv
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Harry Lee
ID: 39878541
Well, you can do it by VBA.

This will do it for one sheet.

Sub CheckColumnC()
Dim I As Long, WS As Worksheet
Set WS = ActiveSheet

For I = 1 To WS.Cells(Rows.Count, 3).End(xlUp).Row
    If WS.Cells(I, 3) = "-" Then
        WS.Cells(I, 3) = WS.Cells(I, 4)
    Else
        WS.Cells(I, 3) = WS.Cells(I, 3)
    End If
Next
End Sub

Open in new window

0
 
LVL 12

Accepted Solution

by:
Harry Lee earned 500 total points
ID: 39878550
This should takes care of all the worksheets in the workbook.

Sub CheckColumnCAllSheet()
Dim I As Long, WS As Worksheet
Set WB = ActiveWorkbook
For Each WS In Worksheets
    For I = 1 To WS.Cells(Rows.Count, 3).End(xlUp).Row
        If WS.Cells(I, 3) = "-" Then
            WS.Cells(I, 3) = WS.Cells(I, 4)
        Else
            WS.Cells(I, 3) = WS.Cells(I, 3)
        End If
    Next
Next
End Sub

Open in new window

0
 

Author Comment

by:vbaabv
ID: 39890617
Hi Harry Lee,

   Thank you very much ! This works very well. Sorry for the delay in getting  back to you.
0
 

Author Closing Comment

by:vbaabv
ID: 39890618
Nice job. Thank you very much.
0
 
LVL 12

Expert Comment

by:Harry Lee
ID: 39890713
That's great! Glad I can help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now