查找单元格,粘贴时间值并设置格式
Find cell, paste and format a time value
我想在文本框中输入一个 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))
替换此行:
Set LastColl = Range("Battery" & BatteryNumber).End(xlToRight)
有了这个:
Dim Rng As Range
Set Rng = Range("Battery" & BatteryNumber)
Set LastColl = Rng(Rng.Columns.Count)
我想在文本框中输入一个 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))
替换此行:
Set LastColl = Range("Battery" & BatteryNumber).End(xlToRight)
有了这个:
Dim Rng As Range
Set Rng = Range("Battery" & BatteryNumber)
Set LastColl = Rng(Rng.Columns.Count)