简体   繁体   中英

VBA Macro - I want to change each cell value to a new one in a specific range… how to do it? Excel

I have a range that is B3:B500 I want to change the value in each cell in that range The range however, is dynamic so I need to look from B3 to last row

First: How do I get it to change the range to work to last row rather than preset range?

Second: I want to change each individual cell value to something like this:

myCell.Value = "=" & Chr(34) & Chr(61) & myCell.Value & Chr(34)

How do I get it to go through cell by cell to make change to each cell in the "dynamic" range we just created?

Appreciate all the help I can get... pretty new at VBA so please bear that in mind and keep it simple :)

Ie Cell b3 contains: "ASP" (Text only) Change to: ="="ASP" (formula instead giving result =ASP)

Dim lastRow As Integer: lastRow = 3
Dim myCell As Range
'determine last row of current region
Do While(Not IsEmpty(Cells(lastRow + 1, 2)))
    lastRow = lastRow + 1
Loop
'loop through the range
For Each myCell In Range(Cells(2, 2), Cells(lastRow, 2))
    myCell.Value = "=" & Chr(34) & Chr(61) & myCell.Value & Chr(34)
Next myCell

In this code, first you determine last row using While loop, which loops until the last row in column is found. Then use For Each loop, where you loop through the range, from third row to last row in B column.

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