VBA Excel更改具有不同值的单元格

时间:2018-12-07 11:51:41

标签: excel vba excel-vba

嗨,我想替换一个具有相同编号的单元格,并将其替换为不同的值。像1846120在Excel中有两行。我想将其中一个更改为5,将另一个更改为10。

  Public Sub ersatt()
        Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
            For i = 1 To 10000
                Select Case ws.Cells(i, 1)
                Case 1741568, 1741984, 1741985, 2267661, 2452546, 2452544, 2239671, 2515828, 2254875, 2553829, 2147721, 2609859, 2055238, 2296699, 2611432
                    ws.Cells(i, 9) = "0"
                Case 1893055, 1868157, 1893055
                    ws.Cells(i, 9) = "=8*F2"
                Case 1846120
                   ws.Cells(i, 9) = "=((F2-90)*29+(90*25))"
                End Select
            Next
        Next ws
    End Sub

2 个答案:

答案 0 :(得分:0)

要写入一个单元格的值,请写... Cells(row,column).Value = X 如果要编写数字变量,请写

 ws.Cells(i, 9).Value = 0

不带“”。如果您写入“ 0”,则单元格将包含字符串“ 0”,编写上述代码将写入数值0。这在您编写变量时很重要。 “选择案例”的论点也是如此

也请在“结束选择”行之后写“下一步” i

答案 1 :(得分:0)

请不要忘记在代码顶部使用“ Option Explicit” 。如果要获取单元格值,请使用 .value

Option Explicit

Public Sub ersatt()

      Dim ws As Worksheet
      Dim i As Long

      For Each ws In ActiveWorkbook.Worksheets

          For i = 1 To 10000
                With ws
                    Select Case .Cells(i, 1)
                    Case 1741568, 1741984, 1741985, 2267661, 2452546, 2452544, 2239671, 2515828, 2254875, 2553829, 2147721, 2609859, 2055238, 2296699, 2611432
                        ws.Cells(i, 9).Value = "0"
                    Case 1893055, 1868157, 1893055
                        .Cells(i, 9).Value = 8 * .Range("F2").Value
                    Case 1846120
                       .Cells(i, 9).Value = ((.Range("F2").Value) - 90) * (29 + (90 * 25))
                    End Select
                End With

          Next i

      Next ws

  End Sub