VBA - Formatting Exported Data for Unpivot

ckp90c

New Member
Joined
Dec 22, 2016
Messages
11
Hi All - I just have a few issues on my VBA i'm creating for some macros. At the bottom I do have some of the code i've done for each part, and if they are successful yet or not.

I am using an in-house data unpivot Excel tool that requires tagging around your data and I am trying to format an OTP export automatically to get it ready for it.

I am happy to provide more context and even a dummied out sample of the file if needed. Thank you all so much!!!

---------------------------------------------------------------------------------------------------------

Part 1 - I need to add "#COL[C]" in the soonest row in column B

Part 2 - I need to add "#COL[A]" in the top row of the first column that doesn't contain header data

Part 3 - I need to add "#COL" in the cell right under "#COL[A]"

Part 4 - I need to add "#ROW" in every cell under "#COL" and will stop based on the last populated row in column B

Part 5 - I last need to add "#ROW" to the right of the populated "#COL[C]" from column B into each column on that same row, up until the last column BEFORE the column with #COL[A] and #COL, etc...technically it would stop at the second to last column.

---------------------------------------------------------------------------------------------------------


Part 1 - GOOD

Sub ColC()
Dim N As Long
N = Cells(Rows.Count, "B").End(xlUp).Row + 1
Cells(N, "B").Value = "#COL[C]"

End Sub


----------------------------------------------------------------------------------------------------------

Part 2 - GOOD

Sub ColA()


Dim N As Long
N = Cells(1, Columns.Count).End(xlToLeft).Column + 1
Cells(N) = "#COL[A]"

End Sub


---------------------------------------------------------------------------------------------------------
Part 3 - BAD

Sub ColB()


Dim N As Long
N = Cells(2, Columns.Count).End(xlToLeft).Column + 1
Cells(N) = "#COL"

End Sub
---------------------------------------------------------------------------------------------------------
Part 4 - Don't know how to do it
---------------------------------------------------------------------------------------------------------
Part 5 - Don't know how to do it
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
ckp90c,

Writing five separate macros seems a bit inefficient, but since that's what you requested, you might consider the following three parts...

Code:
Sub Part3()
Dim found As Range
Set found = ActiveSheet.Rows(1).Find(What:="#COL[A]", After:=Cells(1, 1), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
If Not found Is Nothing Then _
    found.Offset(1, 0).Value = "#COL[B]"
End Sub

Sub Part4()
Dim LastRow As Long
Dim found As Range
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set found = ActiveSheet.Rows(2).Find(What:="#COL[B]", After:=Cells(2, 1), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
If Not found Is Nothing Then _
    ActiveSheet.Range(found.Offset(1, 0), Cells(LastRow, found.Column)).Value = "#ROW"
End Sub

Sub Part5()
Dim LastCol As Long
Dim found As Range
LastCol = ActiveSheet.Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
Set found = ActiveSheet.Cells.Find(What:="#COL[C]", After:=Cells(1, 1), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
If Not found Is Nothing Then _
    ActiveSheet.Range(found.Offset(0, 1), Cells(found.Row, LastCol - 1)).Value = "#ROW"
End Sub

Cheers,

tonyyy
 
Upvote 0
One approach to a single macro...

Code:
Sub AddStrings()
Dim LastRow As Long, LastCol As Long
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column

Cells(LastRow + 1, 2).Value = "#COL[C]"
Cells(1, LastCol + 1).Value = "#COL[A]"
Cells(2, LastCol + 1).Value = "#COL[B]"
Range(Cells(3, LastCol + 1), Cells(LastRow, LastCol + 1)).Value = "#ROW"
Range(Cells(LastRow + 1, 3), Cells(LastRow + 1, LastCol)).Value = "#ROW"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,492
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top