I am new to VBA (started 3 days ago) trying to build a macro. I hope to get help with my code, as well as to understand what is happening with the code in those parts in which I made a mistake.
The purpose of the code is to collect the values from the cells in the last column of each sheet and compile them in the bank column on the first sheet (which I will create when I open the worksheet for the first time).
My code is very raw and possibly contains a lot of errors. This, for most parts, is copied and pasted from sources (even from a macro recorder). I managed to get it to work, but I hope to condense it. Code that works:
Sub Test()
Dim LastCol As Long
Dim rng As Range
' Creating a bank sheet
Sheets.Add
' Returning to Page 1
Sheets("Page 1").Activate
' Use all cells on the sheet "Page 1"
Set rng = Sheets("Page 1").Cells
' Find the last column in "Page 1" and COPY
LastCol = Last(2, rng)
rng(2, LastCol).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
' Paste Selection in Sheet1
Sheets("Sheet1").Activate
Sheets("Sheet1").Paste
' Reset cursor to next blank space
Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Select
' Repeat for Page 2
Sheets("Page 2").Activate
Set rng = Sheets("Page 2").Cells
LastCol = Last(2, rng)
rng(2, LastCol).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet1").Activate
Sheets("Sheet1").Paste
' Reset cursor to next blank space
Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Select
' Repeat for Page 3
Sheets("Page 3").Activate
Set rng = Sheets("Page 3").Cells
LastCol = Last(2, rng)
rng(2, LastCol).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet1").Activate
Sheets("Sheet1").Paste
' Selecting range to sort
Set rng = ActiveSheet.Cells
LastCell = Last(3, rng)
With rng.Parent
.Select
.Range("A1", LastCell).Select
End With
' Sorting
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A177"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A2:A176")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
. , , -. , :
For N = 2 To ThisWorkbook.Worksheets.Count
' Use all cells on active sheet
ActiveWorkbook.Worksheets(N).Select
Set rng = ActiveWorkbook.Cells
' Find the last column in active sheet and COPY
LastCol = Last(2, rng)
rng(2, LastCol).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
' Paste Selection in Sheet1
Sheets("Sheet1").Activate
Sheets("Sheet1").Paste
' Reset cursor to next blank space
Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Select
Next N
, .
, ?
, , ( ):
Function Last(choice As Long, rng As Range)
'Ron de Bruin, 5 May 2008
' 1 = last row
' 2 = last column
' 3 = last cell
Dim lrw As Long
Dim lcol As Long
Select Case choice
Case 1:
On Error Resume Next
Last = rng.Find(What:="*", _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
Case 2:
On Error Resume Next
Last = rng.Find(What:="*", _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
Case 3:
On Error Resume Next
lrw = rng.Find(What:="*", _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
On Error Resume Next
lcol = rng.Find(What:="*", _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
On Error Resume Next
Last = rng.Parent.Cells(lrw, lcol).Address(False, False)
If Err.Number > 0 Then
Last = rng.Cells(1).Address(False, False)
Err.Clear
End If
On Error GoTo 0
End Select
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