» Copy cells from the ActiveCell Row to a Database sheet using VBA in Microsoft Excel
VBA macro tip contributed by Ron de Bruin, Microsoft MVP - Excel
CATEGORY - Cells, Ranges, Rows, and Columns in VBA
VERSION - All Microsoft Excel Versions
- The example codes will copy to a database sheet with the name Sheet2.
- Every time you run one of the subs the cells will be placed below the last row with data or after the last Column with data in sheet2.
- For each example there is a macro that does a normal copy and one that is only Copy the Values.
- The Example subs use the functions below (the macros won’t work without the functions).
Sub CopyCells()
Dim sourceRange As Range
Dim destrange As Range
Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1
Set sourceRange = Sheets("Sheet1").Cells( _
ActiveCell.Row, 1).Range("A1:D1")
Set destrange = Sheets("Sheet2").Range("A" & Lr)
sourceRange.Copy destrange
End Sub
Sub CopyCellsValues()
Dim sourceRange As Range
Dim destrange As Range
Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1
Set sourceRange = Sheets("Sheet1").Cells( _
ActiveCell.Row, 1).Range("A1:D1")
With sourceRange
Set destrange = Sheets("Sheet2").Range("A" _
& Lr).Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function Lastcol(sh As Worksheet)
On Error Resume Next
Lastcol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function
Book Store:
Recommended Books:
- Wall Street Journal Guide to Understanding Money and Investing
- Special Edition Using Microsoft Outlook 2002
- Finance and Accounting for Nonfinancial Managers
- What the IRS Doesn't Want You to Know: A Cpa Reveals the Tricks of the Trade
- Advanced modelling in finance using Excel and VBA
- Special Edition Using Microsoft Access 2002
No comments have been submitted.

