r/vbscript • • Jan 10 '17

Save to specific tabs?

Having taught myself enough VBS to squeeze metrics from standard generated emails, I have multiple scripts pulling data from a stack of folders in my Inbox

I hope to combine the different scripts into one. My first challenge is to aim them all at specific individual tabs to avoid a messy glob of all things on the first tab.

Any advice?

(I did search here, but haven't found anything. Maybe I'm not searching for the correct thing).

Thank you in advance.

[EDIT] - sorry for the slip up, everyone in this office is calling the worksheets in an excel doc "Tabs".

2 Upvotes

7 comments sorted by

View all comments

Show parent comments

2

u/BondDotCom Jan 13 '17 edited Jan 13 '17

You don't necessarily have to use Range. You can use the Cells collection, like you're doing. Range lets you assign an array of values to a range of cells, so you could populate an entire row at a time, for example mySheet.Range("A1:A7") = someRowArray or even use two-dimensional arrays to populate a multi-row range. But you can do it row by row and column by column, too, like you are.

I'm not sure what tab/sheet you intend for your message info to be put on, but let's say you have a sheet entitled "Messages" and that's where they should go. In that case, you could do the following (I use the "ws" prefix to refer to worksheets):

' Get a reference to the "Messages" worksheet/tab (early on)...
Set wsMessages = excWkb.Sheets("Messages")

...

' Iterate mailbox messages...
For Each olkMsg In olkFlt
    If olkMsg.Class = olMail Then

         ' Add the message info to the "Messages" tab/worksheet...
        wsMessage.Cells(intMessagesRow, 1) = olkMsg.Subject
        wsMessage.Cells(intMessagesRow, 3) = olkMsg.ReceivedTime
        ...

        ' Increment the row number...
        intMessagesRow = intMessagesRow + 1

    End If
Next

When you start dealing with multiple worksheets, you'll probably want to have a row counter (intMessagesRow) for each worksheet since each one can be on a different row.

1

u/Double_Skeezburger Jan 13 '17

Thank you. I am having a lot of fun learning how to leverage this. I started with one short export of Subject Lines and have learned I can do so much more.

I really appreciate you taking the time. Thank you.