如果是字符串,则将计数值粘贴到偏移列中,并加一以计算下一个值

If string then paste the count value in an offset column and add one to count for next value

希望这是一个简单的过程。在此 For 循环中,我希望 VBA 读取字符串 "Foot Strike" 的第一个范围 (LeftStrike),然后分配值“1”并将分配的值放入偏移单元格中。然后我希望它将 1 添加到指定值并再次 运行 For 循环,寻找下一个 "Foot Strike" 字符串。这样,我就可以看到 "First" 脚踩在哪一行,等等。为了 "copy and paste" 可以这么说,我真的很难理解一件事的 .value = 另一件事的 .value。

Private Sub CommandButton2_Click()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Sheet2")
Dim LeftStrike As Range, FrameLTD As Range, StepNum As Range
Dim lrL As Long, LastFrame As Long
Dim StepCount As Long
StepCount = 1


lrL = ws.Range("H" & ws.Rows.Count).End(xlUp).Row
LastFrame = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

Set LeftStrike = ws.Range("H2:H" & lrL)
Set StepNum = ws.Range("J2:J" & lrL)

For Each FrameLTD In LeftStrike
   If InStr(FrameLTD, "Foot Strike") Then
           ' assign value of "1" to StepCount
           ' somehow get StepCount to = FrameLTD.Offset(0, 3).Value
           ' StepCount = StepCount + 1

    End If
Next FrameLTD

MsgBox "Steps Numbered"
End Sub

谁能告诉我我的代码行应该在 For 循环中才能让我的 "step count" 进入偏移列???

Tim Williams 的解决方案应该有效!

  • 仔细检查您是否looking/referring编码到正确的工作表(它将 在 "Sheet1")
  • 中打印值
  • 这些值将打印在 K 列中,如下所示 您指的范围是 H 列,然后将其偏移 3 列。您没有任何其他代码 运行 会覆盖此列?

代码:

Option Explicit

Private Sub CommandButton2_Click()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Sheet2")
Dim LeftStrike As Range, FrameLTD As Range, StepNum As Range
Dim lrL As Long, LastFrame As Long
Dim StepCount As Long

StepCount = 1

lrL = ws.Range("H" & ws.Rows.Count).End(xlUp).Row
LastFrame = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

Set LeftStrike = ws.Range("H2:H" & lrL)
Set StepNum = ws.Range("J2:J" & lrL)

For Each FrameLTD In LeftStrike
   If InStr(FrameLTD, "Foot Strike") Then
           'assign value of "1" to StepCount
           'somehow get StepCount to = FrameLTD.Offset(0, 3).Value
           FrameLTD.Offset(0, 3).Value = StepCount
           StepCount = StepCount + 1
    End If
Next FrameLTD

MsgBox "Steps Numbered"
End Sub

结果: