x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 302

# conditional copy from column to another

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
vbaabv
• 3
• 2
1 Solution

Commented:
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
``````
0

Commented:
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
``````
0

Research ScientistAuthor Commented:
Hi Harry Lee,

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

Research ScientistAuthor Commented:
Nice job. Thank you very much.
0

Commented:
That's great! Glad I can help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.