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 27th, 2002, 05:52 PM   #1
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Can't seem to get the syntax right to open the workbook named in the input box. In this case VMNB123 but this will vary but it will be open at the same time as the target workbook. Is this a great forum or what?


Sub ImportJob()
'Imports all data for a new job
Dim JobCode As Variant
Dim sht As Variant
On Error GoTo errhand

JobCode = InputBox(prompt:="", Title:="INPUT THE JOB CODE")
If JobCode = "" Then
MsgBox prompt:="", Title:="NOTHING ENTERED"
Exit Sub
End If

Application.ScreenUpdating = False
Windows("VMNB123.xls").Activate
For Each sht In ActiveWorkbook.Worksheets
Windows("VMNB123.xls").Activate
Range("B18:D37").Select
Selection.Copy
Range("A1").Select
Windows("Pay Verification.xls").Activate
Range("E65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select

Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False

Range("B65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
Selection.FormulaR1C1 = JobCode
Range("C65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
Selection.FormulaR1C1 = sht.Name
Range("D65536").End(xlUp).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
ActiveSheet.Paste
Range("H65536").End(xlUp).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(19, 0)).Select

ActiveSheet.Paste
Selection.NumberFormat = "0.00"
Application.CutCopyMode = False
Range("A1").Select

Next sht
errhand:
MsgBox prompt:="", Title:="ERROR, TRY AGAIN"

End Sub
__________________
George

Learn to listen. Opportunity sometimes knocks very softly.
GeorgeB is offline   Reply With Quote
Old Apr 27th, 2002, 06:35 PM   #2
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

George...

JobCode = InputBox(prompt:="", Title:="INPUT THE JOB CODE")
If JobCode = "" Then
MsgBox prompt:="", Title:="NOTHING ENTERED"
Exit Sub
End If

May be better if replaced with a GetOpenFileName statement.

The activate method is only for workbooks which are already opened...

This line in your code:


Windows("VMNB123.xls").Activate


Should be:


Workbooks.Open YourPath & "/" & JobCode & ".xls"



For example:

Lets assume all of the job code workbooks are in the following folder:

"C:Job Codes Workbooks"

Get your data from the inputbox:

As you stated, in this case, we will assume
"VMNB123.xls"

Use this syntax to open the workbook:


Workbooks.Open "C:Job Codes Workbooks" & JobCode & ".xls"



It is not really neccesary to include the file extension, but it is a good practice.

Is this what you are after?

Tom

[ This Message was edited by: TsTom on 2002-04-27 17:36 ]
Tom Schreiner is offline   Reply With Quote
Old Apr 27th, 2002, 07:10 PM   #3
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Thanks for your reply Tom
Still working on it. So far I have this.

Workbooks.Open Filename:="C:Job Codes & JobCode & .xls"

But it won't go. "Cant be found" Used the macro recorder to get this far.

By the way how do you activate the workbook after it is open and on the 2nd time around in the loop??
__________________
George

Learn to listen. Opportunity sometimes knocks very softly.
GeorgeB is offline   Reply With Quote
Old Apr 27th, 2002, 09:35 PM   #4
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Almost there.
Anyone know the syntax to re-activate a workbook so info on the next sheet can be extracted??
Here's what I have.

Sub ImportJob()
'Imports all data for a new job
Dim JobCode As Variant
Dim sht As Variant
'On Error GoTo errhand

JobCode = InputBox(prompt:="", Title:="INPUT THE JOB CODE")
If JobCode = "" Then
MsgBox prompt:="", Title:="NOTHING ENTERED"
Exit Sub
End If

Application.ScreenUpdating = False
Workbooks.Open Filename:="C:Job Codes" & JobCode & ".xls"
For Each sht In ActiveWorkbook.Worksheets

Windows("VMNB123.xls").Activate 'THIS WORKS BUT ONLY FOR THIS WORKBOOK
'NEED SOME CODE TO REACTIVATE THE WORKBOOK NAMED AS A VARIABLE
'Windows JobCode & ".xls".Activate 'THIS DOES NOT WORK ???

__________________
George

Learn to listen. Opportunity sometimes knocks very softly.
GeorgeB is offline   Reply With Quote
Old Apr 27th, 2002, 09:40 PM   #5
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

WorkBooks(JobCode).Activate
Tom Schreiner is offline   Reply With Quote
Old Apr 27th, 2002, 10:42 PM   #6
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Must be getting close but it extracts data from one sheet of the variable workbook only and ignores the others.
I appreciate your help Tom.
__________________
George

Learn to listen. Opportunity sometimes knocks very softly.
GeorgeB is offline   Reply With Quote
Old Apr 27th, 2002, 11:42 PM   #7
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi George...
I'm looking at your code and trying to help you out...
I'm assuming that this procedure is being run from "Pay Verification.xls"?
What is the name of the sheet in "Pay Verification.xls" that is recieving the paste?
Thanks,
Tom

Tom Schreiner is offline   Reply With Quote
Old Apr 28th, 2002, 12:01 AM   #8
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Try this out George




Sub ImportJob()
'Imports all data for a new job
Dim JobCode As Variant
Dim sht As Worksheet
Dim CurrentSheetName As String
On Error GoTo errhand

CurrentSheetName = ActiveSheet.Name
JobCode = InputBox(prompt:="", Title:="INPUT THE JOB CODE")
If JobCode = "" Then
MsgBox prompt:="", Title:="NOTHING ENTERED"
Exit Sub
End If

Application.ScreenUpdating = False
Workbooks.Open Filename:="C:Job Codes" & JobCode & ".xls"
For Each sht In Workbooks(JobCode).Worksheets
Range("B18:D37").Copy
Workbooks("Pay Verification").Activate
With Workbooks("Pay Verification").Sheets(CurrentSheetName)
.Range("E65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
.Range("B65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
.Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
Selection.FormulaR1C1 = JobCode
.Range("C65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
.Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
Selection.FormulaR1C1 = sht.Name
.Range("D65536").End(xlUp).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
.Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
.Paste
.Range("H65536").End(xlUp).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
.Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
.Paste
Selection.NumberFormat = "0.00"
Application.CutCopyMode = False
.Range("A1").Select
End With
Next sht
Workbooks(JobCode).Close savechanges:=False
Exit Sub
errhand:
MsgBox prompt:="", Title:="ERROR, TRY AGAIN"

End Sub



Tom
Tom Schreiner is offline   Reply With Quote
Old Apr 28th, 2002, 10:10 AM   #9
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Tom
Here’s the code with your modifications and a few of my own. Yes the procedure is run from Pay Verification and the workbook JobCode will already be open. The Network is too big for the computer to go looking for it. The sheet name being pasted to in Pay Verification (the only sheet) is INPUT but can be changed.
This code will loop for as many sheets that are in JobCode but will only extract info from one sheet. Whatever sheet is active when it is opened.
Thanks again for your help.

Sub ImportJob()
'Imports all data for a new job into Pay Verification
Dim JobCode As Variant
Dim sht As Worksheet
Dim CurrentSheetName As Variant ‘(Sheet names can be alphanumeric)
On Error GoTo errhand

'Ask for the Job Code
JobCode = InputBox(prompt:="", Title:="INPUT THE JOB CODE")
If JobCode = "" Then
MsgBox prompt:="", Title:="NOTHING ENTERED"
Exit Sub
End If
Application.ScreenUpdating = False
'Open the Job inputed by user
'Loop through the workbook and extract the data
Workbooks(JobCode).Activate
For Each sht In Workbooks(JobCode).Worksheets
CurrentSheetName = ActiveSheet.Name
'Copy the desired data
Range("B18:D37").Copy
'Open the target workbook
Workbooks("Pay Verification.xls").Activate
With Workbooks("Pay Verification").Sheets("INPUT")
.Range("E65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
'Paste in the data
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
'Paste in the Job name (JobCode)
.Range("B65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
.Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
Selection.FormulaR1C1 = JobCode
'Paste in the sheet name
.Range("C65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
.Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
Selection.FormulaR1C1 = CurrentSheetName
'Extend the formulas down
.Range("D65536").End(xlUp).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
.Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
.Paste
.Range("H65536").End(xlUp).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
.Range(ActiveCell, ActiveCell.Offset(19, 0)).Select
.Paste
Application.CutCopyMode = False
.Range("A1").Select
End With
Workbooks(JobCode).Activate
Next sht
Exit Sub

errhand:
MsgBox prompt:="", Title:="ERROR, TRY AGAIN"

End Sub

__________________
George

Learn to listen. Opportunity sometimes knocks very softly.
GeorgeB is offline   Reply With Quote
Old Apr 28th, 2002, 08:27 PM   #10
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Any takers on this one??
__________________
George

Learn to listen. Opportunity sometimes knocks very softly.
GeorgeB 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:21 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