1

I am trying to take an excel file, look at the columns which have been filled and then split data in one excel row into various rows and in the process eliminate certain columns using VB. I think it will make more sense if you look at the images below.

Below is the image of what I am trying to split.

Before

Below is the image of how I want it split.

After

If someone can help me with it's VB code that would be great. I am new to VB and trying to learn.

1
  • 2
    Have you already attempted to write a piece of code that you could share? Commented Jan 12, 2017 at 22:53

1 Answer 1

1

How about the following code:

Sub Format()

lastrow = ActiveSheet.UsedRange.Rows.Count

For x = lastrow To 2 Step -1

    If Range("G" & x).Value <> "" Then
        Rows(x + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("A" & x + 1 & ":D" & x + 1).Value = Range("A" & x & ":D" & x).Value
        Range("E" & x + 1).Value = Range("G" & x).Value
        Range("G" & x).Value = ""
    End If

    If Range("F" & x).Value <> "" Then
        Rows(x + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("A" & x + 1 & ":D" & x + 1).Value = Range("A" & x & ":D" & x).Value
        Range("E" & x + 1).Value = Range("F" & x).Value
        Range("F" & x).Value = ""
    End If

Next x

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. This was very helpful. Appreciate it. I added code to delete the columns F and G so that there are no headers left when code is run.
@Gary No worries, glad to help. You can also just do do Range("F1:G1").Value = "" to clear the headers.

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.