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

VBA中的CDate功能问题-ProblemwithCDatefunctioninVBA

ImusingCDatetoconvertaparticulardateformattedasstringtotheExcelDatetype.Iwroteas

I'm using CDate to convert a particular date formatted as string to the Excel Date type. I wrote a small UDF for this conversion; however when I run the function as a in-cell function, it returns a number and the calling cells default format is set to Number type. When I manually convert it to Date type, Excel will display the correct date entry.

我正在使用CDate将格式化为字符串的特定日期转换为Excel日期类型。我为这次转换写了一个小UDF;但是当我将该函数作为单元格函数运行时,它返回一个数字,并且调用单元格的默认格式设置为数字类型。当我手动将其转换为日期类型时,Excel将显示正确的日期条目。

So, I need to know whether there is a way to set the default format of the calling cell to Date type through my Date conversion macro. Otherwise, the user has to manually change the format of each of cell.

所以,我需要知道是否有办法通过我的日期转换宏将调用单元格的默认格式设置为日期类型。否则,用户必须手动更改每个单元格的格式。

e.g

例如

 Function Test()
   Test = CDate("2010/12/23")
 End Function

4 个解决方案

#1


1  

The calling cell is exposed via Application.ThisCell however your going to have to manually format before/after the call as an Excel UDF cannot modify the physical characteristic of a cell.

调用单元通过Application.ThisCell公开,但是您必须在调用之前/之后手动格式化为Excel UDF,无法修改单元格的物理特性。

#2


1  

Perhaps you can run something after the cells have been entered?

也许你可以在输入细胞后运行一些东西?

Dim c As range
For Each c In ActiveSheet.UsedRange.Cells
    ''Case sensitive
    If c.Formula = "=test()" Then
        c.NumberFormat = "d/mm/yyy"
    End If
Next

#3


0  

It sounds like what you want to do is actually a two-part process: convert a value in a cell (or range of cells) to a date and then display it as a date, as opposed to simply converting a text value that looks like a date to a date value.

听起来你想要做的事实上是一个由两部分组成的过程:将单元格(或单元格范围)中的值转换为日期,然后将其显示为日期,而不是简单地转换看起来像的文本值日期值的日期。

If that is the case, then I would recommend modifying Remou's suggestion like so (using UsedRange can be problematic on worksheets containing large amounts of data):

如果是这种情况,那么我建议像这样修改Remou的建议(使用UsedRange在包含大量数据的工作表上可能会有问题):

Dim c As Range
For Each c In Selection.Cells
    c.Value = CDate(c.Value)
    c.NumberFormat = "m/d/yyyy"
Next c

The user would then need to select the cells to which the formatting should be applied and run the macro; it sounds as though you do not wish this to happen, but I'm not sure that is possible.

然后,用户需要选择应该应用格式的单元格并运行宏;听起来好像你不希望这种情况发生,但我不确定这是否可能。

Depending on how you want to use the macro, you can add additional checks: for example, you could apply the formatting only to non-blank cells currently formatted as text (c.Value <> "" and c.NumberFormat = "@").

根据您希望如何使用宏,您可以添加其他检查:例如,您可以仅将格式应用于当前格式为文本的非空白单元格(c.Value <>“”和c.NumberFormat =“@” )。

#4


0  

If you return the data as a string and in a format that Excel automatically converts to a date, you can get the cell to format properly.

如果以字符串形式返回数据,并且Excel将自动转换为日期格式,则可以使单元格正确格式化。

Function Test() As String
  Test = Format(DateValue("2010/12/23"), "mm/dd/yyyy")
End Function

This works in US Excel, but if you need it to be language agnostic, I think you'll have problems.

这适用于美国Excel,但如果您需要它与语言无关,我认为您会遇到问题。


推荐阅读
  • 本文详细介绍了中央电视台电影频道的节目预告,并通过专业工具分析了其加载方式,确保用户能够获取最准确的电视节目信息。 ... [详细]
  • 本文探讨了如何在Classic ASP中实现与PHP的hash_hmac('SHA256', $message, pack('H*', $secret))函数等效的哈希生成方法。通过分析不同实现方式及其产生的差异,提供了一种使用Microsoft .NET Framework的解决方案。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 在过去两周中,我们利用 ReportViewer 开发了与生产良率相关的报表,其中每个制程的直通率是所有测试项良率的乘积。由于 ReportViewer 没有内置的累乘函数,因此需要借助自定义代码来实现这一功能。本文将详细介绍实现步骤和相关代码。 ... [详细]
  • 开发笔记:小程序分类页实现三级分类,顶部导航栏,左侧分类栏,右侧数据列表
    开发笔记:小程序分类页实现三级分类,顶部导航栏,左侧分类栏,右侧数据列表 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文由瀚高PG实验室撰写,详细介绍了如何在PostgreSQL中创建、管理和删除模式。文章涵盖了创建模式的基本命令、public模式的特性、权限设置以及通过角色对象简化操作的方法。 ... [详细]
  • 获取计算机硬盘序列号的方法与实现
    本文介绍了如何通过编程方法获取计算机硬盘的唯一标识符(序列号),并提供了详细的代码示例和解释。此外,还涵盖了如何使用这些信息进行身份验证或注册保护。 ... [详细]
  • 实用正则表达式有哪些
    小编给大家分享一下实用正则表达式有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下 ... [详细]
  • 利用R语言进行股票价格数据的线性回归分析
    本文介绍了如何使用R语言对Excel中的股票价格数据集执行线性回归分析。通过具体的代码示例,展示了数据的导入、处理及模型构建的过程。 ... [详细]
  • 探讨了汉字在计算机系统中的字符占用情况,以及在使用SQL Server 2000进行特定条件查询时遇到的问题,特别是针对姓氏为‘刘’的学生记录的查询。 ... [详细]
author-avatar
手机用户2602881147
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有