Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何更改选定列中的所有单元格值?_Excel_Vba - Fatal编程技术网

Excel 如何更改选定列中的所有单元格值?

Excel 如何更改选定列中的所有单元格值?,excel,vba,Excel,Vba,我正在尝试编写一些代码,在某些条件下将更改选定列中的所有单元格 我的代码可以更改选定的单元格,但在尝试更改所有选定的列时会被压碎 Dim ActSheet As Worksheet Dim MyRange As Range Set ActSheet = ActiveSheet Set MyRange = Selection If MyRange.Cells.Value = "Clothes" Then MyRange.Cells.Value = 2 ElseIf My

我正在尝试编写一些代码,在某些条件下将更改选定列中的所有单元格

我的代码可以更改选定的单元格,但在尝试更改所有选定的列时会被压碎

Dim ActSheet As Worksheet  
Dim MyRange As Range

Set ActSheet = ActiveSheet  
Set MyRange = Selection

If MyRange.Cells.Value = "Clothes" Then    
    MyRange.Cells.Value = 2
ElseIf MyRange.Cells.Value = "Extra" Then
    MyRange.Value = 3
ElseIf MyRange.Cells.Value = "Shoes" Then
    MyRange.Value = 1
End If

例如,当我尝试更改所有列的颜色时,它可以正常工作,但不能更改值。

你需要一个循环,试试这个

Dim ActSheet As Worksheet  
Dim MyRange As Range
Dim TargetCell As Range 

Set ActSheet = ActiveSheet  
Set MyRange = Selection

For Each TargetCell In MyRange.Cells

    If TargetCell.Value = "Clothes" Then    
        TargetCell.Value = 2
    ElseIf TargetCell.Value = "Extra" Then
        TargetCell.Value = 3
    ElseIf TargetCell.Value = "Shoes" Then
        TargetCell.Value = 1
    End If

Next TargetCell

循环选定的单元格,并使用Select Case语句替代If…ElseIf语句

Dim MyRange As Range
Set MyRange = ActiveSheet.Selection

Dim TargetCell As Range 
For Each TargetCell In MyRange.Cells
    Select Case TargetCell.Value 
        Case "Clothes"  
            TargetCell.Value = 2
        Case "Extra"
            TargetCell.Value = 3
        Case "Shoes" 
            TargetCell.Value = 1
    End Select
Next TargetCell

感谢您的帮助,效果很好。我尝试了以下代码: Dim被发现为布尔值

  found = False

  Do Until IsEmpty(ActiveCell)

     If ActiveCell.Value = "Clothes" Then
      ActiveCell.Value = 2
      ElseIf ActiveCell.Value = "Shoes" Then
       ActiveCell.Value = 1
      ElseIf ActiveCell.Value = "Extra" Then
       ActiveCell.Value = 3
        found = True

     End If

     ActiveCell.Offset(1, 0).Select
  Loop

您需要在选定列的单元格中进行循环,并单独处理每个单元格。试一试有很多教程介绍如何循环浏览列的数据。谢谢!现在当循环抛出单元格时效果很好。如果它回答了您的问题,请向上投票/将其标记为已解决:您可能会从阅读中受益。使用select是所有这些解决方案中最糟糕的。请看一下其他答案。如果不使用,您的代码会更快。选择!