簡體   English   中英

Excel VBA-無法使用相同公式自動填充整個表格列的代碼

[英]Excel VBA - Code that does not autofill entire table column with the same formula

我正在使用一個表對象,並且我需要代碼來對第3列進行排序,並更改日期(mmm-d)讀為“ Jan-0”的情況。 因為我正在使用表對象,所以每次嘗試引用列“ Clock Out”(E)時,其格式均如下:

m / d / yyy hh:mm:ss AM / PM,

使用.formulaR1C1代碼段,它將第3列中的每個單元格更改為該公式。 這是一個問題,因為我希望C列(日期)由D列中的值(Clock In)確定,或者,如果D列為空,那么我希望它由E列中的值填充(時鍾)。

D列和E列都不為空。

currentSht.ListObjects.Add(xlSrcRange, Range(Cells(1, 1), Cells(erow, ecol)), , xlYes).Name = "Table1"
Set lst = currentSht.ListObjects("Table1")
lst.ListColumns.Add Position:=3
currentSht.Range("C2").Value = "=TEXT([@[Entry Time]],""mmm-d"")"

lstHeaders = Array("", "ID", "Agent Name", "Date", "Clock In", "Clock Out")
For i = 1 To 5
lst.ListColumns(i).Name = lstHeaders(i)
Next i

***Concerning portion of code***
For i = erow to 1 step -1
    if cells(i,3).TEXT = "Jan-0" Then
        cells(i,3).Value = "=([@[Clock Out]],""mmm-d"")"
    end if
next i

End Sub

我想通了,這就是竅門:

For i = erow To 1 Step -1
    If Cells(i, 3).Text = "Jan-0" Then
       Application.AutoCorrect.AutoFillFormulasInLists = False '<~~ THIS LINE DID IT 
       Cells(i, 3).Value = "=TEXT([@[Clock Out]],""mmm-d"")"
    End If
Next i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM