我有一个excel文件,其格式为" 2016年3月6日和#34;我想转换为" d / MM / yyyy"或" 6/3/2016"为了使用像public class TestSentenceCounter
{
private static final String SENTENCE1 = "This is my sentence.";
private static final String SENTENCE2 = "These words make another sentence that is longer";
private SentenceCounter sc1;
private SentenceCounter sc2;
/**
* Create two instances we can play with
*/
@Before
public void setup()
{
sc1 = new SentenceCounter(SENTENCE1);
sc2 = new SentenceCounter(SENTENCE2);
}
/**
* Make sure the instance variable is correct
*/
@Test
public void testConstructor()
{
assertEquals(SENTENCE1, sc1.getSentence());
assertEquals(SENTENCE2, sc2.getSentence());
}
@Test
public void testFirstBlankPosition()
{
assertEquals(4, sc1.firstBlankPosition());
assertEquals(5, sc2.firstBlankPosition());
}
}
----------------------------------------------------
public class SentenceCounter
{
public String sentence;
public SentenceCounter(String sentence)
{
this.sentence = sentence;
}
public Object getSentence()
{
return sentence;
}
public Object firstBlankPosition()
{
return null;
}
}
这样的excel公式来提取部分日期。
我写了一个小宏来帮我解决这个问题,它只是替换日期,就像在输入数据集中手动替换日期一样。
DATEVALUE()
结果是不稳定的数据集。请查看之前和之后的图像。
有些人正在正确转换,而其他人正在互相交换月份和日期。我以前在不使用宏的情况下替换月份时不会发生这种情况。 Excel中的默认日期格式按照我想要的格式设置。
日期的系统区域设置:
答案 0 :(得分:1)
由于您的系统和Excel语言似乎是英语,因此直接CDate
方法可能对您有用。对我来说,它不起作用,因为我的系统不知道英文月份名称。所以我必须用数字真正地替换它们:
Sub MonthReplace()
Dim i As Long
Dim monthArray As Variant
Dim c As Range
Dim strDate As String
monthArray = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
For Each c In Range("RawDataset")
strDate = Replace(c.Value, " ", "")
strDate = Replace(strDate, ",", "")
For i = LBound(monthArray) To UBound(monthArray)
strDate = Replace(strDate, monthArray(i), "/" & (i + 1) & "/", , , vbTextCompare)
If IsDate(strDate) Then Exit For
Next i
If IsDate(strDate) Then c.Value = CDate(strDate)
Next
End Sub
答案 1 :(得分:0)
转换每个单元格可能更可靠:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="selectleft">
<option value="categorie">categorie</option>
<option value="categorie1">sport</option>
<option value="categorie2">movies</option>
<option value="categorie3">news</option>
</select>
<div class="category categorie">article(categorie option 1 default selected)</div>
<div class="category categorie1">article 1(sport option 2)</div>
<div class="category categorie1">article 2(sport option 2)</div>
<div class="category categorie2">article 1(movies option 3)</div>
<div class="category categorie2">article 2(movies option 3)</div>
<div class="category categorie3">article 3(news option 4)</div>
<div class="category categorie3">article 3(news option 4)</div>
答案 2 :(得分:0)
尝试了各种方法之后,最后决定将06 March, 2016
转换为06-March-2016
,使其在Excel(我的主要目标)中可用,明确说明月份以避免VBA日期格式问题。< / p>
Sub MonthReplace()
Dim res As Boolean
Dim i As Long
Dim endRow As Long
Dim columnArray As Variant
' only work on columns containing the dates
columnArray = Array("W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AL", "AM")
' find the last cell with data in the current sheet
endRow = ActiveCell.SpecialCells(xlLastCell).Row
For i = LBound(columnArray) To UBound(columnArray)
With Range(columnArray(i) & "3:" & columnArray(i) & endRow)
res = .Replace(", ", "-")
res = .Replace(" ", "-")
End With
Next i
End Sub
另外,根据+ Axel Richter的答案,检查Cell.Value
中的错误并确保最后4个字符是数字,我写了以下内容。但是,由于检查了每个单元格,因此该方法非常慢。可以使用上述策略(范围中的选定列)来提高速度。
Sub MonthReplace_slow()
Dim i As Long
Dim monthArray As Variant
Dim c As Range
Dim strDate As String
monthArray = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
For Each c In Range("RawDataset")
strDate = ""
If Not IsError(c.Value) Then
strDate = c.Value
If IsNumeric(Right(strDate, 4)) Then
strDate = Replace(strDate, " ", "")
strDate = Replace(strDate, ",", "")
For i = LBound(monthArray) To UBound(monthArray)
strDate = Replace(strDate, monthArray(i), "/" & (i + 1) & "/", , , vbTextCompare)
If IsDate(strDate) Then Exit For
Next i
If IsDate(strDate) Then c.Value = CDate(strDate)
End If
End If
Next
End Sub
我没有使用其他CDate
方法。