473,378 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

excel VBA - search for cell values and place into array

Hi, I am currently having some difficulties with this sunroutine that I created. My 2 dimensional array is empty.

- This code is within my UserForm1

- this sub is suppose to first select the WorkSheets("English"). The sheet contains col A, col B & col C. The sub will goto each row, if there is a cell value that matches the search criteria then all the cell values of that particular row is put into my 2-dimensional array.

- my while loop does not end. Is there something wrong with my code ?

Any ideas would be greatly appreciated !

--------------------------------------------------------------------------

Sub getResult(x, y, z)
Dim str1 As String
Dim str2 As String
Dim str3 As String

Dim cell1 As String
Dim cell2 As String
Dim cell3 As String

Dim tempStr1 As String
Dim tempStr2 As String
Dim tempStr3 As String

Dim counter As Integer

ThisWorkbook.Worksheets("English").Activate


'create 2 dimensional array -- 100 rows & 3 columns / note: lowerbound is 0
Dim myarray(100, 3)

str1 = x
str2 = y
str3 = z
'--------- Checking code ---------
MsgBox "str1 = " + str1 + " and str2 = " + str2 + " and str3 = " + str3


'---- looping structure of myarray -----
'the loop will check every row in Sheet1(English) and compare
'whether row 1 (col A, col B, col C) matches str1, str2 str3.
'If there is match to any str1 or str2 or str3 then the cell values will be read and placed into myarray()

counter = 0
cell1 = counter + 1
cell2 = counter + 1
cell3 = counter + 1

While Worksheets("English").Range("A" + cell1 + "") <> Empty And Worksheets("English").Range("B" + cell2 + "") <> Empty And Worksheets("English").Range("A" + cell1 + "") <> Empty


tempStr1 = Worksheets("English").Range("A" + cell1 + "").Value
tempStr2 = Worksheets("English").Range("B" + cell2 + "").Value
tempStr3 = Worksheets("English").Range("C" + cell3 + "").Value

'--------- Checking code ---------
MsgBox "tempStr1 = " + tempStr1 + " and tempStr2 = " + tempStr2 + " and tempStr3 = " + tempStr3


If tempStr1 = str1 Or tempStr3 = str2 Or tempStr3 = str3 Then

myarray(counter, 0) = tempStr1
myarray(counter, 1) = tempStr2
myarray(counter, 2) = tempStr3

counter = counter + 1

End If

tempStr1 = vbNullString
tempStr2 = vbNullString
tempStr3 = vbNullString

Wend

Call showResult(myarray)

End Sub
Mar 6 '08 #1
1 12393
kadghar
1,295 Expert 1GB
Hi, I am currently having some difficulties with this sunroutine that I created. My 2 dimensional array is empty.
...
i didnt read your full question (sorry) but it seems to me that you're having troubles into reading your excel's cells into an array.
well, the easiest way is to use a Variant.

when you asign a Range to a variant, the variant doesnt change to a Variant/Range, but to a Variant/Array (that's so cool)

something like this will do

Expand|Select|Wrap|Line Numbers
  1. dim a 
  2. a = range(cells(1,1), cells(5,5))
for example.

or if you want to do it with some specific cells, something like this will do

Expand|Select|Wrap|Line Numbers
  1. dim Arr1(1 to 10) as String
  2. arr1(1) = cells(1,1)
  3. arr1(2) = cells(3.1)
  4. 'and so on
  5.  
  6. 'or you can make them from different worksheets:
  7.  
  8. arr1(3) = worksheets("sheet1").cells(1,1)
HTH
Mar 6 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
6
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
3
by: Carlos Magalhaes | last post by:
Hey All, I am doing some excel automation using the excel COM. I can do most of the functions and its working well until I come across a formula. I can run a formula and insert the formula...
5
by: Lee | last post by:
Hi All, My problem is trying to figure out how to get the numbers from a range of cells in Excel into an array (double) in a VB.NET program, do something with the array, and then send it back to...
18
by: Frank M. Walter | last post by:
Hello, I have made an small AddIn with udf for excel 2003. I use vs2003. The point of view is the function __T() I call it in excel sheet writing =__T() I am not able to set a value to a...
3
by: implicate_order | last post by:
Greetings, I'm new to python and am in the process of writing a script to parse some CSV data, spread it across multiple Excel worksheets and then generate charts. I searched the internet to...
3
by: toffee | last post by:
Hi all, I got a pre-formatted spreadsheet. would it be possible using js to copy the data from a table on the current webpage, open the spreadsheet and paste the content ? if so, anyone got any...
5
kadghar
by: kadghar | last post by:
Most of the times VBA is used with variables. Objects (such as worksheets, cells or databases) are only used when we read their properties (value, formula, font...) or we use a method (save,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.