English 中文(简体)
仅列入数据范围的现有日期和具体时间格式
原标题:Insert current date and specific time format for Data Range only
  • 时间:2024-03-09 01:19:33
  •  标签:
  • excel
  • vba

我正试图使Excel案自动化。 目前,我们得到一本载有客户信息的工作文件。 这是我们的工作量应用所无法使用的形式。

为了将数据转至我们的工作量应用,我们必须修改原始数据(去除栏,填充斜体,如果某个特定单位没有数据,则删除行)。 我设法描述了所有这一切,但引起我问题的是A栏和B栏的时间印章。

在上载模板中,我需要从原始数据中重复数据,然后将A栏的格式改为“文本”,然后改为日期/时间格式“mm/dd/yyyy 12:00”,但仅限于过去的数据范围。

B栏应为“文本”,然后是“mm/dd/yyyy 12:00:00”日期/时间格式,但仅限于过去的数据范围。

我已设法将各栏的格式确定为文本,数据过去,但日期/时间仅显示数字价值(即45365.31)。 它还填补整个各栏的日期/时间(大多数每天有40-50个条目)。

我迄今为止所尝试的法典是:

  Set Column A and B with today s date format
For each cell in range(A:A)
    cell = (Now(), "mm/dd/yyyy 12:00"
next

For each cell in range(B:B)
    cell = (Now(), "mm/dd/yyyy 12:00:00"
next

最新消息(我旁边的Excel):

For Each rng In Sheets("Tracker").Range("A2:A" & Sheets("Tracker").Cells(Rows.Count, "I").End(xlUp).Row)
    If rng.Value <> "" Then
        Columns("A:A").Value = Format(Date "mm/dd/yyyy 12:00")
    End If

    For Each rng In Sheets("Tracker").Range("A2:A" & Sheets("Tracker").Cells(Rows.Count, "I").End(xlUp).Row)
        If rng.Value <> "" Then
            Columns("B:B").Value = Format(Date "mm/dd/yyyy 12:00:00")
        End If

我期望/希望的是A栏和B栏,其范围类似:

问题回答

你的两部法典没有产生预期结果。 他们试图填满A栏和B栏中的所有囚室。

  • The first snippet is missing the Format function.
  • The second snippet lacks the Next statement.

  • Locate the last data row on Col I
  • Search the blank cell(s) on Col A and Col B with SpecialCells(xlCellTypeBlanks), then fill with Date
  • Apply date formatting on Col A and Col B

Microsoft documentation:

Range.SpecialCells method (Excel)

Range。 不动产(Excel)

Sub demo()
    Dim lastRow As Long, visRng As Range, c As Range
    With Sheets("Tracker")
        lastRow = .Cells(.Rows.Count, "I").End(xlUp).Row
        Set c = .Range("A2:B" & lastRow).SpecialCells(xlCellTypeBlanks)
        If Not c Is Nothing Then
            c.Value = Date
            .Columns("A").NumberFormat = "mm/dd/yyyy hh:mm"
            .Columns("B").NumberFormat = "mm/dd/yyyy hh:mm:ss"
        End If
    End With
End Sub




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签