473,608 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to loop through worksheets and returning rows based on column value

2 New Member
This feels simple. But not enough coffee I suppose. Cant get it to work.

I have about ten worksheets in a workbook. The first worksheet is where I want ROWS returned to from the other 9 worksheets based on values in the "C" column of each worksheet. The "C" column is a priority column. So in other words, if PRIORITY is 1 then I want those rows returned first from all worksheets, then iterate through and return all PRIORITY 2's all the way down to PRIORITY level 5.

So in the end my first worksheet should be an aggregate of all rows from all ten worksheets, but listed by priority 1 through 5.

Any help would be GREATLY appreciated!! Thanks
Sep 20 '10 #1
2 2144
MikeTheBike
639 Recognized Expert Contributor
Hi

You don't say where or how you are running the code, but the routine below is written a a code module and is called fron a button on the target sheet
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Sub CopyWithPriority()
  4.     On Error GoTo ErrorHandler
  5.  
  6.     Const PriorityColumn As Integer = 3 'COLUMN C
  7.     Dim TargetSht As Worksheet
  8.     Dim sht As Worksheet
  9.     Dim iLastRow As Long
  10.     Dim i As Long
  11.     Dim iPriority As Integer
  12.     Dim iTargetRow As Long
  13.  
  14.     Set TargetSht = ActiveSheet 'IT IS ASSUMED THAT THE TARGET SHEET IS SELECTED BEFORE CODE IS RUN (FROM A BUTTON ON THE SHEET!!)
  15.     TargetSht.Cells.ClearContents   'MAY NOT BE NECESSAY BUT IT IS FOR TESTING!!
  16.  
  17.     Application.ScreenUpdating = False 'HIDE ACTIVITY
  18.     iTargetRow = 2   'SET START TARGET ROW
  19.     For iPriority = 1 To 6   'CYCLE THROUGH PRIORITY NUMBERS
  20.         For Each sht In ActiveWorkbook.Sheets   'LOOP THROUGHT SHEETS
  21.             If sht.Name <> TargetSht.Name Then   'IGNORE TARGET SHEET
  22.                 sht.Select  'SELECT SOURCE SHEET (TO FIND LAST ROW)
  23.                 iLastRow = ActiveCell.SpecialCells(xlLastCell).Row   'GET SHEET LAST ROW
  24.                 TargetSht.Select   'GO BACK TO TARGET SHEET
  25.                 For i = 1 To iLastRow   'CYCLE DOWN ROWS
  26.                     If sht.Cells(i, PriorityColumn) = iPriority Then  'COPY ROW IF CORRECT PRIORITY
  27.                         sht.Rows(i).Copy   'COPY ROW FROM SOURCE
  28.                         Range(Cells(iTargetRow, 1).Address).PasteSpecial xlPasteAll   'PAST IN TARGRET SHEET
  29.                         iTargetRow = iTargetRow + 1   'UPDATE TARGET ROW
  30.                     End If
  31.                 Next i
  32.             End If
  33.         Next sht
  34.     Next iPriority
  35.     Application.ScreenUpdating = True
  36.     Exit Sub
  37.  
  38. ErrorHandler:
  39.     Application.ScreenUpdating = True
  40.     MsgBox "An unexpected error has accured,  Number " & Err.Number & vbLf & Err.Description
  41. End Sub
It will not take much moding to run it from somewhere else.

HTH


MTB
Sep 22 '10 #2
patrick keady
2 New Member
Wow. Thank you for your time and effort, I am just going to run it everytime the workbook opens, not on a button, sorry didnt mention that part. But Thank you so much for helping!!! Great job.
Sep 22 '10 #3

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

Similar topics

0
2643
by: Amber | last post by:
There are times when you will need to highlight or otherwise modify the contents of a particular DataGrid row-column value based upon the value in the column. In this example we will select the CompanyName, ContactName, and ContactTitle columns from the Customers table in the Northwind database. Whenever the value of ContactTitle equals "Owner" we will change the font color to red. We can do this by using an ItemTemplate where we have...
2
1867
by: Renato Cramer | last post by:
Hello All, There is data of several enterprises (ours clients) in a single database. All tables have a column on primary key what identify the enterprise called id_enterprise. My objective is restrict users access on rows depending on value of the column id_enterprise. That is, an user can access data only of his enterprise. I don't want use where clause.
1
1446
by: kcakebread | last post by:
Using VB 2005, I have a strongly typed dataset (from a .xsd file in my project). When I load data into this dataset, the immediate window shows: ?ds.TableX(0).IstheFieldNull True So, dutifully, I give it a value: ds.TableX(0).theField = "testabc123"
7
2819
by: tasmontique | last post by:
I have an access table that outputs to excel using a query . However what I am trying to do is under the arrival date column specify a criteria based on the Datepart function that only displays output based on specific day of week. I want query to check the stored date in the arrival column and based on that date that is examined by the date part function only return rows based on day of week .Here is my sql. SELECT...
0
1742
by: sk27ahmed | last post by:
Hi Any one can show me how to access datagridview column value on column checked unchecked. I create one column in datagridview of type checkbox,and on button click i write code to select all rows in the datagridview. The code for select all is for (int i = 0; i < dataGridView1.Rows.Count; i++) { dataGridView1.Rows.Cells.Value = true; }
3
1507
by: hauschild | last post by:
Guys, I am looping thru a dataset and need to update rows' columns based on the ColumnName value. I get this far but I'm unsure of how to update that actual columns value with the new value. Code follows: string sDate = (Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern.ToString()); string BritishDate; string USDate; if (sDate != "MM/dd/yyyy")
2
2527
by: harichinna | last post by:
Hi to all, My name is hari, iam trying to delete the column value of spread sheet using c sharp, iam trying to get the value and compare, when comparing it showing some errors, can any one help me, to slove this issue i pasted code below Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook ObjWorkBook=null; ...
3
5014
by: Vinda | last post by:
Hi Bytes, Using a previous question as a base Access 2000 Inserting multiple rows based on a date range. I also wanted to insert multiple rows into a table according to a date range supplied by a user (eg txtRDateStart & txtRDateEnd). The script was envisaged to be able to also gather a time and text description that would be repeated within each row. For example: The user would enter... Start Date: 13/03/2010 End Date: 17/03/2010...
3
11150
by: sri devi | last post by:
Hi Frnz, Kindly, help me. How to compare a single row with a whole column value in C3 For Ex: ROW COLUMN ---------------------- SRI DEVI BABU SRI I want to know the first row value "SRI" is present in that column or not. If its present no work, if its not present the row value will be my answer......
0
8488
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8160
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6826
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3972
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4036
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2479
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1339
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.