MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Apr 3rd, 2002, 09:36 AM   #1
murphy00
New Member
 
Join Date: Feb 2002
Posts: 15
Default

The data on sheets 1 thru 30 looks like this:

L 2800 33/2 PP2-1 CIRCUIT
3 el-swch
pd-recep

I want the macro to go through and find CIRCUIT in column G, copy cells A thru D on that row and paste to Worksheet("Circuit") on the next available row. I have most of the code done but keep running into errors. Could someone out there please look this over and offer some assistance?

Here is the code:
Code:
Sub Circuit()
    Dim oSheet As Variant
    Dim sht As Integer
    Dim Firstcell As Range
    Dim cRow As Integer
    Dim nRow As Integer
    Dim pRow As Integer
    Dim dest As Range
    Dim NextCell As Range
    Dim WhatToFind As Variant
        WhatToFind = "CIRCUIT"
        For sht = 1 To 30
        oSheet = "Sheet" & sht
            Worksheets(oSheet).Activate
            Worksheets(oSheet).[g1].Activate
            Set Firstcell = Cells.Find(What:=WhatToFind, LookIn:=xlValues, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
                If Not Firstcell Is Nothing Then
                cRow = Firstcell.Row
                pRow = Worksheets("Circuit").Range("A65536").End(xlUp).Offset(1, 0)
                dest = Worksheets("Circuit").Range("A" & pRow & ":D" & pRow)
                Range("a" & cRow & ":D" & cRow).Copy (dest)
                On Error Resume Next
                    While (Not NextCell Is Nothing) And (Not NextCell.Address = Firstcell.Address)
                    Set NextCell = Cells.FindNext(After:=ActiveCell)
                        If Not NextCell.Address = Firstcell.Address Then
                nRow = NextCell.Row
                pRow = Worksheets("Circuit").Range("A65536").End(xlUp).Offset(1, 0)
                dest = Worksheets("Circuit").Range("A" & pRow & ":D" & pRow)
                Range("a" & cRow & ":D" & cRow).Copy (dest)
                        End If
                    Wend
                End If
            Set NextCell = Nothing
            Set Firstcell = Nothing
            Next sht
End Sub
Thank you very much,
Murph
murphy00 is offline   Reply With Quote
Old Apr 3rd, 2002, 10:35 AM   #2
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

See if the following works:

Dim NextAvailableRow As String
CommandButton1.TakeFocus******* = False
NextAvailableRow = Intersect(Worksheets("Circuit").UsedRange, Worksheets("Circuit").Columns("A")).Offset(1, 0).Address
Set c = Worksheets(1).Columns("G").Find("CIRCUIT", LookIn:=xlValues)
If Not c Is Nothing Then
firstaddress = c.Address
Do
Worksheets("Circuit").Range(Worksheets("Circuit").Range(NextAvailableRow), Worksheets("Circuit").Range(NextAvailableRow).Offset(0, 3)).Value = Worksheets("Sheet1").Range(Range(c.Address).Offset(0, -6), Range(c.Address).Offset(0, -3)).Value
NextAvailableRow = Range(NextAvailableRow).Offset(1, 0).Address
Set c = Worksheets(1).Columns("G").FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstaddress
End If

I wrote it quickly so bear that in mind.
__________________
Kind regards,

Al Chara
Al Chara is offline   Reply With Quote
Old Apr 3rd, 2002, 11:20 AM   #3
murphy00
New Member
 
Join Date: Feb 2002
Posts: 15
Default

Al,

I tried the routine you gave me and I keep getting a 'Objet Variable or With Block Variable not set' on the line firstAddress = c.Address.

Here is the new routine.

Sub Circuit()
Dim NextAvailableRow As String
Dim oSheet As Variant
Dim sht As Integer
Dim c As Object
Dim firstAddress As Range
For sht = 1 To 30
oSheet = "Sheet" & sht
Worksheets(oSheet).Activate
Worksheets(oSheet).[g1].Activate
NextAvailableRow = Intersect(Worksheets("Circuit").UsedRange, Worksheets("Circuit").Columns("A")).Offset(1, 0).Address
Set c = Worksheets(oSheet).Columns("G").Find("CIRCUIT", LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
Worksheets("Circuit").Range(Worksheets("Circuit").Range(NextAvailableRow), Worksheets("Circuit").Range(NextAvailableRow).Offset(0, 3)).Value = Worksheets(oSheet).Range(Range(c.Address).Offset(0, -6), Range(c.Address).Offset(0, -3)).Value
NextAvailableRow = Range(NextAvailableRow).Offset(1, 0).Address
Set c = Worksheets(1).Columns("G").FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
Next sht
End Sub


Thanks,
Murph
murphy00 is offline   Reply With Quote
Old Apr 3rd, 2002, 11:23 AM   #4
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

Don't dim firstaddress as range (it is a string). Use the code exactly as I gave you.
__________________
Kind regards,

Al Chara
Al Chara is offline   Reply With Quote
Old Apr 3rd, 2002, 11:41 AM   #5
murphy00
New Member
 
Join Date: Feb 2002
Posts: 15
Default

Al,

Thank you very much!!!
It works great!
Is there any way to have it find all CIRCUIT on the first 30 sheets of the workbook?

Thanks,
Murph
murphy00 is offline   Reply With Quote
Old Apr 3rd, 2002, 12:14 PM   #6
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

Throw it all in a for loop where you have something like the following:

for i=1 to 30
worksheets(i)...
next

Make sure that the circuit worksheet is not one of the first 30 worksheets.

Try the following:

Dim NextAvailableRow, firstaddress As String
Dim c As Object
Dim i As Integer
NextAvailableRow = Intersect(Worksheets("Circuit").UsedRange, Worksheets("Circuit").Columns("A")).Offset(1, 0).Address
For i = 1 To 30
Set c = Worksheets(i).Columns("G").Find("CIRCUIT", LookIn:=xlValues)
If Not c Is Nothing Then
firstaddress = c.Address
Do
Worksheets("Circuit").Range(Worksheets("Circuit").Range(NextAvailableRow), Worksheets("Circuit").Range(NextAvailableRow).Offset(0, 3)).Value = Worksheets(i).Range(Range(c.Address).Offset(0, -6), Range(c.Address).Offset(0, -3)).Value
NextAvailableRow = Range(NextAvailableRow).Offset(1, 0).Address
Set c = Worksheets(i).Columns("G").FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstaddress
End If
Next


(you can get rid of your code that activates the sheet and cell; I wrote the code so you don't have to activate anything)

_________________
Hope this helps.
Kind regards, Al.

[ This Message was edited by: Al Chara on 2002-04-03 11:17 ]
Al Chara is offline   Reply With Quote
Old Apr 3rd, 2002, 01:26 PM   #7
murphy00
New Member
 
Join Date: Feb 2002
Posts: 15
Default

Al,
I tried but it keeps giving me an error in this line:
Worksheets("Circuit").Range(Worksheets("Circuit").Range(NextAvailableRow), Worksheets("Circuit").Range(NextAvailableRow).Offset(0, 3)).Value = Worksheets(i).Range(Range(c.Address).Offset(0, -6), Range(c.Address).Offset(0, -3)).Value
Any idea why? Could it be that I have to define a variable to add "Sheet" & i?

Thanks,
Murph
murphy00 is offline   Reply With Quote
Old Apr 4th, 2002, 08:18 AM   #8
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

The code Worksheets(1) will look at the first worksheet in the workbook, Worksheets(2) will look at the second worksheet in the workbook, and so on. The following code should work if you have the thirty worksheets in order and Circuit is not one of the first thirty worksheets. You can also replace worksheets(i) with worksheets("Sheet" & i).

Dim NextAvailableRow, firstaddress As String
Dim c As Object
Dim i As Integer
NextAvailableRow = Intersect(Worksheets("Circuit").UsedRange, Worksheets("Circuit").Columns("A")).Offset(1, 0).Address
For i = 1 To 30
Set c = Worksheets(i).Range("G:G").Find("CIRCUIT", LookIn:=xlValues)
If Not c Is Nothing Then
firstaddress = c.Address
Do
On Error Resume Next
Worksheets("Circuit").Range(Worksheets("Circuit").Range(NextAvailableRow), Worksheets("Circuit").Range(NextAvailableRow).Offset(0, 3)).Value = Worksheets(i).Range(Worksheets(i).Range(c.Address).Offset(0, -6), Worksheets(i).Range(c.Address).Offset(0, -3)).Value
NextAvailableRow = Range(NextAvailableRow).Offset(1, 0).Address
Set c = Worksheets("Sheet" & i).Columns("G").FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstaddress
End If
Next
__________________
Kind regards,

Al Chara
Al Chara is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 05:28 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes