Macro not working as expected in excel 2010 - vba

I have taken over an excel doc with several macros. I am not well versed in how this works but have been able to update and fix it until now. The following script is to do a file SaveAs using text in the excel document as the file name. When I run the macro, I get Run-time error '1004': The item with the specified name wasn't found. I'm stuck on what it is looking for and not finding. Any feedback is appreciated.
Sub save_file()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("QUOTE").Select
ActiveSheet.Shapes("Option Button 1").Select
Selection.Delete
ActiveSheet.Shapes("Option Button 6").Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("Picture 3")).Select
Selection.Delete
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Rows("8:11").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False,
Transpose:=False
Range("D13").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False,
Transpose:=False
Application.CutCopyMode = False
Range("E3").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False,
Transpose:=False
Application.CutCopyMode = False
Sheets(Array("Main", "S", "H", "AP", "SE", "DC", "UI", "LX", "TS", "SS", "ALL NOTES",
"Customer List", "SHIPPING", "SING", "COSTS", "BOM's")).Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
If Range("c17").Characters.Count < 25 Then
ActiveWorkbook.SaveAs Application.GetSaveAsFilename(Range("E3") & " (" & Range("C17") & ")",
"Excel files (*.xlsm), *.xlsm"), Password:=""
Else
ActiveWorkbook.SaveAs Application.GetSaveAsFilename(Range("E3"), "Excel files (*.xlsm),
*.xlsm"), Password:=""
End If
End Sub

Related

Macro works when stepping through, but when running, it seems to skip steps

I am using the below macro:
'Copy active agency ID and paste into search on Worker Details
ActiveCell.Select
Selection.Copy
Range("L5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Call macro to run the agency details search
Call AgencyDetails
Basically, it uses the active cell, pastes it in the search field, and runs a macro that pulls data based on that criteria.
When stepping through, it copies and pastes the active cell and then the search works fine.
When running the macro, it seems to not copy and paste the active cell to the search field. Or that the called macro runs too early...
I have tried adding pauses and doevents etc, but I assumed doevents was for odbc connections.
To further complicate things. I have another macro that is almost identical, where it copies text into a search field and then returns data based on that criteria:
'Copy active worker ID and paste into search on Worker Details
ActiveCell.Select
Selection.Copy
Sheets("Worker Details").Select
Range("E5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Call macro to run the worker details search
Call WorkerDetails
And this works fine.
Any ideas? Probably incredibly simple, as my vba is not brilliant.
Thanks,
Sub AgencyDetails()
Dim BlankCheckAgency As Range
Set BlankCheckAgency = Range("AgencyDetails[[#Headers],[Agency ID]]")
Dim BlankCheckWorkers As Range
Set BlankCheckWorkers = Range("AgencyWorkers[[#Headers],[auto_number]]")
Application.ScreenUpdating = False
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVisible
'Clear Data
Range("G9,L9,G12,I12,G15,I15,G18,L18,Q9,Q12,Q15").Select
Selection.ClearContents
Range("G28").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("I28").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("K28").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("L5").Select
'Refresh Data
ActiveWorkbook.Connections("AgencyDetails").Refresh
ActiveWorkbook.Connections("AgencyBDM").Refresh
ActiveWorkbook.Connections("AgencyAM").Refresh
ActiveWorkbook.Connections("AgencySalesRep").Refresh
ActiveWorkbook.Connections("AgencyWorkers").Refresh
'DataCheck for agency details
Sheets("Agency Search Data").Select
BlankCheckAgency.Select
ActiveCell.Offset(1).Select
If IsEmpty(ActiveCell) = False Then
GoTo Data
Else
GoTo NoData
End If
NoData:
'Go back to search window and display message
Sheets("Agency Search").Select
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVeryHidden
Application.ScreenUpdating = True
msgBox "No agency on record matched the ID you have searched for." & vbNewLine & vbNewLine & "If you think this is wrong, please contact OSD"
GoTo Finish
Data:
'Agency Name
Range("AgencyDetails[Agency Name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("G9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Unmerge Address
Range("L9").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
'Full Address
Sheets("Agency Search Data").Select
Range("AgencyDetails[Full Address]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("L9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Merge Address
Range("L9:O15").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
'Agency Status
Sheets("Agency Search Data").Select
Range("AgencyDetails[Agency Status 2]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("G15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Agency Brand
Sheets("Agency Search Data").Select
Range("AgencyDetails[Brand]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("I15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Agency Reg
Sheets("Agency Search Data").Select
Range("AgencyDetails[Agency Reg]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("G12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'VAT Reg
Sheets("Agency Search Data").Select
Range("AgencyDetails[Vat Reg]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("I12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Unmerge GNotes
Range("G18").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
'General Notes
Sheets("Agency Search Data").Select
Range("AgencyDetails[General Notes]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("G18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Merge GNotes
Range("G18:J24").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
'Unmerge SNotes
Range("L18").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
'Sales Notes
Sheets("Agency Search Data").Select
Range("AgencyDetails[Sales Notes]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("L18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Merge SNotes
Range("L18:O24").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
'BDM
Sheets("Agency Search Data").Select
Range("AgencyBDM[Full Name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("Q9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Sales Rep
Sheets("Agency Search Data").Select
Range("AgencySalesRep[Full Name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("Q12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'AM
Sheets("Agency Search Data").Select
Range("AgencyAM[Full Name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("Q15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'DataCheck for workers
Sheets("Agency Search Data").Select
BlankCheckWorkers.Select
ActiveCell.Offset(1).Select
If IsEmpty(ActiveCell) = False Then
GoTo Data2
Else
GoTo NoData2
End If
NoData2:
Rows("1:1000").Select
Selection.RowHeight = 15
Sheets("Agency Search").Select
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVeryHidden
Application.ScreenUpdating = True
msgBox "The agency details have been pulled but there are no workers associated with the Agency" & vbNewLine & vbNewLine & "If you think this to not be true, please contact OSD"
GoTo Finish
Data2:
'Pull worker IDs
Sheets("Agency Search Data").Select
Range("AgencyWorkers[auto_number]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("G28").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Pull worker first name
Sheets("Agency Search Data").Select
Range("AgencyWorkers[first_name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("I28").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Pull worker last name
Sheets("Agency Search Data").Select
Range("AgencyWorkers[last_name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("K28").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Rows("1:1000").Select
Selection.RowHeight = 15
Range("L5").Select
Finish:
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVeryHidden
Application.ScreenUpdating = True
End Sub
It's a advisable not to use Select and Activate methods where possible, pass the value from selected cell to your desired cell like so:
Range("L5").Value = ActiveCell.Value
'Call macro to run the agency details search
Call AgencyDetails
As Vityata mentioned, it is better to fully qualify your ranges such as:
Sheet1.Range("L5").Value or even Sheets("Sheet1").Range("L5").Value, this way your code will not assume ActiveSheet and will take values from the defined ranges.
UPDATE
Sub AgencyDetails()
Dim BlankCheckAgency As Range
Set BlankCheckAgency = Sheets("Agency Search Data").Range("AgencyDetails[[#Headers],[Agency ID]]")
Dim BlankCheckWorkers As Range
Set BlankCheckWorkers = Range("AgencyWorkers[[#Headers],[auto_number]]")
'
Application.ScreenUpdating = False
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVisible
'Clear Data
ActiveSheet.Range("G9,L9,G12,I12,G15,I15,G18,L18,Q9,Q12,Q15").ClearContents
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "G").End(xlUp).Row
Range("G28:G" & LastRow).ClearContents
Range("I28:I" & LastRow).ClearContents
Range("K28:K" & LastRow).ClearContents
'Range("L5").Select
'Refresh Data
ActiveWorkbook.Connections("AgencyDetails").Refresh
ActiveWorkbook.Connections("AgencyBDM").Refresh
ActiveWorkbook.Connections("AgencyAM").Refresh
ActiveWorkbook.Connections("AgencySalesRep").Refresh
ActiveWorkbook.Connections("AgencyWorkers").Refresh
'DataCheck for agency details
If IsEmpty(BlankCheckAgency.Offset(1)) = False Then
GoTo Data
Else
GoTo NoData
End If
NoData:
'Go back to search window and display message
Sheets("Agency Search").Select
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVeryHidden
Application.ScreenUpdating = True
MsgBox "No agency on record matched the ID you have searched for." & vbNewLine & vbNewLine & "If you think this is wrong, please contact OSD"
GoTo Finish
Data:
'Agency Name
Range("AgencyDetails[Agency Name]").Copy
Sheets("Agency Search").Range("G9").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
The problem with the both codes is that you are not referring to the correct worksheet, but you are assuming the activesheet.
Make sure that you refer it and avoid working with ActiveCell:
Sub TestMe()
With Worksheets("SomeDetails")
.Range("A1").Copy
.Range("L5").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End With
Call AgencyDetails
End Sub
How to avoid using Select in Excel VBA
Whenever you are copying and pasting in Excel, it is a good practice to use Application.CutCopyMode = False.
It makes sure that the pasted range gets unselected, it is the same as pressing Esc.
One consideration might be the Application.Calculation mode - if this is xlCalculationManual or xlCalculationSemiautomatic then Excel may not register that $L$5 has been updated when it calls the macro.
You can force recalculation on everything (with Application.Calculate), just the ActiveSheet (ActiveSheet.Calculate) or the specific range (Range("L5").Calculate or Cells(5,12).Calculate)
In big/complicated Macros, setting the Calculation Mode to manual and explicitly deciding when to calculate can save a lot of time, in the same way that setting Application.ScreenUpdating = False to does. Just remember to reset them both afterwards! (Very long-running macros probably also need DoEvents somewhere to let Windows know that Excel has not crashed!)

Macro copies from one workbook into another, Runs with no error but sometimes doesn't work

I am slowly learning VBA in Excel on my own so I'm sure this code can be picked a part. Basically users fill this area with info and click a button that in the background copies the data they populate, opens a new workbook and pastes it in the next open row. There are many users, and for some it works, for others it runs with no error but their info is not pasted in the new. location. Most of the stuff at the end is just reformatting, but I didn't want to take it out in case it could be a part of the problem.
Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long
On Error Resume Next
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function
Sub FF_Temp_Upload()
'
' FF_Temp_Upload Macro
'
Application.ScreenUpdating = False
Dim Workbk As Workbook
Set Workbk = ThisWorkbook
Dim LR As Long
Dim Cell As Long
Dim Ret As String
LR = Range("B" & Rows.Count).End(xlUp).Row
Ret = IsWorkBookOpen("Location of the 2nd workbook/OVS Upload Template.xlsx")
If Ret = True Then
MsgBox "Template is currently being updated elsewhere. Please try again."
Exit Sub
Else
Workbooks.Open FileName:= _
"Location of the 2nd workbook/OVS Upload Template.xlsx"
End If
Workbk.Activate
Range("A2:C" & LR).Select
Selection.Copy
Windows("OVS Upload Template.xlsx").Activate
If Range("A2") = "" Then
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Else
Range("A1").Select
Selection.End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End If
Workbk.Activate
Range("H2:H" & LR).Select
Application.CutCopyMode = False
Selection.Copy
Windows("OVS Upload Template.xlsx").Activate
If Range("L2") = "" Then
Range("L2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Else
Range("L2").Select
Selection.End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End If
ActiveSheet.Range("$A$1:$M$100000").RemoveDuplicates Columns:=1, Header:=xlYes
LR = Range("B" & Rows.Count).End(xlUp).Row
Range("B2:B" & LR) = "=text(left(A2,8),""00000000"")"
Range("B2:B" & LR).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("C2:C" & LR) = "=""DCG""&MID(A2,9,4)"
Range("C2:C" & LR).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("D2:D" & LR).Select
Selection.Formula = "DT"
Range("I2:I" & LR).Select
Selection.Formula = "730"
Range("M2:M" & LR).Select
Selection.Formula = "MAJOH73"
ActiveWorkbook.Save
ActiveWindow.Close
Workbk.Activate
MsgBox "Articles Uploaded"
End Sub
You do not refer to Worksheets anywhere in your code. Thus, for some users, it works and for some it does not.
For those who works - their Excel file was saved with the correct Worksheet selected.
For those who does not work - their Excel file was saved with wrong Worksheet selected. Thus, when it is opened, the ActiveSheet is the wrong one and the code works there.
To fix it (quick and dirty) rewrite your code, refering the worksheet like this:
Worksheets("MyWorksheet").Range("$A$1:$M$100000").RemoveDuplicates Columns:=1
Then try to avoid Selection and ActiveSheet - How to avoid using Select in Excel VBA. At the end, each range or cell should be with refered Worksheet. Like this:
With Worksheets("MyName")
.Range("D2:D" & LR).Formula = "DT"
.Range("I2:I" & LR).Formula = "730"
.Range("M2:M" & LR).Formula = "MAJOH73"
End With

Skip cell - Excel VBA Macro

I am working on a VBA Macro for a budget sheet. When it copies data to cell B30 I want it to look at the cell and if there is data in the cell move to B31 and so on. However, when it gets to cell B41 I want it to throw up and error message. How would I go about writing this code?
Sheets("Budget with Revenue 2017-Test").Select
Range("B18:I18").Select
Application.CutCopyMode = False
Selection.Copy
Range("B22").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("Table2940[[Sum of Revenue]:[Sum of Savings]]").Select
Application.CutCopyMode = False
Selection.Copy
Range("B30").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("B21:I22").Select
Range("I21").Activate
Application.CutCopyMode = False
Selection.ClearContents
Range("B14:H14").Select
Range("H14").Activate
Selection.ClearContents
Range("J2:J3").Select
Selection.ClearContents
Range("G2").Select
Selection.ClearContents

Flattening data from oddly shaped "tables"

I have a spreadsheet with hundreds of table broken out by WBS, with odd formatting.
Beginning Format
What I want it to look like
I found a solution in which the starting tables are better organized into a summary table with headers at top:
How to "flatten" or "collapse" a 2D Excel table into 1D?
The macro I used works for two tables but uses absolute references to copy and transpose the data. It's very rough, but I've included below to show that I attempted.
The column (HRS, P, etc) and row (AL, Con, IH, etc) headings don't appear to change, so I assume I need something that will find a WBS and grab this information. Another issue is that some of the tables have extra Column headings before the Travel row (see second table in screenshot).
How do I go about writing something that will search for a WBS and record the flattened data, without referencing the specific cells?
Let me know if my question is poorly worded or if more information is needed.
Code from first macro:
Attribute VB_Name = "Module2"
Sub flatten_data()
Attribute flatten_data.VB_ProcData.VB_Invoke_Func = " \n14"
'
' flatten_data Macro
'
'
Range("B1").Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("A1:A42"), Type:=xlFillDefault
Range("A1:A42").Select
ActiveSheet.Previous.Select
Range("F3:K3").Select
Selection.Copy
ActiveSheet.Next.Select
ActiveWindow.SmallScroll Down:=-45
Range("B1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
Selection.Copy
Range("B7").Select
ActiveSheet.Paste
Range("B13").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=6
Range("B19").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=9
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("B19:B42"), Type:=xlFillDefault
Range("B19:B42").Select
ActiveSheet.Previous.Select
Range("C6").Select
Selection.Copy
ActiveSheet.Next.Select
Range("C16").Select
ActiveWindow.SmallScroll Down:=-54
Range("C1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("C1:C6"), Type:=xlFillDefault
Range("C1:C6").Select
Selection.Copy
ActiveSheet.Previous.Select
Range("C7").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("C7:C12").Select
ActiveSheet.Paste
ActiveSheet.Previous.Select
Range("C8").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("C13:C18").Select
ActiveSheet.Paste
ActiveSheet.Previous.Select
Range("C9").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("C19:C24").Select
ActiveSheet.Paste
ActiveSheet.Previous.Select
Range("C10").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("C25:C30").Select
ActiveSheet.Paste
ActiveSheet.Previous.Select
Range("C11").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
ActiveWindow.SmallScroll Down:=12
Range("C31:C36").Select
ActiveSheet.Paste
ActiveSheet.Previous.Select
Range("C12").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("C37:C42").Select
ActiveSheet.Paste
ActiveSheet.Previous.Select
Range("F6:K6").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
ActiveWindow.SmallScroll Down:=-33
Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("D7").Select
ActiveSheet.Previous.Select
Range("F7:K7").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveSheet.Previous.Select
Range("F8:K8").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D13").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveSheet.Previous.Select
Range("F9:K9").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D19").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveSheet.Previous.Select
Range("F10:K10").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D25").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveWindow.SmallScroll Down:=18
ActiveSheet.Previous.Select
Range("F11:K11").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D31").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveSheet.Previous.Select
Range("F12:K12").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D37").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveSheet.Previous.Select
Range("B16").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("A43:A84").Select
ActiveSheet.Paste
Range("B1:B42").Select
Range("B42").Activate
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=24
Range("B43").Select
ActiveSheet.Paste
Range("C1:C42").Select
Range("C42").Activate
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=27
Range("C43").Select
ActiveSheet.Paste
ActiveSheet.Previous.Select
Range("F21:K21").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D43").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveSheet.Previous.Select
Range("F22:K22").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D49").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveSheet.Previous.Select
Range("F23:K23").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D55").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveSheet.Previous.Select
Range("F24:K24").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
ActiveWindow.SmallScroll Down:=12
Range("D61").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveSheet.Previous.Select
Range("F25:K25").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D67").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveWindow.SmallScroll Down:=21
ActiveSheet.Previous.Select
Range("F26:K26").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D73").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveSheet.Previous.Select
Range("F29:K29").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("D79").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End Sub
I'm assuming that the tables are all of the same size and relative offset to the WBS keyword. I'm also assuming that the row "Travel" isn't needed in the final output, and the subtotals will be recalculated if needed.
Option Explicit
Sub Flatten_Data()
Dim wb As Workbook
Dim ws As Worksheet
Dim GCell As Range
Dim TableCell As Range
Dim TotalTables As Integer
Dim TableNumber As Integer
Dim TableRow As Integer
Dim TableColumn As Integer
Dim ColumnHeader(6) As String
Dim RowHeader(7) As String
ColumnHeader(1) = "HRS"
ColumnHeader(2) = "P"
ColumnHeader(3) = "OH"
ColumnHeader(4) = "G"
ColumnHeader(5) = "C"
ColumnHeader(6) = "F"
RowHeader(1) = "AL"
RowHeader(2) = "Con"
RowHeader(3) = "IH"
RowHeader(4) = "Mat"
RowHeader(5) = "OD"
RowHeader(6) = "SUB"
RowHeader(7) = "Trav"
Set wb = Workbooks("Book1") ' or whatever sheet holds the source data
Set ws = Worksheets("Sheet1") ' or whatever sheet you want to copy the flattened data to
With wb
With ws
Set GCell = .Range("A:A")
TotalTables = Application.WorksheetFunction.CountIf(GCell, "WBS")
Set GCell = .Cells.Find("WBS", .Cells(1048576, 1)) ' looks for "WBS" and ensures that it finds one in A1 if it exists
For TableNumber = 1 To TotalTables
For TableRow = 1 To 7
For TableColumn = 1 To 6
Worksheets("Sheet2").Cells(TableColumn + (TableRow - 1) * 6 + (TableNumber - 1) * 42, 4) = GCell.Offset(4 + TableRow, 4 + TableColumn).Value
Worksheets("Sheet2").Cells(TableColumn + (TableRow - 1) * 6 + (TableNumber - 1) * 42, 3) = RowHeader(TableRow)
Worksheets("Sheet2").Cells(TableColumn + (TableRow - 1) * 6 + (TableNumber - 1) * 42, 2) = ColumnHeader(TableColumn)
Worksheets("Sheet2").Cells(TableColumn + (TableRow - 1) * 6 + (TableNumber - 1) * 42, 1) = "1." & TableNumber
Next TableColumn
Next TableRow
Set GCell = .Cells.FindNext(GCell)
Next TableNumber
End With
End With
End Sub
I'll leave it to you to ensure that the table numbers are correct.
And I'd avoid 'Select' like the plague for this sort of thing, it'll only slow down the code.

Mass edit multiple workbooks of the same format

I'm very new to VBA (as of this morning), so excuse my ignorance. I have a few hundred Excel workbooks, all formatted exactly the same way (just with different text). I'm trying to both format and delete a few sheets within the workbooks (the same for alL).
I recorded a macro that works fine when applied individually, but I'm getting a runtime error when I try to run this as a means of mass format:
Sub LoopFiles()
Dim MyFileName, MyPath As String
Dim MyBook As Workbook
MyPath = "I:\Academic Networks\All scorecard copies, 6.18.2015"
MyFileName = Dir(MyPath & "*.xlsm")
Do Until MyFileName = ""
Workbooks.Open MyPath & MyFileName
Set MyBook = ActiveWorkbook
Application.Run "Workbook1.xlsm!ScorecardMacro"
MyBook.Save
MyBook.Close
MyFileName = Dir
Loop
End Sub
I keep getting a runtime error (9) - Subscript out of range. Any thoughts?
Here's the formatting/deleting I'm trying to apply to all my workbooks (which works fine when applied to one workbook at a time:
Sub ScorecardMacro()
'
' Scorecard Macro
'
'
Sheets.Add
Sheets("Scorecard").Select
Range("D3:D36").Select
Selection.Copy
Sheets("Sheet1").Select
Range("B1").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
Sheets("Scorecard").Select
Range("A3:A36").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
Sheets("Scorecard").Select
Range("F3:I36").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets("Checklist").Select
Range("D4:D27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
ActiveWindow.ScrollColumn = 28
Range("AJ1").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
Sheets("Checklist").Select
Range("A4:A27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("AJ2").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
Sheets("Additional Information").Select
Range("A4:B14").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("BH1").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets("Program Recommendations").Select
Range("A4:D21").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("BS1").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
ActiveWindow.ScrollColumn = 1
Range("A2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
ActiveCell.FormulaR1C1 = _
"=MID(CELL(""filename""),SEARCH(""["",CELL(""filename""))+1,SEARCH(""]"",CELL(""filename""))-SEARCH(""["",CELL(""filename""))-1)"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A6"), Type:=xlFillDefault
Range("A2:A6").Select
Sheets("Program Recommendations").Select
ActiveWindow.SelectedSheets.Delete
Sheets("Additional Information").Select
ActiveWindow.SelectedSheets.Delete
Sheets("Scorecard").Select
ActiveWindow.SelectedSheets.Delete
Sheets("Checklist").Select
ActiveWindow.SelectedSheets.Delete
End Sub
The error shows you are trying to access something that doesn't exist.
Since you are deleting something, its better to do all you updates first, then do all the deletes in the end.
If you do some deletes in between and then update, there might be some values/sheets missing
You are referring to a named range called "filename":
"=MID(CELL(""filename""),SEARCH(""["",CELL(""filename""))+1,SEARCH(""]"",CELL(""filename""))-SEARCH(""["",CELL(""filename""))-1)"
I suspect that name is not defined in the other workbooks.