我需要能够遍历我的行(特别是B列),并使用某个单元格中的数字来使用该行中的其他单元格来执行特定的功能。例如,规则#1表示我需要在Rule#旁边的单元格中找到路径的最后修改日期,但每个规则的任务都不同。
我是VBA的新手,我一直在努力设置一个循环并将变量传递给不同的潜艇,并且非常感谢任何帮助。为了清楚起见,我正在寻找循环和传递变量的语法帮助
谢谢!
参考图片:The spreadsheet
The attempt at sketching out the code
Private Sub CommandButton1_Click()
Dim x As Integer
NumRows = Range("B2", Range("B2").End(xlDown)).Rows.Count
Range("B2").Select
For x = 1 To NumRows
If Range(RowCount, 1).Value = 1 Then
RuleOne (RowCount)
End If
Next
'Dim RowCount As Integer
'RowCount = 1
'Worksheets("Sheet2").Cells(1, 2) = Worksheets("Sheet1").UsedRange.Row.Count
'While RowCount < Worksheets("Sheet1").Rows
'If Worksheets("Sheet1").Cells(RowCount, 1).Value = 1 Then
'RuleOne (RowCount)
'End If
'Wend
End Sub
Sub RuleOne(i As Integer)
'use filedatetime and path from i cell
'Worksheets("Sheet2").Cells(1, 1) = FileDateTime(C, i)
Worksheets("Sheet2").Cells(1, 1) = "hello"
End Sub
Sub RuleTwo(i As Integer)
Worksheets("Sheet2").Cells(1, 1) = "hello"
End Sub
答案 0 :(得分:0)
尝试将Range(RowCount, 1).Value = 1
更改为Cells(x, 2).Value = 1
。
答案 1 :(得分:0)
变量RowCount尚未初始化/设置。 我假设这个变量是B列中的数字
RowCount = Cells(x, "B").Value
我还注意到变量NumRows似乎比它应该少一个(所以如果最后一行是1,它会跳过它)。所以我改用了它:
NumRows = Cells(Rows.Count, "B").End(xlUp).Row
所以试试这段代码:
Sub CommandButton1_Click()
Dim x As Integer
NumRows = Cells(Rows.Count, "B").End(xlUp).Row
For x = 1 To NumRows
RowCount = Range("B" & x).Value
If RowCount = 1 Then
RuleOne (x)
End If
Next
'Dim RowCount As Integer
'RowCount = 1
'Worksheets("Sheet2").Cells(1, 2) = Worksheets("Sheet1").UsedRange.Row.Count
'While RowCount < Worksheets("Sheet1").Rows
'If Worksheets("Sheet1").Cells(RowCount, 1).Value = 1 Then
'RuleOne (RowCount)
'End If
'Wend
End Sub
Sub RuleOne(i As Integer)
'use filedatetime and path from i cell
'Worksheets("Sheet2").Cells(1, 1) = FileDateTime(C, i)
Worksheets("Sheet2").Cells(1, i) = "hello"
End Sub
Sub RuleTwo(i As Integer)
Worksheets("Sheet2").Cells(1, 1) = "hello"
End Sub