简体   繁体   中英

VBA excel change cell with different value

Hi I want to replace a cell that have same number and replace it with diffrent value. Like 1846120 have two row in the excel. I want to change one of them to 5 and the other one to 10. How?

  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

to write the value of a cell, write...Cells(row, column).Value = X If you want to write a numeric variable, write

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

without the "". If your write "0" the cell will contain the string '0', writing the above code will write the numeric value 0. This becomes important when you write variables. Same goes for the argument of "Select Case"

Also write Next i after the line that says "End Select"

Dont forget to use "Option Explicit" at the top of your code.also if you want to get cell value use .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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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