移至下一行以从VBA填充excel文件


1

我有以下代码,从我的MS Access(小型酒店预订数据库)表单中获取某些字段,并填充所述Excel文件中的已定义单元格。

Dim objXLApp As Object
Dim objXLBook As Object
Set objXLApp = CreateObject("Excel.Application")
Set objXLBook = objXLApp.Workbooks.Open("Y:\123files\File\Hotel Reservation.xls")
objXLApp.Application.Visible = True

objXLBook.ActiveSheet.Range("B2") = Me.GuestFirstName & " " & GuestLastName
objXLBook.ActiveSheet.Range("C2") = Me.PhoneNumber
objXLBook.ActiveSheet.Range("E2") = Me.cboCheckInDate
objXLBook.ActiveSheet.Range("F2") = Me.cboCheckOutDate
objXLBook.ActiveSheet.Range("H2") = Me.RoomType
objXLBook.ActiveSheet.Range("I2") = Me.RoomNumber
End Sub

如何将新访客填充到下一行的同一个Excel文件中?


请不要重复您的问题,请根据您的需要编辑原始问题。
Mehper C. Palavuzlar 2011年

对不起@edmon,我尝试合并问题,并没有保留新版本。我为这个麻烦道歉
Ivo Flipse

Answers:


2

或者只是为这里的确切问题提供一个简单的答案,如果你(也许是出于其他原因)真的想通过VBA这样做。(说实话,我的建议是创建一个访问报告并完全转储excel文件),以下非常简单的VBA代码将做你想要的...

Dim i as Integer 
i = ObjXLBook.ActiveSheet.UsedRange.Rows.Count 
ObjXLBook.ActiveSheet.Cells(i + 1, 2).Value = Me.GuestFirstName & " " & GuestLastName 
ObjXLBook.ActiveSheet.Cells(i + 1, 3).Value = Me.PhoneNumber 

谢谢。尽管如此,它仍然没有将新的任务移到下一行。最初,我只是想运行一个查询,让用户知道房间是否可用或占用,但我不知道如何编写该查询,因此我创建了一个Excel文件,该文件将客人信息存储并以颜色编码存储图表,让用户知道房间是否可用。查询会更容易吗?
edmon

@罗伯特。我道歉它确实有效。唯一的事情是它开始行1000而不是行1.在第1行开始我需要添加什么?谢谢。
edmon

在第1行启动它可归结为确保所有其他行未使用。excel中的“used”可能会变得棘手。我会尝试创建一个新的工作簿,删除行1-1000(只是为了安全),然后尝试上面的脚本。它应该工作,应该从没有数据的第一行开始(再次excel的数据定义有点棘手)...如果你试图将它插入已经有数据的行,那么,我的建议将插入到不同的工作表中,然后使用一个公式,用于从另一个工作表中提取其他数据的工作表。
罗伯特

0

你应该阅读http://office.microsoft.com/en-ca/access-help/import-export-and-link-data-between-access-and-excel-HP001095095.aspx,它会帮助你链接你的MS使用MS Excel spreedsheet访问数据,在这种简单的情况下不需要VBA。

摘抄:

导入操作入门

ShowStep 1:标识要导出的数据

首先在数据库中找到包含要导出数据的数据库和对象。您可以导出表,查询,表单或报表。例如,您可以导出存储在Customers表中的客户数据或整个产品目录报告。

注意您无法导出数据访问页(数据访问页:用于查看和处理来自Internet或Intranet的数据的Web页面。其数据通常存储在Access数据库中。),宏和模块。

ShowStep 2:确定从哪里开始导出操作

....

您可以从“数据库”窗口导出对象(数据库窗口:在Access 2003及更早版本中,打开数据库或项目时出现的窗口。它显示用于创建新数据库对象和打开现有对象的快捷方式。在以后的版本中,它是由导航窗格替换。),或者在视图中打开时。下表描述了视图如何影响导出的内容。

注意您无法从设计视图或SQL视图导出数据。对象视图/窗口导出的内容表,查询,表单数据库窗口所有字段和记录表,查询,表单数据透视表和数据透视图视图基础记录源中的所有字段和记录,而不管字段是否实际包含在视图中。表,查询,表单数据表视图如果只想导出部分数据,可以选择它,然后选择仅导出所选数据。您还可以选择导出整个数据表。表单窗体视图基础记录源中的所有字段和记录,无论字段是否实际包含在视图中。“报告数据库”窗口,“打印预览”和“布局预览”“组头和详细信息”部分中的文本框中包含的所有数据,组页脚中包含带Sum函数的表达式的任何文本框。Access使用Excel的大纲功能在Excel中格式化报表。有关如何将报表导出到Excel的详细信息,请参阅如何将报表输出到Microsoft Excel。有关如何在Excel中使用报表的详细信息,请参阅Excel帮助中有关使用轮廓的主题。

ShowStep 3:标识导出操作的目标文件

在导出操作期间,系统将提示您指定目标文件的名称。如果不存在具有您指定名称的文件,则将创建新文件。如果文件存在,将发生以下事件之一:

