Blockhead15
New Member
- Joined
- Jun 13, 2022
- Messages
- 17
- Office Version
- 365
- Platform
- Windows
I have my macro set up on my sample data file but it's not clean at all. I tried to implement a looping structure over the worksheets to simplify but could not get it to work. So long as the input file meets a certain condition, I add some sheets and populate. I know there's a simpler way instead of all the clicks back and forth but frustration is hampering the thought process. Any suggestions on simplest way to clean this up?
VBA Code:
Sub GetData()
'
' GetData Macro
'
'
Application.ScreenUpdating = False 'To avoid screen flickering
Windows("TEST_XYZ.xlsx").Activate
Sheets("Run Info").Copy After:=Workbooks("TestSample.xlsm").Sheets(Workbooks("TestSample.xlsm").Sheets.Count)
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Data"
Windows("TEST_XYZ.xlsx").Activate
Range("A1:B17").Select
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("A1").Select
ActiveSheet.Paste
Range("C7").Select
Windows("TEST_XYZ.xlsx").Activate
Range("D12:D17").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("B12").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("A19").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Windows("TEST_XYZ.xlsx").Activate
Range("A19").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("B20").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("A26").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("C20").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("A33").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("D20").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("A42").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("E20").Select
ActiveSheet.Paste
Range("H18").Select
' Worksheet 1
Windows("TEST_XYZ.xlsx").Activate
Sheets("X99X_GroupB").Select
Range("D20").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("B22").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("D31").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("C22").Select
ActiveSheet.Paste
Windows("TestSample.xlsm").Activate
Application.CutCopyMode = False
Range("E22").Select
Windows("TEST_XYZ.xlsx").Activate
Range("D40").Select
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("D22").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("D45").Select
Application.CutCopyMode = False
Selection.Copy
Range("D45").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("X99X_GroupB").Select
Windows("TestSample.xlsm").Activate
Range("E22").Select
ActiveSheet.Paste
' Worksheet 2
Windows("TEST_XYZ.xlsx").Activate
Sheets("X99X_GroupC").Select
Range("D20").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("B23").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("D31").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("C23").Select
ActiveSheet.Paste
Windows("TestSample.xlsm").Activate
Application.CutCopyMode = False
Range("E23").Select
Windows("TEST_XYZ.xlsx").Activate
Range("D40").Select
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("D23").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("D45").Select
Application.CutCopyMode = False
Selection.Copy
Range("D45").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("X99X_GroupC").Select
Windows("TestSample.xlsm").Activate
Range("E23").Select
ActiveSheet.Paste
' Worksheet 3
Windows("TEST_XYZ.xlsx").Activate
Sheets("X99X_GroupA").Select
Range("D20").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("B21").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("D31").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("C21").Select
ActiveSheet.Paste
Windows("TestSample.xlsm").Activate
Application.CutCopyMode = False
Range("E22").Select
Windows("TEST_XYZ.xlsx").Activate
Range("D40").Select
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("D21").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("D45").Select
Application.CutCopyMode = False
Selection.Copy
Range("D45").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("X99X_GroupA").Select
Windows("TestSample.xlsm").Activate
Range("E21").Select
ActiveSheet.Paste
' Worksheet 3
Windows("TEST_XYZ.xlsx").Activate
Sheets("X99X_GroupD").Select
Range("D20").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("B24").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("D31").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("C24").Select
ActiveSheet.Paste
Windows("TestSample.xlsm").Activate
Application.CutCopyMode = False
Range("E22").Select
Windows("TEST_XYZ.xlsx").Activate
Range("D40").Select
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("D24").Select
ActiveSheet.Paste
Windows("TEST_XYZ.xlsx").Activate
Range("D45").Select
Application.CutCopyMode = False
Selection.Copy
Range("D45").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("X99X_GroupD").Select
Windows("TestSample.xlsm").Activate
Range("E24").Select
ActiveSheet.Paste
'SheetNames
Windows("TEST_XYZ.xlsx").Activate
Sheets(Array("X99X_GroupA", "X99X_GroupB", "X99X_GroupC", "X99X_GroupD")).Select
Range("H1").Select
ActiveCell.Formula2R1C1 = _
"=MID(CELL(""filename"",R[-8]C[-5]),FIND(""]"",CELL(""filename"",R[-8]C[-5]))+1,255)"
Range("H2").Select
Sheets("Run Info").Activate
Sheets("X99X_GroupA").Activate
Range("H1").Select
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("A21").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("TEST_XYZ.xlsx").Activate
Sheets("X99X_GroupB").Select
Range("H1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("A22").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("TEST_XYZ.xlsx").Activate
Sheets("X99X_GroupC").Select
Range("H1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("A23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("TEST_XYZ.xlsx").Activate
Sheets("X99X_GroupD").Select
Range("H1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("TestSample.xlsm").Activate
Range("A24").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D11").Select
Columns.AutoFit
Application.ScreenUpdating = True
End Sub