简体   繁体   English

Excel VBA 根据另一个单元格的值逐行更改一个单元格的值

[英]Excel VBA change the value of a cell based on the value of another cell row by row

Consider the screenshot attached: enter image description here考虑随附的屏幕截图:在此处输入图像描述

I would like to add a button to the worksheet that, when clicked, it would go row by row checking if the first column of the row contains "Italy" or "Spain".我想在工作表中添加一个按钮,单击该按钮时,它将逐行检查该行的第一列是否包含“意大利”或“西班牙”。 If it does, it should set its points (cell next to it) to 0.如果是这样,它应该将其点(旁边的单元格)设置为 0。

I would like to do it with some kind of loop or iteration (not with a bunch of If/Else If), but I'm not an expert on VBA, I'm learning on my own and I honestly don't know how to do it.我想用某种循环或迭代来做(不是用一堆 If/Else If),但我不是 VBA 方面的专家,我自己学习,老实说我不知道如何去做吧。

Thank you for your time.感谢您的时间。

As long as you know how to go to Insert , Shapes , pick any shape (basic rectangles, circles or anything else) and place it it your sheet, you can right click on the shape and asign macro.只要您知道如何将 go InsertShapes 、选择任何形状(基本矩形、圆形或其他任何形状)并将其放置在您的工作表中,您就可以右键单击形状并分配宏。

You can paste this macro into VBA macro editor and save, and then run or do as I said to asign it to the shape where you can click at will.您可以将此宏粘贴到 VBA 宏编辑器中并保存,然后运行或按照我说的将其分配给您可以随意单击的形状。

Im sure others could give you a better macro.我相信其他人可以给你一个更好的宏。 This is very basic.这是非常基本的。 And looping over values is very inificient & slow, but it does what you asked for.并且循环值非常低效且缓慢,但它可以满足您的要求。

Sub MyFirstMacro()
    Dim myCell As Range
    Dim LastRow As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row

    For Each myCell In Range("A1:A" & LastRow)
     If IsError(myCell.Value) Then
      myCell.Offset(0, 1) = "Do you know you had an error in " & myCell.Address & "???"
         Else:
            If myCell.Value = "Italy" Or myCell.Value = "Spain" Then
            myCell.Offset(0, 1) = 0
            End If
     End If
    Next myCell
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM