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

Using VBA to Conditionally Select Rows in Excel

1
I have an Excel workbook where data of a particular type is separated by "header" rows using colons to specify the data type:

:DataType1
FirstDataPoint
SecondDataPoint
ThirdDataPoint
:DataType2
FirstDataPoint
SecondDataPoint
ThirdDataPoint
:DataType3
FirstDataPoint
SecondDataPoint
ThirdDataPoint

The amount of data between the header rows is variable.

I would like to select all of the data between the first two header rows and copy/paste it to a new worksheet. Then copy all of the data between the second and third header rows and copy/paste it to a new worksheet, etc. (one worksheet for each data type).

I have tried many methods with filters, but my knowledge of VBA is limited.

Any thoughts/suggestions would be most appreciated.

WWUser
Jun 20 '07 #1
1 2308
kadghar
1,295 Expert 1GB
Hi

It's not a great thing, but it certainly helps.
Just a few comments: try to write the name of the headers exactly as they apear in your wsheet, and add another one after the last column.

So if you had 9 headers, now you'll have 10, and the last one will only help the program to know when to stop copy and pasting.

Good Luck

Kad

Expand|Select|Wrap|Line Numbers
  1. Sub copypaste()
  2. Dim i As Integer
  3. Dim j As Integer
  4. Dim k As Integer
  5. Dim numHdr As Integer
  6. Dim Headers() As String
  7.  
  8. numHdr = 4
  9.  
  10. ReDim Headers(1 To numHdr)
  11. Headers(1) = "header1"
  12. Headers(2) = "header2"
  13. Headers(3) = "header3"
  14. Headers(4) = "EndOfFile"
  15.  
  16. i = 1
  17. j = 1
  18.  
  19. While j <= numHdr
  20.     If Worksheets(1).Cells(1, i).Value = Headers(j) Then
  21.         j = j + 1
  22.         Worksheets.Add after:=Worksheets(Worksheets.Count)
  23.  
  24.         k = 1
  25.     End If
  26.     Worksheets(1).Columns(i).Copy
  27.     Worksheets(Worksheets.Count).Cells(1, k).Select
  28.     Worksheets(Worksheets.Count).Paste
  29.     i = i + 1
  30.     k = k + 1
  31. Wend
  32.  
  33. End Sub
Jun 21 '07 #2

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

Similar topics

1
by: barma16 | last post by:
I've hit a bit of a brick wall here, and could use some advice. I have an Access application whose output is a four-tab Excel spreadsheet where three of the four tabs are the result of database...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
3
by: Scott | last post by:
I want to search 5 or 6 very big excel spreadsheets looking for specific data held in them. The search would be using clients surnames and maybe postcodes to eventually find the correct person. ...
4
by: bubulle | last post by:
Hi, all. Here is my problem: Let's say i have table1 with columns a,b,c and table2 with cols x,y,z. Some of columns contain the same type of data from one table to the other, but others are...
2
by: Ch Pravin | last post by:
Hi All: I am having the following xml which i need to convert to excel using xslt. Please help me out. Afghanistan.xml <?xml version="1.0" encoding="utf-16"?> <Languages...
0
by: hne | last post by:
Hi friends, I have an application, written in C#, in which I am updating an Excel spreadsheet with data I pull from a sql database. When I try to select a cell in where I want to begin inserting my...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
2
by: | last post by:
I am using a datareader to cycle through a list of records one at a time. I frequently get connection timeouts and am wondering what I am doing wrong. In more detail, I retrieve an excel...
2
by: Krishna | last post by:
I was trying to delete rows in an existing .xls file using python. How do I do that? I was using the following code, it seem to work if I type in python window, but if I save it in text editor and...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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...

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.