0

I have the following:
enter image description here


I am using @BigBobby's answer to achieve this:

Sub MoveData()
Range("H5:H99").ClearContents
START_ROW = 5
START_COL = 1
STEP_COL = 2
OUTPUT_ROW = 5
OUTPUT_COL = 8
Limit_Col = 9

Row = START_ROW
Col = START_COL
Out_Row = OUTPUT_ROW
While Col < Limit_Col
    While Cells(Row, Col).Value <> ""
        Cells(Out_Row, OUTPUT_COL).Value = Cells(Row, Col).Value
        Out_Row = Out_Row + 1
        Row = Row + 1
    Wend
    Row = START_ROW
    Col = Col + STEP_COL
Wend
End Sub


But as you see, I expect to get those values which appear after a blank cell in the columns. But this code fails to pull those cells highlighted in yellow.
How to modify this code to pull all of the data which may appear after one or several blank cells?

1
  • You're getting the error because your 2nd While loop moves down the column until it finds a blank, once it finds one it moves across 2 columns. I'd suggest you just need to add in a line which identifies the last row in the column which has data in it. Commented May 20, 2015 at 1:47

2 Answers 2

2

The previous answer is close, but it will also copy all of the blank spaces. This code should do what you need:

Sub MoveData()

START_ROW = 5
START_COL = 1
STEP_COL = 2
OUTPUT_ROW = 5
OUTPUT_COL = 10

Row = START_ROW
Col = START_COL
Out_Row = OUTPUT_ROW
While Col < OUTPUT_COL
    While Row < ActiveSheet.UsedRange.Rows.Count
        If Cells(Row, Col).Value <> "" Then
            Cells(Out_Row, OUTPUT_COL).Value = Cells(Row, Col).Value
            Out_Row = Out_Row + 1
        End If
        Row = Row + 1
    Wend
    Row = START_ROW
    Col = Col + STEP_COL
Wend
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

this is working. But it is taking so long to run the command. Why is that?
How much data do you have? It runs quickly here, but I am not testing with many rows and I have a very fast computer. One trick you can use to make macros go faster, is to include: Application.ScreenUpdating = False at the beginning of the macro, and then Application.ScreenUpdating = True at the end. That way, Excel isn't repainting the screen after every operation that affects a cell.
1

Adjust this code:

While Cells(Row, Col).Value <> ""
    Cells(Out_Row, OUTPUT_COL).Value = Cells(Row, Col).Value
    Out_Row = Out_Row + 1
    Row = Row + 1
Wend

For:

Do until row > Cells(65536, Col).End(xlUp).Row
    Cells(Out_Row, OUTPUT_COL).Value = Cells(Row, Col).Value
    Out_Row = Out_Row + 1
    Row = Row + 1
Loop

This essentially checks to see if the row has passed the last row with data, and if it has, it moves onto the next column.

Edit

To not copy across the blank cells use this:

    Do until row > Cells(65536, Col).End(xlUp).Row
        If Cells(Row, Col).Value <> "" then
            Cells(Out_Row, OUTPUT_COL).Value = Cells(Row, Col).Value
            Out_Row = Out_Row + 1
        End If
        Row = Row + 1
    Loop

3 Comments

this code runs faster that Bigbobby's answer, but it includes those blanks cells in the results as well. How to modify the code not to include those blank cells?
So it does! I've updated it to skip over the blank cells. I've not tested it but have a go and see if you can make it work if it doesn't already.
Although just seen Bigbobby's answer, I'm sure you can tinker with it to speed it up!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.