热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从宏打开XLS文件时,数据会发生变化。-DatachangeswhenopeningXLSfilefrommacro

Iwantedtohaveamacroonworkbook1thataskedtheusertoopenaworkbook(workbook2)thathason

I wanted to have a macro on workbook1 that asked the user to open a workbook (workbook2) that has only one sheet from which to copy all the data to sheet "Import" in workbook1.

我希望在workbook1上有一个宏,该宏要求用户打开一个工作簿(workbook2),该工作簿只有一个工作表,从中可以将所有数据复制到workbook1的“导入”中。

My code is as follows

我的代码如下

Sub test()

Dim NewWB As Workbook
Dim Vfile As Variant

Vfile = Application.GetOpenFilename("Excel-files,*.xls", 1, "The Import _
    sheet is empty, please select ME3M data to import", , False)
If Vfile = False Then Exit Sub
Set NewWB = Workbooks.Open(Vfile)
NewWB.Sheets(1).Cells.Copy Destination:=ThisWorkbook.Sheets("Import").Range("A1")
NewWB.Close

End Sub

My problem is that the file I need to open is in xls and the one with the macro on (on which I want to copy the data) is in xlsx and I suspect this creates some problems because there are some cells that contain numbers the just change their value if opened through vba.

我的问题是,我需要打开的文件是xls和宏观的(我想复制数据)xlsx,我怀疑这造成了一些问题,因为有一些包含数字的单元格,如果打开通过vba只是改变他们的价值。

For example in one cell there is the number 1.500 (one thousand five hundred) and if I open that file the old fashion way by clicking it that's what I see; but If I get it opened by the macro by running the above code without the last two lines the number in the cell becomes 1,5 (one and a half).

例如,在一个单元格中有1。500(1500)如果我按下它来打开旧的格式,这就是我看到的;但是如果我通过运行上面的代码来打开它,而没有最后两行,那么单元格中的数字就变成了1 5(1。5)

1 个解决方案

#1


1  

If you use:

如果你使用:

Set NewWB = Workbooks.Open(Filename:=Vfile, Local:=True)

it should respect your local settings rather than using US settings as VBA defaults to.

它应该尊重您的本地设置,而不是使用VBA默认的US设置。


推荐阅读
author-avatar
瑶2012瑶_881
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有