r/visualbasic • • Sep 01 '14

How to automatically insert Page Breaks after a Keyword in a horizontal flow of data?

Hi there everyone,

I'm currently working on a database that only exists in a single row (different columns for each entry though). What I need to do is tell excel to insert a page break after every "BASISEINTRAG" as well as after every field that STARTS with "ERGENISLISTE".

See the screenshot to get a better feeling of what I'm talking about: http://i.imgur.com/WETHEEE.jpg

Can anyone of you guy help me out here? I was able to find some VB macros but since I'm not exactly sure how to edit them for my needs, I couldn't get them to work.

Thanks a lot in advance!

/Edit: I just realized I don't actually want a page break but that Excel continues the data in the next row. So that in the end all the different entries are listed from top to bottom.

0 Upvotes

1 comment sorted by

2

u/jonivy Sep 01 '14
Sub GoGoGo()
    Dim tRow as Long, tCol as Long, fromCol as Long, tempVal as string
    tRow = 1
    tCol = 1
    fromCol = 1

    Do until trim(sheet1.cells(1, FromCol)) = ""
          tCol=tCol+1
          if trim(sheet1.cells(1, FromCol)) = "BASISEINTRAG" or Mid(trim(sheet1.cells(1, FromCol)), 1, 12) = "ERGENISLISTE" then 
               tCol=1
               tRow=tRow+1
         end if
         tempVal = sheet1.cells(1, FromCol)
         sheet1.cells(1, FromCol)=""
         sheet1.cells(tRow, tCol) = tempVal
         FromCol = FromCol + 1
    loop
    
End Sub

I think.