In trying to reproduce lesson 2 p 33, I write
Sub ImportFile()
'
' ImportFile Macro
' Macro recorded 5/17/2012 by Louis Napoli
'
'
myFile = Application.GetOpenFilename("Text Files.*.txt")
Workbooks.OpenText
FileName:=myFile, _
Origin: = xlWindows,_
StartRow:=4, _
DataType:xlFixedWidth, _
And I get an error message "compile error" highlighting :=
Yet this is exactly what the book shows.
You should learn vba syntax. Still heppful could be vba help (conceptual topics) in the vbe environment.
There are a lot of syntax errors in your example, be careful:
- [tt]Workbooks.OpenText[/tt]: a line divisor missing, the following (named) arguments should be in one command,
- [/tt]Origin: = xlWindows,_[/tt]: a space is missing before line divisor,
- [tt]DataType:xlFixedWidth, _[/tt]: command divisor ) instead of named argument assignment =).
Be careful with excel version you use. VBA in excel 97 was based on visual basic 5, VBAs in office 2000 and later are based on visual basic 6. Additionally, host application's object model (in your case excel and could be office) varies with office version. By theory all the code should work when you upgrade office, but it's not so.
If formatted the code in the editor to have one argument per row and pasted each into an Excel sheet and then used a formula to test each row for equality.
You can see the differences this way.
Skip,
Just traded in my old subtlety...
for a NUANCE!
VBA is rather descriptive. In this example the differces are caused either by different user actions recorded (for active window, cell/row selection) or guess that different excel versions (Origin argument for TextOpen method).
You could copy complete blocks of the code, next paste and execute in separate procedure. IMHO the fundamental problem is to understand what the code does rather than focusing on differences.
"what each statement does?"
If you know how to set a break point in your code (if you don't - you should ): place the cursor on the line you want to stop at and hit F9, and when you excecute your code and stop on that line, hit F8 to step line-by-line thru your code. You can see what every line of your code does.
If works great if you have 2 monitors, but one is fine, too.
I created the above macro, but when I stepped through it using F8, it got hung up on
Selection.SpecialCells(xlCellTypeBlanks).Select
Showing a "run time error '1004'and no cells found. Any thoughts on that?
means there is no blank cells in the range you have selected
That is why most people that program with excel do not use select / activate as it can cause problems with understanding the range of cells you are working with. Unfortunately, recording a macro DOEs use a lot of select / activate
As a basic tip, if you see a select at the end of 1 line followed by a selection at the start of the next you can dispense with both
So:
Range("A1").Select
Selection.CurrentRegion.Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"
Can become:
Range("A1").CurrentRegion.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
Please feel free to use the VBA help to try to understand basic error messages
Rgds, Geoff
We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.