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

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

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

✓ answered by MikeTheBike

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

2 2135
MikeTheBike
639 Expert 512MB
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
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
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...
2
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...
1
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,...
7
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...
0
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...
3
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. ...
2
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...
3
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...
3
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 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.