Home Excel VBA VBA Program to Swap Values ​​Between Two Excel Cells

VBA Program to Swap Values ​​Between Two Excel Cells

by Nguyen Duc Thanh
A+A-
Reset

In your daily work, sometimes you need to swap values ​​between two Excel cells quickly. Instead of doing it manually, you can use VBA to automate this process. The following article will guide you on how to write a VBA program to swap values ​​between Excel cells, along with step-by-step explanations to help you understand better.

1. VBA Program to Swap Values ​​Between Two Excel Cells

Step 1: Create a Command Button

  • Open an Excel file.
  • Go to the Developer tab on the ribbon.
  • Select Insert > ActiveX Controls > Command Button and draw the button on the worksheet.

Step 2: Write VBA Code

  • Double-click the Command Button to open the VBA Editor.
  • In the editor, enter the following code:
Private Sub CommandButton1_Click()
    Dim temp As Variant
    temp = Range("A1").Value
    Range("A1").Value = Range("B1").Value
    Range("B1").Value = temp
End Sub
  • Press Ctrl + S to save the Excel file as .xlsm (macro-enabled format).

Step 3: Activate the Macro

  • Turn off Design Mode in the Developer tab.
  • Click the Command Button to swap values between cells A1 and B1.

Code Explanation

  • Dim temp As Variant – Declares the variable temp to temporarily store a value.
  • temp = Range(“A1”).Value – Assigns the value in cell A1 to the temporary variable.
  • Range(“A1”).Value = Range(“B1”).Value – Copies the value from B1 to A1.
  • Range(“B1”).Value = temp – Copies the value stored in the temporary variable to B1.

2. Swapping Values Between Multiple Cells

You can extend the code to swap values between multiple cells within the same column, row, or data range:

Sub SwapMultipleCells()
    Dim temp As Variant
    Dim i As Integer
    For i = 1 To 10
        temp = Cells(i, 1).Value
        Cells(i, 1).Value = Cells(i, 2).Value
        Cells(i, 2).Value = temp
    Next i
End Sub

The code above swaps values between columns A and B from row 1 to row 10.

4. Swapping Values Based on Conditions

You can also add conditions to check values before performing the swap:

Sub ConditionalSwap()
    Dim temp As Variant
    If Range("A1").Value > Range("B1").Value Then
        temp = Range("A1").Value
        Range("A1").Value = Range("B1").Value
        Range("B1").Value = temp
    End If
End Sub

In this example, values are swapped only if the value in cell A1 is greater than the value in B1.

The VBA program to swap the values ​​of two Excel cells is a simple yet effective example of how to use VBA to automate tasks. With extensions and practical applications, you can apply it to optimize many different tasks in Excel. Practice and explore more!

Bài viết cùng chuyên mục

Leave a Comment

Về chúng tôi

Chào mừng các bạn đến với “Hoa ngữ Phổ truyền ” – nơi chúng tôi luôn cố gắng giúp bạn học tiếng Trung một cách tốt nhất! Chúng tôi hân hạnh được chia sẻ với cộng đồng những kiến thức, tài liệu, phần mềm trải nghiệm học tiếng Trung và văn hóa truyền thống Trung Hoa.

@2023 – Bảo lưu mọi quyền. Được thiết kế và phát triển bởi hoanguphotruyen