When performing cell searches or navigation in VBA, you often encounter situations where you want to “move to another sheet or cell temporarily to do a task, and then return to the original location when finished.”
For example, you might want to search for information in a different sheet based on an input value, display it, and then return to the input position to continue editing.
In this article, I will introduce how to use Application.GoTo in VBA to temporarily jump to another cell and return to the original position after the process is complete.
Basic Code: Recording the Original Position and Returning After the Jump
Sub JumpAndReturn()
Dim originCell As Range
Dim matchedCell As Range
' Record the original cell (ActiveCell)
Set originCell = ActiveCell
' Search for the value of the original cell in Column C of the "Products" sheet
Set matchedCell = Worksheets("Products").Columns("C").Find(What:=originCell.Value)
If Not matchedCell Is Nothing Then
' Jump to the found cell
Application.GoTo matchedCell
' Perform arbitrary processing (Example: Display a message)
MsgBox "Data Found: " & matchedCell.Offset(0, 1).Value
' Return to the original cell
Application.GoTo originCell
Else
MsgBox "No matching data found."
End If
End Sub
Key Points of the Code
- Set originCell = ActiveCellIt is essential to record the currently selected cell before moving.
- Find FunctionSearches for a matching value in a specific column of the target sheet (e.g., “Products”).
- Application.GoTo matchedCellTemporarily jumps to the cell found by the search.
- Application.GoTo originCellReturns to the originally recorded cell.
Advanced: Moving Across Sheets
This method fully supports moving across different sheets. It is useful for the following purposes:
- Searching a Master Sheet based on an input cell.
- Referencing detailed information linked to a Customer ID on another sheet.
- Automatically returning to the original input field after checking the search results.
Important Notes
| Item | Details |
| Save ActiveCell | You must record the current position before moving. |
| Find Range | Narrowing the range by specifying columns (e.g., Columns("C")) makes the process faster. |
| Error Handling | Always use If Not matchedCell Is Nothing Then to handle cases where data is not found. |
Example Use Cases
- Referencing master data corresponding to an input value and returning.
- Moving from an error location to the corresponding information, and returning after correction.
- Implementing navigation buttons with jump processing.
Summary
| Action | Code Example |
| Record Original Cell | Set originCell = ActiveCell |
| Jump to Another Cell | Application.GoTo matchedCell |
| Return to Original | Application.GoTo originCell |
As shown above, using Application.GoTo allows you to easily implement temporary jumps and returns. This is very useful when you want to smooth the flow of master data searches or confirmation processes and increase work efficiency.
