Arrange (Cut and Insert Cut Cells) Rows using VBA Code
I'm a beginner and have very less knowledge on VBA.
There are 2 worksheets in the attachment - 'Sheet 1 and Sheet 2'.
Once I run the Code on 'Sheet 1' I would like to see Data as on 'Sheet 2'.
Basically, I thought to write a lot of stuff here and ended up writing a lot and had everything deleted.
My thanks in advance to all the experts.. I using Excel 2007
Thank you...
Attachment | Size |
---|---|
VBA Question.xls | 19 KB |
See data as on another sheet
Hi,
Here's some solution. In the standard code module put the following code:
' ************************* ' ************************* '
Public oBaseWsh As Worksheet
Sub RefreshData()
Dim rngBase As Range
Dim rngActive As Range
Dim i As Long
Dim N As Long
Application.ScreenUpdating = False
On Error GoTo ERROR_HANDLER
If oBaseWsh Is ActiveSheet Then GoTo EXIT_SUB
ActiveSheet.Cells.Clear
ActiveSheet.Columns.UseStandardWidth = True
Set rngBase = oBaseWsh.UsedRange
rngBase.Copy
ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll
Set rngActive = ActiveSheet.UsedRange
N = rngBase.Columns.Count
For i = 1 To N
rngActive.Columns(i).ColumnWidth = rngBase.Columns(i).ColumnWidth
Next i
rngActive.Cells(1, 1).Select
EXIT_SUB:
Application.ScreenUpdating = True
Exit Sub
ERROR_HANDLER:
' Some code for error handling
Err.Clear
GoTo EXIT_SUB
End Sub
' ************************* ' ************************* '
Then in the ThisWorkbook code module put this:
' ************************* ' ************************* '
Private Sub Workbook_Open()
' Change Sheet2 if you want to copy from another sheet
Set oBaseWsh = Sheet2
End Sub
' ************************* ' ************************* '
To be more automate in the Sheet1 (or any other) code module put this:
' ************************* ' ************************* '
Private Sub Worksheet_Activate()
Call RefreshData
End Sub
' ************************* ' ************************* '
Thus any time you activate the sheet, data will be refreshed and you no need to run macro manually.
Save and reopen your workbook.
Best regards