Do we sometimes need to insert a chart in a Web page, or use a chart in another non-Office program, and have a way to insert a chart from an Excel workbook into another program? You can actually do this by saving the worksheet as an HTML file, and when you save it as a Web page, Excel automatically converts the chart to a GIF file. If you're having trouble doing this, you can also use Office's own VBA editor to write a simple macro to do the job.
The specific steps are as follows:
1. Create a chart for the exercise
Start Excel, the default workbook is Book1, first we enter the data shown in Figure 1 in the worksheet Sheet1. Notice here that the fluctuations in the data are not too large to prevent the created graphs from being uncoordinated.
Figure 1
Select the A1 to E5 cell, and then execute the menu command Insert > chart, Open the Chart Wizard dialog box, and click the Finish button in the dialog box to create the chart shown in Figure 2.
Figure 2
2. Create macros
Press the shortcut key Alt+f11 key to open the Visual Basic Editor. In the Projects window, select the workbook that contains the chart, and then execute the menu command Insert > module, open the Module 1 window and enter the following four lines of code in the window, as shown in Figure 3.
Sub Savechartasgif ()
Fname = Thisworkbook.path & "" & Activechart.name & ". gif"
Activechart.export filename:=fname, filtername:= "GIF"
End Sub
Figure 3
When the code is finished, don't forget to save the disk. Close the Visual Basic Editor after you save the disk.
3. Execute macros
Go back to the Excel window, save the workbook to a folder, select the chart, and then press the shortcut key ALT+F8 to open the Macros dialog box. Select the Savechartasgif macro and click the Execute button, as shown in Figure 4.
Figure 4
The GIF file that is generated after you run the macro is named after the chart name. and is saved to the folder in which the workbook is located, open the folder, you can see the generated GIF file, use any image browsing software to browse the file, in other programs can easily insert this GIF file.
Note: Because this simple macro has no error checking mechanism, an error message will appear if the chart is not selected before the macro is executed or if the workbook is not saved.