Please someone explain this to me..

oric

New Member
Joined
Jul 19, 2005
Messages
36
OK.

I'm trying to write some code that will basically perform the same calculations to a lot of excel files in a folder called "Test Folder". I learned from here that I can use the dir command. Here is my code

Code:
Sub Batchtest()
Dim myFile As String, myCurrFile As String
Dim value1
myCurrFile = ThisWorkbook.Name
myFile = Dir("C:\Test Folder\*.xls")
Do Until myFile = ""
Workbooks.Open "C:\Test Folder\" & myFile
Sheets("Nav").Activate
Range("A1").Select
value1 = ActiveCell.Value
Worksheets(2).Range("A1").Value = value1
ActiveWorkbook.Save
ActiveWorkbook.Close
myFile = Dir
Loop

End Sub
All I'm doing on this test run is selecting the value of "A1" which is in the sheet called "Nav" and copying it to sheet2 of the same workbook. After I'm done I save it, close it, move on to next file. The problem is that it doesn't want to put the value on sheet 2.

BUT when I use this code on just one excel file it works.
Code:
Sub test()
Dim value1
Sheets("Nav").Activate
Range("A1").Select
value1 = ActiveCell.Value
Worksheets(2).Range("A1").Value = value1
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub

What is the differece? The guts are pretty much the same but my batch process doesn't like to copy that value to the second sheet.

I'm still pretty newb to this so pardon if this is really easy.
thanks.
 

Some videos you may like

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Are you actually getting an error message with the code you are running? If not, it may be correctly running the code (including extracting the value of A1 on sheet "Nav" and copying it to A1 on Worksheets(2).Range("A1")). However, specifying the sheet via its index value is very unreliable (it doesn't necessarily follow the order the sheets are arranged, so it is possible that Worksheets(2) is sheet "Nav") - it would be better to specify an actual sheet (ie Worksheet("Sheet2") or whatever) assuming the sheet is always named the same in each file.


Best regards

Richard
 

oric

New Member
Joined
Jul 19, 2005
Messages
36
Thanks for the input.

I'm not getting an error message. It's just not writing the value to the second sheet whereas the other code (non-batch) writes it just fine. I'll try and specify the sheet via "Sheet2" nomenclature. Though I don't see why in one case it works and in the other it doesn't.
 

acw

MrExcel MVP
Joined
Feb 13, 2004
Messages
4,814
Hi

Ran the batchtest code making sure that sheet Nav was the first sheet (index 1) and sheet2 existed, and it ran OK for me. Transferred the data from nav!a1 to sheet2!a1.

What do you have in nav!a1? Is it a formula or a constant?


Tony
 

oric

New Member
Joined
Jul 19, 2005
Messages
36
Thanks for running the code.

For some reason I am still not able to get it to work. I am positive my first sheet is called "Nav". I even tried to change it back to "Sheet1" and it still didn't work. Also I'm sure that my second sheet is "Sheet2". I explicitly stated it as "Sheet2" in the code as well but no go.

I just can't figure out why it will work when I run the non-batch code but not the batch one.

Also.. the contents Nav!A1 is just a constant.

Here is exactly what I did.

Made "Test Folder" in C:
Made 3 excel files that had the first sheet labeled (in this case "Nav"). The contents of A1 in all the files were just some numbers (1, 2, etc.).
Put these files in the Test folder.
Tried to run the batch code shown above.
Code runs and I see it openning and closing the files.
When I open the files all I see is the original data. There is no data in Sheet2.

When running the non-batch code I have a file already openned. The code does its thing and then closes that file. When I open it Sheet2 has the appropriate data in it.

weird....
 

oric

New Member
Joined
Jul 19, 2005
Messages
36
OK...OK...

I think I must have been on crack or something.

I tried it again and it worked. I don't know what happenned. Maybe it was my voiced threats to the computer...

Thanks for your help.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,760
Messages
5,574,083
Members
412,566
Latest member
TexasTony
Top