473,396 Members | 1,895 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,396 software developers and data experts.

Assign COUNTIF result a variable

5
I am working with VBA in ExcelUltimately I am trying to take a huge amount of data (9500-10000 rows) and do a search for text including the words "Violated Door". I then want to take the rows (which contain Date in one cell, time in another, building name, room, etc) which contain that text in the description, copy and paste them into another workbook in Excel. I have created a program to do a simple "find, copy, paste, return, find next, copy, paste, return, etc," but I wanted to loop the program. So, I wanted to do a COUNTIF and find out how many rows contain the words "Violated Door", and then assign that number to a variable. I then want to take that variable and loop it that many times using a Do...Until and have it stop once it has reached that number.

So, I'm looking for help with 2 things:

1) how do I assign that COUNTIF result to a variable,
and 2) What will my Do Until line look like? I was thinking something like:

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer = 0
  2. Do Until i = [variable]
  3. Loop
  4.  
Any help would be awesome. Thanks!
Mar 26 '08 #1
2 2184
kadghar
1,295 Expert 1GB
Any help would be awesome. Thanks!
No wonder!

I am working with VBA in ExcelUltimately I am trying to take a huge amount of data (9500-10000 rows) and do a search for text including the words "Violated Door". I then want to take the rows (which contain Date in one cell, time in another, building name, room, etc) which contain that text in the description, copy and paste them into another workbook in Excel. I have created a program to do a simple "find, copy, paste, return, find next, copy, paste, return, etc," but I wanted to loop the program. So, I wanted to do a COUNTIF and find out how many rows contain the words "Violated Door", and then assign that number to a variable. I then want to take that variable and loop it that many times using a Do...Until and have it stop once it has reached that number.

So, I'm looking for help with 2 things:

1) how do I assign that COUNTIF result to a variable,
and 2) What will my Do Until line look like? I was thinking something like:

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer = 0
  2. Do Until i = [variable]
  3. Loop
  4.  
well, first of all dont worry 10,000 rows is not that HUGE, so it'll be a simple task.

Seems like something interesting to do:

Lets say in column K is the description (where we'll search "Violated Door", no case sensitive ^.^ )

I'll also asume your table starts in Column A and in Row 1, and you dont have any blank spaces in Column A or in Row 1

I'll make N = 3. Please note you can also change the initial Row and Column.

Expand|Select|Wrap|Line Numbers
  1. Sub ViolatedDoor()
  2. dim a
  3. dim b(), c()
  4. dim N as long, StCol as long, StRow as long
  5. dim i as long, j as long, k as long
  6. N = 3: StCol = 1: StRow = 1
  7. a= range(cells(strow,stcol).end(-4121), cells(strow,stcol).end(-4161))
  8. redim b(1 to ubound(a,2), 1 to 1): j=1
  9. for i = 1 to ubound(a)
  10.     if instr(ucase(a(i,N)),"VIOLATED DOOR") <> 0 then
  11.         for k = 1 to ubound(a,2)
  12.             b(k,j) = a(i,k)
  13.         next
  14.         j=j+1
  15.         redim preserve b(1 to ubound(a,2), 1 to j)
  16.     end if
  17. next
  18. redim c(1 to ubound(b,2), 1 to ubound(b))
  19. for i = 1 to ubound (b,2)
  20.     for j = 1 to ubound(b)
  21.         c(i,j ) = b(j,i)
  22.     next
  23. next
  24. workbooks.add
  25. workbooks(workbooks.count).activate
  26. range(cells(1,1),cells(ubound(c), ubound(c,2))) = c
  27. end sub
Well, that'll give you a good idea. Hope it helps.
Mar 26 '08 #2
mguy27
5
That actually helped quite a bit. Thank you!

A co-worker came in, sat down, took 2 seconds to look at it with me, and suggested a filter macro, which entailed no coding (since I recorded it) and about 15 seconds to do. So, thank you again for your help! I'm sure I will use it again in the near future. =]
Mar 26 '08 #3

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

Similar topics

9
by: Mike | last post by:
Is there any way I can use a function to create a variable and assign a value to it? I have a Perl script that returns some LDAP information: sn=Shore givenname=Mike logintime=20041008153445Z...
25
by: Rim | last post by:
Hi, I have been thinking about how to overload the assign operation '='. In many cases, I wanted to provide users of my packages a natural interface to the extended built-in types I created for...
4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
2
by: Matt | last post by:
If I assign Java variable a to javascript variable x, it is fine. <% int a = 10; %> var x = <%= a %>; alert(x); But if I do the other way around, then it has 500 error. any ideas??
4
by: Jim via DotNetMonster.com | last post by:
Hi, How can I assign the result of a function to a variable. I need to get the result so that I can query the database again. What I'm trying is:...
6
by: david | last post by:
I try to use "for" loop to retrieve and assign values in web form. The code is in the following. But it can not be compiled. What I want to do is: txtQ1.Text =...
0
by: Hbalushi3 | last post by:
How you going to apply countif in VB Editor in Excel sheet: Example: I create Userfrom In Excel sheet. So i want to apply countif in this userfrom with textbox opetion Could you plase help...
4
by: =?Utf-8?B?R0c=?= | last post by:
Pseudo code Count if greater than value in a reference cell Cell C3 would be: =COUNTIF(A1:A8,">=$B$1")/8
4
by: jameswilkinsonfjs | last post by:
Hi All, Ok I have a table - it lists items with a unique reference code; lets say there are 4 items : Item RefCode 1 ABC1 2 ABC2 3 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.