mjcalderone3
New Member
- Joined
- Jul 2, 2012
- Messages
- 2
First time poster, long-time user. I have a VBA that I have been able to peice together to closely fit my needs. I have a file that has numerous sheets (30), I want select specified sheets to append each other on the "Stats Upload" (Master) sheet. The selected sheets have identical format. Currenly my macro requires me to delete the "Stat Upload" sheet rather than just replacing/overriding the data on the "Stats Upload" sheet when when I have any change on the selected sheets. This "Stats Upload" tab is linked to other things so this part of the macro is causing me a problem; I want the macro to replace the data on the "Stats Upload' tab to not #REF my formulas which it is linked.
Also, the macro is currently pulling the heading into the "Stats Upload" file which I do not want. I only want the data. Which part of the macro can I discard to not pull the header into "Stats Upload" file. "400030 SL Stats Upload 2013" appears to be the tab that the macro is using to pull the header.
I have specified the sheet names within the macro that I want appended. The macro will be reused in numerous files once I get it tied-down; only the sheet names will have to be changed.
The macro that I am working with is below. Your assistance is greatly appreciated.
Sub StatsUpload()
Dim wrk As Workbook 'Workbook object - Always good to work with object variables
Dim sht As Worksheet 'Object for handling worksheets in loop
Dim trg As Worksheet 'Stats Upload Worksheet
Dim rng As Range 'Range object
Dim colCount As Integer 'Column count in tables in the worksheets
Set wrk = ActiveWorkbook 'Working in active workbook
For Each sht In wrk.Worksheets
If sht.Name = "Stats Upload" Then
MsgBox "There is a worksheet called as 'Stats Upload'." & vbCrLf & _
"Please remove or rename this worksheet since 'Stats Upload' would be" & _
"the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"
Exit Sub
End If
Next sht
'We don't want screen updating
Application.ScreenUpdating = False
'Add new worksheet as the last worksheet
Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
'Rename the new worksheet
trg.Name = "Stats Upload"
'Get column headers from the first worksheet
'Column count first
Set sht = wrk.Worksheets("400030 SL Stats Upload 2013")
colCount = sht.Cells(1, 255).End(xlToLeft).Column
'Now retrieve headers, no copy&paste needed
With trg.Cells(1, 1).Resize(1, colCount)
.Value = sht.Cells(1, 1).Resize(1, colCount).Value
'Set font as bold
.Font.Bold = True
End With
'
'
'
Dim i As Long
Dim ShtNames
ShtNames = Array("400030 SL Stats Upload 2013", "400070 SL Stats Upload 2013", "400080 SL Stats Upload 2013", "400165 SL Stats Upload 2013", "400170 SL Stats Upload 2013", "400180 SL Stats Upload 2013", _
"400235 SL Stats Upload 2013", "400240 SL Stats Upload 2013", "400430 SL Stats Upload 2013", "400490 SL Stats Upload 2013", "400550 SL Stats Upload 2013", _
"400570 SL Stats Upload 2013", "400600 SL Stats Upload 2013", "400605 SL Stats Upload 2013", "400610 SL Stats Upload 2013", "400630 SL Stats Upload 2013", "400770 SL Stats Upload 2013", _
"400810 SL Stats Upload 2013")
'We can start loop
For i = LBound(ShtNames) To UBound(ShtNames)
Set sht = Worksheets(ShtNames(i))
'
'
'
'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))
'Put data into the Stats Upload worksheet
trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
Next i
'Fit the columns in Stats Upload worksheet
trg.Columns.AutoFit
'Screen updating should be activated
Application.ScreenUpdating = True
End Sub
Also, the macro is currently pulling the heading into the "Stats Upload" file which I do not want. I only want the data. Which part of the macro can I discard to not pull the header into "Stats Upload" file. "400030 SL Stats Upload 2013" appears to be the tab that the macro is using to pull the header.
I have specified the sheet names within the macro that I want appended. The macro will be reused in numerous files once I get it tied-down; only the sheet names will have to be changed.
The macro that I am working with is below. Your assistance is greatly appreciated.
Sub StatsUpload()
Dim wrk As Workbook 'Workbook object - Always good to work with object variables
Dim sht As Worksheet 'Object for handling worksheets in loop
Dim trg As Worksheet 'Stats Upload Worksheet
Dim rng As Range 'Range object
Dim colCount As Integer 'Column count in tables in the worksheets
Set wrk = ActiveWorkbook 'Working in active workbook
For Each sht In wrk.Worksheets
If sht.Name = "Stats Upload" Then
MsgBox "There is a worksheet called as 'Stats Upload'." & vbCrLf & _
"Please remove or rename this worksheet since 'Stats Upload' would be" & _
"the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"
Exit Sub
End If
Next sht
'We don't want screen updating
Application.ScreenUpdating = False
'Add new worksheet as the last worksheet
Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
'Rename the new worksheet
trg.Name = "Stats Upload"
'Get column headers from the first worksheet
'Column count first
Set sht = wrk.Worksheets("400030 SL Stats Upload 2013")
colCount = sht.Cells(1, 255).End(xlToLeft).Column
'Now retrieve headers, no copy&paste needed
With trg.Cells(1, 1).Resize(1, colCount)
.Value = sht.Cells(1, 1).Resize(1, colCount).Value
'Set font as bold
.Font.Bold = True
End With
'
'
'
Dim i As Long
Dim ShtNames
ShtNames = Array("400030 SL Stats Upload 2013", "400070 SL Stats Upload 2013", "400080 SL Stats Upload 2013", "400165 SL Stats Upload 2013", "400170 SL Stats Upload 2013", "400180 SL Stats Upload 2013", _
"400235 SL Stats Upload 2013", "400240 SL Stats Upload 2013", "400430 SL Stats Upload 2013", "400490 SL Stats Upload 2013", "400550 SL Stats Upload 2013", _
"400570 SL Stats Upload 2013", "400600 SL Stats Upload 2013", "400605 SL Stats Upload 2013", "400610 SL Stats Upload 2013", "400630 SL Stats Upload 2013", "400770 SL Stats Upload 2013", _
"400810 SL Stats Upload 2013")
'We can start loop
For i = LBound(ShtNames) To UBound(ShtNames)
Set sht = Worksheets(ShtNames(i))
'
'
'
'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))
'Put data into the Stats Upload worksheet
trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
Next i
'Fit the columns in Stats Upload worksheet
trg.Columns.AutoFit
'Screen updating should be activated
Application.ScreenUpdating = True
End Sub