查找单元格,粘贴并格式化时间值

时间:2016-01-08 16:18:29

标签: excel-vba time offset vba excel

我想在文本框中输入3位或4位数值,验证它是否为数字,将数字拆分为1位或2位数分钟数和2位数秒数。然后,我想在电子表格中找到命名区域,移动到该范围的右端,然后粘贴时间值,从该单元格向下偏移多个单元格。我还想将我粘贴的单元格格式化为分钟和秒钟单元格。大部分都可以,但我在小程序的最后有一个偏移粘贴和格式的问题。 现在的问题是,将TextBox2值粘贴到该单元格中。

Dim tbV As String
Dim sV As String
Dim mV As String
Dim TimeCell As Range
Dim LastColl As Range

tbV = TextBox2.Text

    If Len(tbV) > 4 Or Len(tbV) < 2 Or Not IsNumeric(tbV) Then
        MsgBox "wrong"
    Exit Sub
    End If


sV = Right(tbV, 2)
mV = Left(tbV, Len(tbV) - 2)


Dim iOffset As Integer
    Select Case Range("AC1").Value
        Case Is = 30: iOffset = 16
        Case Is = 33: iOffset = 20
        Case Is = 22: iOffset = 14
    End Select

Set LastColl = Range("Battery" & BatteryNumber).End(xlToRight)

Set TimeCell = Range(LastCol.Offset(iOffset, 0))

TimeCell.NumberFormat = "m:ss"
TimeCell.Value = TimeSerial(0, Val(mV), Val(sV))

1 个答案:

答案 0 :(得分:1)

替换此行:

Set LastColl = Range("Battery" & BatteryNumber).End(xlToRight)

用这个:

Dim Rng As Range
 Set Rng = Range("Battery" & BatteryNumber)
 Set LastColl = Rng(Rng.Columns.Count)