VBA to Update data in Excel

Here’s an example of VBA code that updates data in an Excel worksheet:

Sub UpdateDataInExcel()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim dataRange As Range
    Dim targetCell As Range
    Dim newValue As String
    
    ' Set the workbook and worksheet objects
    Set wb = ThisWorkbook ' Assumes the code is running in the same workbook
    Set ws = wb.Worksheets("Sheet1") ' Replace "Sheet1" with the name of your worksheet
    
    ' Set the range where the data is located
    Set dataRange = ws.Range("A1:B10") ' Replace with the range containing your data
    
    ' Set the target cell where you want to update the value
    Set targetCell = ws.Range("C3") ' Replace with the cell where you want to update the value
    
    ' Set the new value to update
    newValue = "New Value" ' Replace with the new value you want to update
    
    ' Find the target cell within the data range
    Dim cell As Range
    For Each cell In dataRange
        If cell.Value = targetCell.Value Then
            ' Update the value
            cell.Offset(0, 1).Value = newValue ' Assuming the value to update is in the next column (offset by 1)
            Exit For
        End If
    Next cell
End Sub

In this code, you need to modify the following parts:

  • Replace "Sheet1" with the name of your worksheet where the data is located.
  • Replace ws.Range("A1:B10") with the range that contains your data. This specifies the range within which the target cell will be searched.
  • Replace ws.Range("C3") with the target cell where you want to update the value.
  • Replace "New Value" with the new value you want to update.

When you run this code, it will search for the target cell within the specified data range and update the adjacent cell (offset by 1 column) with the new value.

Note: Make sure that the workbook and worksheet names, as well as the range references, are accurate and match your actual data in the Excel file.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech