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 May 17th, 2002, 05:17 AM   #1
Mudface
MrExcel MVP
 
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
Default

I've written the following to extract data from a number of workbooks, opened by a form. It works OK, but the ExtractData sub using arrays to 'copy and paste' the data seems a bit slow (around 2-3 seconds to put the data into a new workbook). Does anyone have any ideas about how to speed it up (screenupdating etc set to false, already)? Thanks for any help.

Code:
Option Explicit
Dim x As Integer
Dim y As Integer
Dim ws As Worksheet
Dim wb As Workbook
Dim myLastRow As Long
Const myWBName As String = "Hydrog Reactors*"

Public Sub Main()
Dim myYear As Integer
Dim myarray() As String
Const myPath As String = "blah-de-blah"
Const myFilename As String = "Hydrog Reactors"
Const myExtension As String = ".xls"
Const myPath2 As String = "Spreadsheets"

y = 0
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

myYear = Val(frmMain.cboYear.Text)

For x = 0 To frmMain.lstQtr.ListCount - 1
    If frmMain.lstQtr.Selected(x) Then
        ReDim Preserve myarray(y)
        myarray(y) = frmMain.lstQtr.List(x)
        y = y + 1
    End If
Next x

For y = 0 To UBound(myarray)
    Workbooks.Open myPath & myYear & myPath2 & myarray(y) & " " & myYear & myFilename & " " _
    & myarray(y) & " " & myYear & myExtension
Next y

Unload frmMain

Call CheckWorkbooks
Call DeleteRows
Call SetUpPivot
Call CloseWorkbooks

With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

End Sub

Public Sub CheckWorkbooks()

ThisWorkbook.Sheets(1).Rows(1).Copy
With Workbooks.Add
    .Windows(1).Caption = "Destination"
    .Sheets(1).Paste
    .Sheets(1).Name = "Summary"
    .Sheets(2).Name = "Pivot Table"
    .Sheets(3).Delete
End With

For Each wb In Workbooks
    If wb.Name Like myWBName Then
        wb.Activate
        Call CheckWorksheets
    End If
Next wb

End Sub

Public Sub CheckWorksheets()
Const mySheetName As String = "Hydrog*"

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like mySheetName Then
        ws.Activate
        Call ExtractData
    End If
Next ws

End Sub

Public Sub ExtractData()
Dim Pos As Variant
Dim OverallArray() As Variant
Dim z As Integer

Set ws = ActiveSheet
Pos = Array(1, 5, 7, 10, 12, 14, 16, 18, 20)

For x = 3 To ws.UsedRange.Rows.Count Step 10
    For y = 0 To 8
        ReDim Preserve OverallArray(10, z)
        OverallArray(y, z) = Cells(x, Pos(y))
        If OverallArray(1, z) <> "" Then OverallArray(9, z) = ws.Name
    Next y
    If Cells(x + 1, 14) <> "" Then
        OverallArray(10, z) = "Yes"
        OverallArray(5, z) = OverallArray(5, z) + Cells(x + 1, 14)
    End If
    z = z + 1
Next x

Windows("Destination").Activate
myLastRow = LastRow(Sheet1) + 1
For x = 0 To z - 1
    For y = 0 To 10
    Cells(myLastRow + x, y + 1) = OverallArray(y, x)
    Next y
Next x

End Sub

Public Sub DeleteRows()

Windows("Destination").Activate

For x = LastRow(Sheet1) To 1 Step -1
    If WorksheetFunction.CountA(Rows(x)) = 0 Then
        Rows(x).EntireRow.Delete
    End If
Next x

End Sub

Public Function LastRow(ws As Worksheet) As Long

Set ws = ActiveSheet
If WorksheetFunction.CountA(Cells) > 0 Then
    LastRow = ws.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
Else:
    LastRow = 0
End If

End Function

Public Sub SetUpPivot()
Dim myPivot As PivotTable

myLastRow = LastRow(Sheet1)
Columns("A:K").AutoFit
Range("L2").Select
With Selection
    .FormulaR1C1 = "=MAX(RC[-4],RC[-3])"
    .AutoFill Destination:=Range("L2:L" & myLastRow)
    .AutoFilter
End With
ActiveWorkbook.Names.Add Name:="MyRange", RefersToR1C1:= _
"=Summary!R1C1:R" & myLastRow & "C12"

ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:="myrange", _
TableDestination:="'Pivot Table'!R3C1", TableName:="mypv1"

Set myPivot = ActiveSheet.PivotTables("mypv1")

myPivot.AddFields RowFields:=Array("Feed", "Data"), ColumnFields:=Array("Date"), _
PageFields:=Array("Reactor")

For x = 1 To 12
Select Case x
Case 6, 12
With myPivot.PivotFields(x)
    .Orientation = xlDataField
    .Function = xlAverage
    .NumberFormat = "0.00"
End With

Case 2, 11
With myPivot.PivotFields(x)
    .Orientation = xlDataField
    .Position = 1
    .Function = xlCount
End With

End Select
Next x

myPivot.PivotSelect "Date[All]", xlLabelOnly
Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, False, _
True, False, False)
    
With myPivot.PivotFields("Feed")
        .PivotItems("(blank)").Visible = False
End With

End Sub

Public Sub CloseWorkbooks()

For Each wb In Workbooks
    If wb.Name Like myWBName Then
        wb.Close False
    End If
Next wb

End Sub
Mudface is offline   Reply With Quote
Old May 17th, 2002, 06:43 AM   #2
Tommy Bak
Board Regular
 
Join Date: Feb 2002
Location: Guderup, Denmark
Posts: 287
Default

Hi

Try replacing

Windows("Destination").Activate
mylastrow = LastRow(Sheet1) + 1
For x = 0 To z - 1
For y = 0 To 10
Cells(mylastrow + x, y + 1) = overallArray(y, x)
Next y
Next x

with

Windows("Destination").Activate
mylastrow = LastRow(Sheet1) + 1
Range(Cells(mylastrow, 1), Cells(mylastrow + z, 11)) = overallArray

regards Tommy
Tommy Bak is offline   Reply With Quote
Old May 17th, 2002, 07:00 AM   #3
Mudface
MrExcel MVP
 
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
Default

Thanks, Tommy, unfortunately, the array comes out 'transposed'. Any ideas? (I'm a bit slow this afternoon ).
Mudface is offline   Reply With Quote
Old May 17th, 2002, 07:23 AM   #4
Tommy Bak
Board Regular
 
Join Date: Feb 2002
Location: Guderup, Denmark
Posts: 287
Default

Well, it is friday, so i'm slow to

Range(Cells(mylastrow, 1), Cells(mylastrow + z, 11)) = application.worksheetfunction.transpose(overallArray)

Tommy Bak is offline   Reply With Quote
Old May 17th, 2002, 08:02 AM   #5
Mudface
MrExcel MVP
 
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
Default

Brilliant, thanks Tommy, works like a charm with the addition of a z-1. I'll buy you a pint tonight if you can make it to the Tap and Spile in Hull for 8 .
Mudface 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 03:41 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