* If you are exporting a table or query and you don't select the Save Formatted check box during the export operation, the file will not be overwritten. A new worksheet will be added to the file with the same name as the object that is being exported. If a worksheet already exists with that name, Access will prompt you to either replace the contents of the corresponding worksheet, or specify a different name for the new sheet.

选中“保存格式”复选框会使工作表继承与数据表类似的格式设置,但会覆盖工作表的现有内容。

* If you are exporting a form or a report, the file will always be overwritten. All of its existing worksheets will be removed, and a new worksheet with the same name as the exported object will be created.

ShowStep 4:在导出某些数据类型和控件之前应该了解的事项

* Pictures and objects    All graphical elements, such as a logo, background picture, and contents of OLE object fields that are part of the exported data will not be exported.
* Graph    When you export a form or a report that contains a Microsoft Graph object, the graph object does not get exported. To resolve this situation, see How to export a graph from Access to Excel.
* Null values    Sometimes, Null values get replaced with the data that should be in the adjacent column in the resulting worksheet. For more information on when and why this problem occurs, and how to resolve it, see Nulls replaced with next field's data when you export to Excel.
* Calculated values    The expression that is used to calculate the values is not exported to Excel. Only the results of the expressions get exported.
* Date values    Date values earlier than Jan 1, 1900 do not get exported — the corresponding cell in the worksheet will contain a Null value, instead. For more information about how to resolve this, see Exporting dates may result in nulls or cause "Numeric Field Overflow" errorExporting dates may result in nulls or cause "Numeric Field Overflow" error.
* Check boxes    If you start the export operation from the Database window (Database window: In Access 2003 and earlier, the window that appears when a database or project is opened. It displays shortcuts for creating new database objects and opening existing objects. In later versions, it is replaced by the Navigation Pane.) or in Form view, check boxes on forms will not get exported. The corresponding column in the worksheet will display "#". To solve this situation, open the form in Datasheet view before exporting it. The corresponding column in the worksheet will contain TRUE or FALSE, depending on the selected status of the check box in the form.
* Subreports and subforms    Subreports are exported, but subforms will be ignored.

ShowStep 5:开始导出操作

  1. 如果要导出的对象未打开,请在“数据库”窗口中(数据库窗口:在Access 2003及更早版本中,打开数据库或项目时显示的窗口。它显示用于创建新数据库对象和打开现有对象的快捷方式。在更高版本中,它将被导航窗格替换。),单击对象的名称。要仅保存数据表的一部分,请在继续之前打开数据表并选择数据表的该部分。
  2. 在“文件”菜单上,单击“导出”。
  3. 在“保存类型”框中,单击“Microsoft Excel 5-7”或“Microsoft Excel 97-2003”。

    注意:如果在“文件类型”框中未显示Microsoft Excel,则表明注册表中所需驱动程序的路径无效。有关如何更正此问题的详细信息,请参阅无法找到可安装的ISAM错误消息。

  4. 单击“保存位置”框右侧的箭头,然后选择要保存到的驱动器或文件夹。

  5. 在“文件名”框中,输入文件的名称(或使用建议的名称)。
  6. 选中“保存格式化”复选框。
  7. 执行以下操作之一:*如果要保存数据表,请单击“全部导出”以保存整个数据表,或者如果在步骤1中选择了数据表的一部分,则单击“保存选择”。*对于所有其他数据库对象,请单击“导出”。

ShowStep 6:查看Excel工作表

打开工作表并确保数据已完全导出。查找单元格(绿色三角形)或错误值(以“#”开头的字符串而不是数据)上的错误指示符。有关解决错误指示符和错误值的详细信息,请参阅Excel帮助。

在查看工作表中的错误时,还要查找空白或缺少的列以及空单元格。如果发现重大问题,请在源数据库中更正它们,然后重复导出操作。

有关详细的故障排除信息,请参阅Access中的导出疑难解答。将Access数据导入Excel的其他方法

除了导出之外,您还可以使用以下技术将数据从Access导入Excel。

* Cut or copy data from Access and paste it into an Excel worksheet. For more information about how to do this, see the section "Copy or move records or data from multiple fields in Microsoft Access to another application" in the topic Copy or move data.
* Export data by using code. You can write a macro or a Visual Basic for Applications (VBA) procedure to export data programmatically. For more information about how to do this, see Export data programmatically.
* Load Access data in an instance of Excel.

ShowHow?

  1. 在“数据库”窗口中(“数据库”窗口:在Access 2003及更早版本中,打开数据库或项目时显示的窗口。它显示用于创建新数据库对象和打开现有对象的快捷方式。在更高版本中,它将替换为导航窗格。),单击要保存并加载到Excel中的数据表,表单或报表的名称。要仅加载数据表的一部分,请打开数据表,然后在继续之前选择数据表的该部分。
  2. 在“工具”菜单上,指向“Office链接”,然后单击“使用Microsoft Excel分析它”。

    • 将Microsoft Access数据导出为可扩展标记语言(可扩展标记语言(XML):标准通用标记语言(SGML)的精简形式,使开发人员能够创建自定义标记,提供组织和呈现信息的灵活性。)(XML)文件,然后可以导入到Excel中。有关如何将Access数据导出为XML的更多信息,请参阅将访问数据导出为XML。

....

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.