4

Is there a command equivalent to readLine of Java in VBA. I want to use the text input in the immediate window and put it in a variable. Is it possible?

Emi
  • 484
  • 2
  • 16
  • 2
    [ReadLine](https://msdn.microsoft.com/en-us/library/office/gg278803.aspx) is a VBA method. –  Oct 05 '15 at 05:01
  • 3
    I'm pretty sure that the answer is no - you can't use the immediate window as an interactive console. What are you trying to do? Perhaps you could use a userform. – John Coleman Oct 05 '15 at 05:07
  • 1
    i want to use a loop to input data from user and put it in an array to calculate the total (something like that). If I use the form, i will have to create infinite no. of textboxes. So.. – Emi Oct 05 '15 at 05:13
  • Textboxes can be cleared. Input can be split. – John Coleman Oct 05 '15 at 05:21
  • I'm sorry I did not understand. – Emi Oct 05 '15 at 05:24
  • Read a line from where? From a file? From console? What do you mean by using text input in the immediate window? Immediate window is for debugging. – Shadow Oct 05 '15 at 05:27
  • What I meant was that you don't need an unlimited number of textboxes - you can clear them between successive reads. Also, you can have a user enter several numbers at one time, separated by spaces or commas and then use the split function to load them into an array. – John Coleman Oct 05 '15 at 05:36
  • @JohnColeman surely the whole point of the immediate window is that it is an interactive console. Whether you SHOULD use it as an interactive console is another matter. – Toby Allen Oct 08 '15 at 16:25
  • @TobyAllen You can't use it in the same way that you use a console for say a C program which is running in a command window. It isn't interactive in the sense that it doesn't accept keyboard input while a sub is actually running, so it can't be used as something like `stdin` (which is what OP wanted it to be used for). It *can* be used as a sort of light-weight REPL, and I do sometimes use it for that. – John Coleman Oct 08 '15 at 16:35

2 Answers2

4

You can't use the Immediate Window interactively. For one thing -- while a sub is running it won't accept any keyboard input. You can, however, use it to pass data to a sub or function when you invoke it, so in a sense you can "scrape" data that is there already. Something along these lines:

Sub AddNums(ParamArray nums())
    Dim total As Double
    Dim i As Long
    For i = 0 To UBound(nums)
        total = total + nums(i)
    Next i
    Debug.Print total
End Sub

For example:

enter image description here

Beyond that -- you could move the input-gathering phase to a VBScript script running in console mode, invoke it from VBA, and use either a file (which the script writes to) or perhaps the clipboard to get the data from the script after it is done running. This should be feasible, though it is probably better to find a more idiomatic (form-based) way to do it within VBA.

John Coleman
  • 51,337
  • 7
  • 54
  • 119
3

I am not quite sure why you need to write from Immediate Window to a variable at a runtime - is it some weird debugging practices?

Normally, if you need to take an input you end up with a form to interact with an user.

However, if you do need to write to a variable at a runtime consider the following:

Sub Main()

    Dim immediateInput As String
    Dim readImmediate As Boolean
    Do While (readImmediate = False)
        readImmediate = True
    Loop

End Sub

now, set a breakpoint at the readImmediate = true line and add immediateInput to the Watch. Bring up both the Immediate Window and Watches and run the macro.

When the runtime hits the breakpoint enter the below in the Immediate Window:

immediateInput = "hello world"

Now have a look in the Watches; your immediateInput's value should be "hello world".

enter image description here