无法将excel单元格转换为“hh:mm”

时间:2016-08-31 05:53:48

标签: excel vba

我将单元格值转换为hh:mm格式时遇到问题。

这是我的代码:

lastRow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
lastColumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count

sql = "INSERT INTO temp (colId,rowId,kodp,data) "
plansql = "INSERT INTO tempPlan (colId,rowId,kodp,data) "
sql3 = "DELETE From temp"
sql4 = "DELETE From tempPlan"
conn.Execute sql3
conn.Execute sql4
For i = 13 To lastRow
    kodp = Worksheets(2).Cells(i, 1)
    For j = 6 To lastColumn
        data = Worksheets(2).Cells(11, j)
        sql = sql & "SELECT " & j & "," & i & "," & kodp & ",'" & data & "' UNION ALL "
    Next
    i = i + 10
Next
For i = 12 To lastRow
    kodp = Worksheets(2).Cells(i, 1)
    For j = 6 To lastColumn
        data = Worksheets(2).Cells(11, j)
        plansql = plansql & "SELECT " & j & "," & i & "," & kodp & ",'" & data & "' UNION ALL "
        'sql = sql & "SELECT " & j & "," & i & "," & kodp & ",'" & data & "' UNION ALL "
    Next
    i = i + 10
Next
sql = Left(sql, Len(sql) - 10)
plansql = Left(plansql, Len(plansql) - 10)
conn.Execute sql
conn.Execute plansql
sql2 = "SELECT t.rowId,t.ColID,Realization From FRM_Raport f INNER JOIN temp t ON f.EmployeeId=t.kodp and f.data=t.data"
sql5 = "SELECT t.rowId,t.colId,[Plan] From FRM_Raport f INNER JOIN tempPlan t ON f.Employeeid=t.kodp and f.data=t.data"
Set tmp = conn.Execute(sql2)

Do Until tmp.EOF
    For Each fld In tmp.Fields
        Worksheets(2).Cells(tmp.Fields(0), tmp.Fields(1)) = tmp.Fields(2)
    Next fld
    tmp.MoveNext
Loop

tmp.Close
Set tmp = Nothing

Set planRecordset = conn.Execute(sql5)



Do Until planRecordset.EOF
    For Each fld In planRecordset.Fields
        Worksheets(2).Cells(planRecordset.Fields(0), planRecordset.Fields(1)) = planRecordset.Fields(2)
    Next fld
    planRecordset.MoveNext
Loop

planRecordset.Close
Set planRecordset = Nothing

我需要将Worksheets(2).Cells(i,j)(i = 12到lastRow,j = 6到lastColumn,i = 13到lastRow,j = 6到lastColumn)转换为“hh:mm”格式。我试过读取数据类型,它是Range或Empty。我也尝试将这些单元格首先转换为int,但它也没有用。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在这里给出条件.... 然后给, 工作表(2).Cells(i,j)=格式(THE_DATE_VARIABLE,“hh:mm”)

试试这个。