Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store data into an array

Status
Not open for further replies.

Xsi

Programmer
May 29, 2015
121
0
0
SE
Hello everyone,

I have made following code,

Code:
Dim UserNameArr()
Dim PwdArr()

Public sub ExcelDataToArr()

TestData=Environment.value("TestPath")&"Testdata\DB.xlsx"

Datatable.Importsheet (TestData),"TestDataDB",1

intRowcount=DataTable.GetSheet(1).GetRowCount
For iLoop = 1 To intRowcount

  DataTable.GetSheet(1).SetCurrentRow iLoop 
 
   If not DataTable.value("UserName") = "" Then
         Username= (DataTable.value("UserName",1))
             
        
        ' MsgBox(Username)
         
         'Store into an array
         ReDim UserNameArr()
         UserNameArr(iLoop)=Username
                
End If

   If not DataTable.value("Password") = "" Then
         Password= (DataTable.value("Password",1))
         
         ' MsgBox(0)
          
          'Store into an array
  
End If

Next
End Sub


I don't success to store to following arrays: Dim UserNameArr(),Dim PwdArr()

Could someone help me out?

Thank you in advance
 
I don't success to store to following arrays: Dim UserNameArr(),Dim PwdArr()

Assuming the above should translate to "Cannot write data to the array cells"

What code have you tried, because nowhere in the code you have provided do you actually give the arrays any actual dimension.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
This is my atempt:

Code:
Dim UserNameArr()
Dim PwdArr()


ExcelDataToArr()
ReadTestdata()


Public sub ExcelDataToArr()


TestData=Environment.value("TestPath")&"Testdata\DB.xlsx"
Datatable.Importsheet (TestData),"TestDataDB",1

intRowcount=DataTable.GetSheet(1).GetRowCount
For iLoop = 1 To intRowcount

  DataTable.GetSheet(1).SetCurrentRow iLoop 
 If not DataTable.value("UserName") = "" Then
         Username= (DataTable.value("UserName",1))               
         ReDim  UserNameArr(intRowCount)
         
         If not Username ="" Then
         	 UserNameArr(iLoop)=Username   
         End If
                        
	End If       

	
'	If not DataTable.value("Password") = "" Then
'         Password= (DataTable.value("Password",1))               
'         ReDim preserve PwdArr(intRowCount)
'         
'         If not Password ="" Then
'         	 PwdArr(iLoop)=Password         	
'         End If
'                        
'	End If
	
	

Next

End Sub
Public sub ReadTestdata()

For each uname in UserNameArr
msgbox(uname)
	
Next

End Sub

Though its contains empty cell I don't know how to solve that.


I have tried:

atempt 1
Code:
If not DataTable.value("UserName") = "" Then
          Username= (DataTable.value("UserName",1))               
          ReDim preserve UserNameArr(intRowCount)
          
          If not Username ="" Then
               UserNameArr(iLoop)=Username             
          End If
                         
     End If

atempt 2



Code:
If not DataTable.value("UserName") = "" Then
          Username= (DataTable.value("UserName",1))               
          ReDim preserve UserNameArr(intRowCount)
          
          If not Username ="" Then
               UserNameArr(iLoop)=Username             
          End If
                         
     End If

 
Okay, whenever you re-dimension an array with data already inserted you need to include the Preserve command ;

Code:
ReDim Preserve ArrayName(NewSize)

The addition of the Preserve keyword extends the existing array instead of re-initialising it with empty elements.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top