如何select列并使用VBAmacros显示其当前格式?

请在下面find我的要求,我无法find任何解决scheme:

1.从工作簿中迭代workSheet
2.使用当前格式/列types查找包含date值的所有列
(这是一个技巧,工作表不是静态的,它可以包含任意数量的包含date值的列,包含date值的列可以有任何名称,而且这样的工作表可以是多个)
3.如果“标志”值为“y”,则在date格式上应用macros(如下macros)

<code> Sub FormatDate() If wksSecDist.Range("Flag").value = "y" Then LastRowColA = Range("X" & Rows.Count).End(xlUp).Row ' Here I am finding total number of rows in column X wksSecDist.Range("X2", "X" & LastRowColA).NumberFormat = "dd/mmm/yyyy" ' Here applying specified date format to Range("X2", "X10") [if last row index for column X is 10] End If End Sub </code> 

我只是VBA的初学者。
提前致谢。

我怀疑你在互联网上找不到解决scheme,因为你只是简单地寻找一个解决scheme,而不是构build你自己的解决scheme所需的部分。

你提到你是一个VBA初学者,请采取下面的答案是教育使用,并开始让你在哪里你需要你的工具。 请注意,如果由于未包含的信息而无法回答您的问题,它仍然回答您的问题,缺less的信息应构成新问题的一部分。 也就是说,让这个函数启动并运行。

从你写的东西我已经解释的要求是:

  1. 查看工作簿中的所有工作表(“ 工作表可以有多个 ”)
  2. 检查每一列,看看它是否有一个date值
  3. 如果是,请将整列设置为特定的格式

完成这个所需要的是迭代(循环),循环遍历所有工作表,另一个循环遍历所有列:

这是目标的伪代码:

。对于Worksheet Workbook每个Worksheet

..对于Worksheet每个Column

…如果Column包含date,则根据需要进行格式化

..进程下一column

处理下一个Worksheet

我们使用一个variables来引用一个Worksheet并使用一个循环(For Each)来改变引用。 列也一样。

 Public Sub Sample() Dim WkSht As Excel.Worksheet Dim LngCols As Long Dim LngCol As Long 'This loop will process the code inside it against every worksheet in this Workbook For Each WkSht In ThisWorkbook.Worksheets 'Go to the top right of the worksheet and then come in, this finds the last used column LngCols = WkSht.Range(WkSht.Cells(1, WkSht.Columns.Count).Address).End(xlToLeft).Column 'This loop will process the code inside it against every column in the worksheet For LngCol = 1 To LngCols 'If the first cell contains a date then we should format the column If IsDate(WkSht.Cells(2, LngCol)) Then 'Set right to the bottom of the sheet WkSht.Range(WkSht.Cells(2, LngCol), WkSht.Cells(WkSht.Rows.Count, LngCol)).NumberFormat = "dd/mmm/yyyy" End If Next Next End Sub 

希望这一切都是有道理的,这样做的前提是标题行始终是第一行,并且列中没有空白,但是这些是您准备好时可以处理的单独问题。