Skip to main content
  1. About
  2. For Teams
Asked
Modified 8 years ago
Viewed 1k times
0

I have a VBA function that returns a lot of information about the computer it's running on. I'd like to format the output neatly, with the titles left-justified and the results all tabbed on a neat line to the right.

Here's what I thought would work, and this is the solution on many sites I've consulted:

AllInfo = _
    "Date/Time Opened:" & Space(33 - Len("Date/Time Opened:")) & Now() & vbCrLf & _
    "Filename:" & Space(33 - Len("Filename:")) & Application.ActiveWorkbook.Name & vbCrLf & _
    "Name in Cell B2:" & Space(33 - Len("Name in Cell B2:")) & Cells(2, 2).Value & vbCrLf & _

But the output, in both a message box and the body of an email, is jagged. The titles are all lined up on the left, of course, but the results aren't lined up nicely.

Can anyone see what I'm doing wrong?

4
  • VBA IsNot VB.NET - the tags tell you so
    Ňɏssa Pøngjǣrdenlarp
    –  Ňɏssa Pøngjǣrdenlarp
    2017-10-13 16:13:07 +00:00
    Commented Oct 13, 2017 at 16:13
  • 2
    Are you using a monospaced font?
    emed
    –  emed
    2017-10-13 16:21:06 +00:00
    Commented Oct 13, 2017 at 16:21
  • Len("Date/Time Opened:" & Now())
    user4039065
    –  user4039065
    2017-10-13 16:23:19 +00:00
    Commented Oct 13, 2017 at 16:23
  • 1
    use vbTab in your strings
    jsotola
    –  jsotola
    2017-10-13 16:31:37 +00:00
    Commented Oct 13, 2017 at 16:31

2 Answers 2

1

This works:

Sub djfsdf()
    allinfo = _
        "Date/Time Opened:" & Space(33 - Len("Date/Time Opened:")) & Now() & vbCrLf & _
        "Filename:" & Space(33 - Len("Filename:")) & Application.ActiveWorkbook.Name & vbCrLf & _
        "Name in Cell B2:" & Space(33 - Len("Name in Cell B2:")) & Cells(2, 2).Value
    Cells(3, 3) = allinfo


End Sub

with the proper font:

enter image description here

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

2 Comments

You nailed it, it was a font problem. I didn't think that could be an issue in a message box, but that's incorrect. Thanks.
@buckshot Getting a MsgBox to honor good spacing is a WAY cooler problem !
0

You are not including the string vars in the calculation for the number of spaces to add.

AllInfo = _
    "Date/Time Opened:" & Space(33 - Len("Date/Time Opened:" & Now())) & Now() & vbLf & _
    "Filename:" & Space(33 - Len("Filename:" & Application.ActiveWorkbook.Name)) & Application.ActiveWorkbook.Name & vbLf & _
    "Name in Cell B2:" & Space(33 - Len("Name in Cell B2:" & Cells(2, 2).Value)) & Cells(2, 2).Value & vbLf & _
    (etc)

I've also change your vbCRLF to just vbLF; that may or may not be correct.

1 Comment

Thanks Jeeped. It was a different problem, but vbLF does work.

Your Answer

Post as a guest

Required, but never shown

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.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.