-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcelsheetsmergerv1.1.frm
134 lines (122 loc) · 4.62 KB
/
xcelsheetsmergerv1.1.frm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} xcelmerger
Caption = "XcelSheetsMerger"
ClientHeight = 5412
ClientLeft = 108
ClientTop = 456
ClientWidth = 6972
OleObjectBlob = "xcelsheetsmergerv1.1.frx":0000
StartUpPosition = 1 'CenterOwner
End
Attribute VB_Name = "xcelmerger"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
offSetRowsBox.Enabled = True
offSetRowsBox.BackColor = RGB(255, 255, 255)
Else
offSetRowsBox.Enabled = False
offSetRowsBox.BackColor = RGB(232, 232, 232)
End If
End Sub
Private Sub Label6_Click()
ActiveWorkbook.FollowHyperlink _
Address:="https://github.com/erajtob/XcelSheetsMerger"
End Sub
Private Sub mergeButton_Click()
'ErajExcelMerger
Dim i As Integer
Dim xRows As Integer
Dim yCol As Integer
Dim offSetL As Integer
Dim sCount As Integer
Dim xTCount As Variant
Dim xWs As Worksheet
Dim cWs As Worksheet
Dim NewName As String
Dim Selected_Sheets As String
Dim listLoop As Integer
Dim Exclude() As String
Dim xClude As String
Dim Delim As String
Dim chckBox As Boolean
Dim offSetRows As Integer
On Error Resume Next
LInput:
xTCount = xTCountBox.Value
If TypeName(xTCount) = "Boolean" Then Exit Sub
If Not IsNumeric(xTCount) Then
MsgBox "Only can enter number", , "Merger for Excel"
GoTo LInput
End If
Application.ScreenUpdating = False
'Add extra Sheet to workbook for the Merged dump
Set cWs = ActiveWorkbook.Worksheets.Add(Sheets(1))
'Input for Combined Sheet
NewName = cWsBox.Value
cWs.Name = NewName
'Copy Title and Paste on A1 of Merged Sheet
Worksheets(sWsBox.Value).Range("A1").EntireRow.Copy Destination:=cWs.Range("A1")
For listLoop = 1 To Me.ListBox1.ListCount
If Me.ListBox1.Selected(listLoop - 1) Then
Selected_Sheets = Selected_Sheets & "," & Me.ListBox1.List(listLoop - 1)
End If
Next
Selected_Sheets = Mid(Selected_Sheets, 2)
Delim = ","
Exclude = Split(Selected_Sheets, ",")
xClude = Join(Exclude, Delim)
xClude = Delim & cWs.Name & Delim & xClude & Delim
sCount = Sheets.Count - (UBound(Exclude) - LBound(Exclude) + 2)
chckBox = Me.CheckBox1.Value
'offSetRow count
offSetRows = Me.offSetRowsBox.Value - 1
'Outer Loop to keep sheet count to determine 1st paste incase of offset
For offSetL = sCount To 0 Step -1
'Inner Loop to iterate through worksheets
For Each xWs In ThisWorkbook.Sheets
'InStr to exclude sheets from selected sheets to exclude
If InStr(1, xClude, Delim & xWs.Name & Delim, vbTextCompare) = 0 Then
'Offset requires first paste to be positive offset thus splitting with IF statement
If chckBox = True And offSetL = sCount Then
xWs.Range("A1").CurrentRegion.Offset(CInt(xTCount), 0).Copy
cWs.Cells(cWs.UsedRange.Cells(cWs.UsedRange.Count).Row + 1, 1).PasteSpecial Paste:=xlPasteValues
offSetL = offSetL - 1
ElseIf chckBox = True And offSetL < sCount Then
xWs.Range("A1").CurrentRegion.Offset(CInt(xTCount), 0).Copy
cWs.Cells(cWs.UsedRange.Cells(cWs.UsedRange.Count).Row - offSetRows, 1).PasteSpecial Paste:=xlPasteValues
offSetL = offSetL - 1
'No Offset Code Run
ElseIf chckBox = False Then
xWs.Range("A1").CurrentRegion.Offset(CInt(xTCount), 0).Copy
cWs.Cells(cWs.UsedRange.Cells(cWs.UsedRange.Count).Row + 1, 1).PasteSpecial Paste:=xlPasteValues
offSetL = offSetL - 1
End If
End If
Next xWs
Next
'Offset row for the last paste
If chckBox = True Then
xRows = cWs.UsedRange.Rows.Count
yRows = cWs.UsedRange.Columns.Count
cWs.Rows(xRows & ":" & xRows - offSetRows).EntireRow.Delete
End If
Application.ScreenUpdating = True
MsgBox "All Sheets Merged.", vbOKOnly, "Done"
End Sub
Private Sub UserForm_Initialize()
Dim J As Long
Dim K As Worksheet
xcelmerger.Caption = "XcelSheetsMerger " & versionNo.Caption
offSetRowsBox.BackColor = RGB(232, 232, 232)
Me.sWsBox.Clear
For J = 1 To Sheets.Count
Me.sWsBox.AddItem Sheets(J).Name
Next
For Each K In Worksheets
Me.ListBox1.AddItem K.Name
Next K
End Sub