Friday, March 15, 2013

How to Use ActiveCell in Excel Using a VBA


1. Launch Microsoft Office Excel, click the 'Developer' tab, and click 'Visual Basic.'
2. Click the 'Insert' menu and click 'Module' to insert a new code module. Type the following code to start a new sub procedure:Private Sub usingActiveCell()
3. Copy and paste the following to activate 'Sheet1:'Worksheets('Sheet1').Activate
4. Copy and paste the following to add a value to 'A1,' 'A2,' and 'A3:'Range('A1').SelectActiveCell.Value = 3.5Range('A2').SelectActiveCell.Value = 10Range('A3').SelectActiveCell.Value = 20
5. Copy and paste the following to highlight the cells with data:With ActiveCellRange(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count .CurrentRegion.Column - 1)).Interior.ColorIndex = 8Range(Cells(.CurrentRegion.Row, .Column), Cells(.CurrentRegion.Rows.Count .CurrentRegion.Row - 1, .Column)).Interior.ColorIndex = 8End WithApplication.ScreenUpdating = True
6. Copy and paste the following to display the values added through the 'Immediate Window:'Range('A1').SelectDebug.Print ActiveCell.ValueRange('A2').SelectDebug.Print ActiveCell.ValueRange('A3').SelectDebug.Print ActiveCell.Value
7. Copy and paste the following to end the sub procedure:End Sub
8. Press 'Ctrl' and 'G' to display the 'Immediate Window' and press 'F5' to run the procedure.

Blogger news