Beruflich Dokumente
Kultur Dokumente
This function is used to open a file for either input or output, depending on the exact syntax used.
You use this function to obtain the next valid number for use when performing file operations
Note that after allocating a number in this way, you must open or close a file before using it again, otherwise the same number is generated.
intInFile = FreeFile intOutFile = FreeFile Open "C:\in.txt" For Input As intInFile Open "C:\out.txt" For Output As intOutFile
intInFile = FreeFile Open "C:\in.txt" For Input As intInFile intOutFile = FreeFile Open "C:\out.txt" For Output As intOutFile
This function indicates when you have reached the end of a file, and as such it is normally used as the condition for a Do loop.
This function will read data from a file that has been written to using the Write function. As this means that text will normally be delimited with quotes and similar, it is not of much use for general text importing.Mode can be
This function will input a line at a time from the text file You will often read this into a temporary variable, and then split it using the string handling functions (Left, Mid, etc.) as required This is probably of most use when you have a record of fixed width One trick with this function is to read the whole file in one go this is often faster than reading it line by line or character by character, and then separating it
Sub sImportAll() On Error GoTo E_Handle Dim strImport As String Dim lngChars As Long Dim intFile As Integer intFile = FreeFile Open "C:\test.txt" For Input As intFile
Do while not EOF( intFile )
E_Handle: MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number E-exit: End Sub
This function is used to write data to an output file. I would normally use this function, rather than Write, as it does not wrap data in special characters, making the exported more universally available. E.g. Print filenumber, string
This function is used to write data to a file, but as it 'wraps' data with special characters (such as double quotes) it is not that useful as far as exporting data for use in other programs is concerned Also be aware that it ignores locale settings, such as the decimal separator.
You use this function to close a file that has been opened using the Open statement. It is important to close files that you have opened, either using this function or Reset, otherwise they are 'locked'. E.g. close filenumber
You use this function to reset all files that have been opened therefore it should be used with care, as you may find yourself closing a file that has been opened in another procedure Unless I know that I have a file open in another procedure, I will normally have this statement in the exit point for a procedure, otherwise I will use the Close statement on each individual file.
You encounter error while running the code Not all error are because of bad code You might encounter an error because the procedure might anticipate different data Type Whatever is the cause, you will need determine source of the error and resolve the issue The process of finding error is called debugging
Predominantly created when you write code Most design errors are syntax error when you mistype a statement You create design errors when you forget about function argument or missing paranthesis E.g MsgBox(sample text As long as you use the code setting of VBA editor, you immediately know when a syntax error occurs
Compiling is converting the VBA to the format that computer can understand In other languages you need to compile before you run the code In VBA compilation of code happens when you run the macro/subroutine If any error occurs during compile process an error message box pops up and VBA editor highlights the location of the error
You encounter runtime errors when your code executes If you not handled runtime error the execution of macro/procedure stops To avoid stopping of execution you need to write On Error Resume Next VBA put information of error in Err Object
Err Object
Error object property Description Number Contains VBA description of the runtime error Contains VBA error number of the runtime error Indicate name of the current procedure which has caused as error
Source
Logical error do not produce any type of error message Instead logical error return unexpected result E.g Price1 = 4.45 Price2 = 6.95 TotalCost =(Price1 +price2)*1.75
Workbook can be opened using Open Method of workbooks Collection It requires filename
E.g.
Workbooks.Open(C:\Workbooks\budget.xls)
You can open a text file within Excel using OpenText method of workbooks collection method It requires filename
Text file remains text file but you can modify it using excel as editor E.g. Workbooks.Opentext filename : =
C:\Workbooks\Sample.txt), Datatype :+ xlDelimited, Tab:= True
you can retrieve the name of the file with prompting the user with Open dialog box To display open dialog box, you use the GetOpenFileName method When you use this method file does not open when user clicks ok. The dialog passes the name to variable name and then you can open the file using open property
You can save currently selcted workbook using save or save as method. e.g workbooks(sample.xls).save
You can request the name, location and format for saving workbook file from the user e.g GetSaveAsFileName opens dialogbox
The dialog box does not save the file whereas it retunes user specified information information to a variable To save the file you use SaveAs command
Myworkbook = workbooks(1).name You can compare Myworkbook to filename you want to check
You can close the workbook using close method E.g. Activeworkbook.close or Thisworkbook.close
You can create new workbook using the Add method of workbooks collection E.g. workbooks.Add(Tempalate)
VBA provides ability to delete workbook or any othe file suing Kill method E.g. kill(pathname)
You can find file on your computer By using this you can check if file exits before doing any operation on the file You use FileSearch object to serach for file on the computer You can set .filename property to filename you want to search You can set .LookIn property to directory where you want to search the file
Axes Collection Collection of Axis objects including Axistilte, Border,Gridlines and Ticklables Chart Area Chart Area including Border,Font and Interior object Chartobjects Collection- Collection of chart objects on the sheet Charttitle Datatable represents chart data table Legend
There are different chart types available You can select charttype by specifying xlChartType constant value of Charttype property
With any chart you must specify charts range of data You use SetSourceData method to specify datasource for your chart E.g Mychart.SetSourceData Source:=Worhsheets(Sheet1). Range(A1:A5)
Dim Echart Aas ChartObject Set Echart =Sheets(Sheet1).ChartObjects.Add(left:=50, top:=100, Widhth:=300, Height :=400) Setting the chart type Worksheets(Sheet1).Chartobjects(1).chart.ch artytype =xlColumstacked
Vinayak.jadhav@thinkinghut.com
e.g. Dim SelectChart As Chart Set SelectChart =ThisWorkbook.Charts(Chart2) SelectChart.ChartWizard Gallery:=xL3DLine, Format=2,Categorylables:= True
To define new Data series or to add to existing dataseries you create a new Series object and add it to SeriesCollection object using add method E.g SelectChart.SeriesCollection.Add Source:=Worksheets(Sheet1).Range(D1:D7 )
You can customize the text of the chart by changing font and colour attribute e.g. With Selectchart .ChartArea.Font.Name =Tahoma .ChartArea.Font.Colour = RGB(0,0,255)
You can customize each Axis of the chart using Axis Object properties and Methods Each Chart Axis is separate object Axes collection object contains all chart Axis objects E.g. with SelectChart.Axes(xlValue) .HasTitle =True .AxisTitle.Text =Value Axis