2

I'm new in VBA , so i would lile to store values from inputbox into array , i wrote the code below , then i tried define a array to store them , i got the issue with my VBA code , could you please help check ? any assist will be appreciated

Sub inpubox()
    Dim sn As String
    Dim em_ID As String
    Dim name As String
    Dim dept As String
    Dim hostname As String
    Dim loc As String
    sn = InputBox("Enter Laptop's Serial")
    em_ID = InputBox("Enter Colleague ID")
    name = InputBox("Enter Colleague Name")
    dept = InputBox("Enter Colleague department")
    loc = InputBox("Enter office location")
    'Xu li hostname
    hostname = "VN" & loc & sn
    sodong = Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1).row
    Dim arr6 As String
    arr6 =(sn,em_id,name,dept,hostname,loc)
           
End Sub

The error also attached array error

3
  • Try .. arr6 = split(sn&","&em_ID&","&name&","&dept&","&hostname&","&loc, ",") .. this will give you 0 based arr6 Commented Jun 27, 2021 at 15:10
  • 1
    You defined arr6 as a string. You need to define it as an array Commented Jun 27, 2021 at 15:10
  • Yes.. Dim arr6() As String will give you string type array. Commented Jun 27, 2021 at 15:19

3 Answers 3

3
Sub ShortCode()
    arr = Array(InputBox("Enter Laptop's Serial"), InputBox("Enter Colleague ID"), _
          InputBox("Enter Colleague Name"), InputBox("Enter Colleague department"), _
          InputBox("Enter office location"))
    Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, UBound(arr) + 1) = arr
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Nice! I like compact pieces of code... Voted up.
.......................................Super clever!
2

This direct entry in to the array through inputbox

Option Explicit
Sub inpubox()

Dim arr(1 To 6) As String, sodong as long
    arr(1) = InputBox("Enter Laptop's Serial")
    arr(2) = InputBox("Enter Colleague ID")
    arr(3) = InputBox("Enter Colleague Name")
    arr(4) = InputBox("Enter Colleague department")
    arr(6) = InputBox("Enter office location")
    
    arr(5) = "VN" & arr(6) & arr(1)

sodong = Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
Sheet1.Range("A" & sodong).Resize(1, 6).Value = arr

End Sub

Just to add to brilliant answer from Алексей Р below - - - We can use code like below to overcome the limitation of concatenation for arr(4) position (0 based).

Sub ShortCode()
arr = Array(InputBox("Serial"), InputBox("ID"), InputBox("Name"), _
      InputBox("Department"), "HostName", InputBox("location"))
arr(4) = "VN" & arr(5) & arr(0)
Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Offset(1).Resize(, UBound(arr) + 1) = arr
End Sub

Comments

1

We need to:

  1. define the array
  2. fill the array
  3. store the array in a row of cells

For example:

Sub inpubox()
    Dim sn As String
    Dim em_ID As String
    Dim name As String
    Dim dept As String
    Dim hostname As String
    Dim loc As String
    sn = InputBox("Enter Laptop's Serial")
    em_ID = InputBox("Enter Colleague ID")
    name = InputBox("Enter Colleague Name")
    dept = InputBox("Enter Colleague department")
    loc = InputBox("Enter office location")
    'Xu li hostname
    hostname = "VN" & loc & sn
    sodong = Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    
    Dim arr(1 To 6) As String
    arr(1) = sn
    arr(2) = em_ID
    arr(3) = name
    arr(4) = dept
    arr(5) = hostname
    arr(6) = loc
    
    Sheet1.Range(Cells(sodong, 1), Cells(sodong, 6)) = arr
           
End Sub

3 Comments

No need to even create variables .. we can directly input values in array as arr(1) = InputBox("Enter Laptop's Serial") and arr(6) = InputBox("Enter office location") and arr(5) = "VN" & arr(6) & arr(1)
@Naresh Good Comment You are completely correct!
thank you all for your help :) . here is the best place for me can learn alot about programing skills.

Your Answer

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.