0

I am trying to write a macro to for a rule.

A   B   C   D   E

X   1   RT  YY  SOW  
D   3   FT  GH  TOW  
F   4   FG  TY  

Rule: Filter on column E one by one and copy the value in column A and C. The value in column E is not predetermined. So I can have an excel with different values in column E and hence I cannot hardcode my filter criteria.

I want to filter on first value (sow in example) then whatever rows I get, I copy column E value in column A and C for those rows. Then I filter on second value (tow in example) and copy it in column A and C.

The result should look like this

A     B    C   D   E

SOW   1   SOW  YY  SOW  
TOW   3   TOW  GH  TOW  
F     4   FG   TY  

Note: Now of course there will be hundreds of rows and just not one row. and hence the need for a macro.

Clarification : I want to take everything but blanks, from column E and copy into respective rows in column A and C. But since column E does not have strict defined values, I am not able to hard code my filter criteria. Column E may have different number of unique values and combination. And I would like to take the values as is without the blanks and copy into columns A and C.

8
  • After copying columns A & C .. Where would you like to paste your data? Commented Oct 30, 2018 at 12:57
  • I want to copy the value from Column E into column A and C Commented Oct 30, 2018 at 12:59
  • 1
    Sill not clear. Can you provide snapshot with before and after? Commented Oct 30, 2018 at 13:00
  • 1
    It feels like you just can copy column E and replace it with column A and C. Doesn't need to filter to do that.... or what I'm missing? And what do you want to do when there are blank cells, like the last row in column E? Commented Oct 30, 2018 at 13:01
  • @Wizhi- has a point, why would you need to filter? just copy column E and paste to column A & C Commented Oct 30, 2018 at 13:06

1 Answer 1

2

If I understand your question correctly you only want to copy the values in Column E that is not empty. Therefore you want to filter it.

VBA Code:

Sub CopyNoneEmptyCells()

Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1") 'Define your worksheet name
Dim lrow As Long
Dim i As Long


lrow = ws.Cells(Rows.Count, 1).End(xlUp).Row 'Check last row in column A 'Find the last row in column A

For i = 2 To lrow 'Loop from row 2 until last row. I assume you have a header at row 1
    If ws.Cells(i, 5).Value <> "" Then 'If cell is not empty in column E, then
         ws.Cells(i, 1).Value =  ws.Cells(i, 5).Value 'Copy value from column E to Column A
         ws.Cells(i, 3).Value =  ws.Cells(i, 5).Value 'Copy value from column E to Column C
    End If
Next i 'Next row
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

yeah this is exactly what I was trying to do, but instead of a loop I was trying to do it through filtering. But this works too. Thanks
Both methods works fine (loop or filter) :), good you provided a result table!. That gave a lot of valuable information to us. Good luck with your project and welcome to SO :)
got it. :) Thanks for the help Hoping to learn a lot here :)
Almost... In the example, the last row has an empty cell in column E. Therefore if you copy that value it would overwrite valuable information in Column A and C (they would be blank). We only want to copy a cell value from column E if the current cell ws.Cells(i, 5).Value <> "" is non empty. Otherwise go to next row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.