通过每一列循环IF语句

我下载了调查结果并把它们放到Excel中。

我可以将这个macros应用到“A”列。

Private Sub CommandButton1_Click() counter = 2 'moves output For Each n In [A7:A50] 'loops through cell in specified range If n < 400 Then Sheets("Output").Cells(counter, "B") = 0 'Output to other sheet, = points awarded ElseIf n > 400 Then Sheets("Output").Cells(counter, "B") = 3 'Output to other seet, = points awarded End If counter = counter + 1 'moves counter up 1 Next End Sub 

“BR”与“A”栏具有相同的条件。 而不是input这个代码

 Sheets("Output").Cells(counter, "B") = 3 'Output to other seet, = points awarded 

只需切换“A”和输出栏,是否可以循环我的计数器和我的if语句。

我的脚本查看“结果工作表”中的列/行A7:A50,在“输出工作表”的B列中生成点。

我一直无法看到列B(结果工作表),然后输出到列C(输出工作表),然后查看列C(结果工作表),然后输出到D列(输出工作表)。

也许这会帮助你:

 Private Sub CommandButton1_Click() Dim rng As Range Dim i As Integer Set rng = Range("A:R") ' define your range For i = 1 To rng.Columns.Count ' loop through the columns counter = 2 'moves output For Each n In Range(Cells(7, i), Cells(50, i)) 'loops through cell in specified column i If n < 400 Then Sheets("Output").Cells(counter, i + 1) = 0 'Output to other sheet, = points awarded ElseIf n > 400 Then Sheets("Output").Cells(counter, i + 1) = 3 'Output to other seet, = points awarded End If counter = counter + 1 'moves counter up 1 Next Next i End Sub 

我已经添加了一个遍历范围A:R中的每个列的循环,并将其应用于每个列。 我已经在if语句中replace了“B”,所以结果将被应用到期望的列:

评估列“A” – >更新列“B”,

评估栏“B” – >更新栏“C”…

这有帮助吗?

编辑答案,希望能解决你的问题。 我看到了一些东西。

首先,我相信你需要在结果进入之前激活每个工作表。其次,在识别你想要输出的单元格之后使用.value。 第三个Cells()函数使用整数,所以“B”应该是2。

尝试使用下面的代码,看看是否适合你。


 Private Sub CommandButton1_Click() Dim r as Long Dim c as Long Dim lastrow as Integer Dim lastcol as Integer Dim cpath as String cpath = Worksheets("SHEETNAME") 'less typing later on cpath2 = Worksheets("Output") lastrow = WorksheetFunction.CountA(Range("A:A")) lstcolm = WorksheetFunction.CountA(cpath.Rows(1).EntireRow) cpath.Cells(2,1).Activate 'assuming you have a header row For c = 1 to lastcol For r = 2 to lastrow If cpath.cells(r,c).value < 400 Then cpath2.Activate cpath2.Cells(r, 2).value = 0 ElseIf n > 400 Then cpath2.Activate cpath2.Cells(r, 2).value = 3 End If cpath.activate next r next c End Sub