judeinajuke
New Member
- Joined
- Oct 27, 2023
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
Hello all,
I'm relatively new to coding, and a lot of it kind of goes over my head so I apologize if I don't understand right away, and I also apologize if I don't make much sense trying to explain what my goal is.
I've set up some macros for my work, and they work relatively fine - however, in order for the macros to work, we have to rename the file we want to work with to what the macro is named.
We pull reports from our website and each report has the name with the date, and it will add a number at the end for each report pulled that day (1, 2, 3, so on so forth) - we pull these reports a couple times each day, for different tasks, so you may see why it can be a little confusing and frustrating to have to rename each file to the macro it's being used for.
One of my macros is titled "Rosters Macro". For this particular macro, it requires two workbooks - one we don't need to rename, as it's just a template file we utilize and never moves or changes, and the other workbook being the pulled report, "Course_Status_2023_10_27_1.csv", with the name changing each time a report is pulled. The macro utilizes the template and places it into the pulled report, and organizes the data how we'd like. However, I cannot find a way to make the macro work without renaming the report to "Rosters Macro" for each report I work on in the day. Code below.
In this code, I have the .csv file renamed to Rosters Macro. Again, the goal is to not have to change the name of the .csv file
This is also an issue for me with macros I created that only use the pulled report itself without any templates. If I don't rename them, I'll get "Run-time error: "9" Subscript out of range".
I have a few macros I've created that somehow do NOT require I change the name, which are perfect. Example here:
Any help in understanding is greatly appreciated!
I'm relatively new to coding, and a lot of it kind of goes over my head so I apologize if I don't understand right away, and I also apologize if I don't make much sense trying to explain what my goal is.
I've set up some macros for my work, and they work relatively fine - however, in order for the macros to work, we have to rename the file we want to work with to what the macro is named.
We pull reports from our website and each report has the name with the date, and it will add a number at the end for each report pulled that day (1, 2, 3, so on so forth) - we pull these reports a couple times each day, for different tasks, so you may see why it can be a little confusing and frustrating to have to rename each file to the macro it's being used for.
One of my macros is titled "Rosters Macro". For this particular macro, it requires two workbooks - one we don't need to rename, as it's just a template file we utilize and never moves or changes, and the other workbook being the pulled report, "Course_Status_2023_10_27_1.csv", with the name changing each time a report is pulled. The macro utilizes the template and places it into the pulled report, and organizes the data how we'd like. However, I cannot find a way to make the macro work without renaming the report to "Rosters Macro" for each report I work on in the day. Code below.
Sub RostersMacro() ' ' RostersMacro Macro ' ' Rows("1:3").Select Selection.Delete Shift:=xlUp Columns("D:T").Select Selection.Delete Shift:=xlToLeft Columns("I:J").Select Selection.Delete Shift:=xlToLeft Columns("J:V").Select Selection.Delete Shift:=xlToLeft ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollColumn = 2 ActiveWindow.ScrollColumn = 1 Columns("G:G").Select With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Range("A1").Select ActiveWindow.SmallScroll Down:=12 Range("A1:K40").Select ActiveWorkbook.Worksheets("Rosters Macro").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Rosters Macro").Sort.SortFields.Add2 Key:=Range( _ "A2:A29"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Rosters Macro").Sort .SetRange Range("A1:K29") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With ActiveWorkbook.Worksheets("Rosters Macro").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Rosters Macro").Sort.SortFields.Add2 Key:=Range( _ "A2:A30"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal ActiveWorkbook.Worksheets("Rosters Macro").Sort.SortFields.Add2 Key:=Range( _ "B2:B30"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Rosters Macro").Sort .SetRange Range("A1:K30") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With Columns("G:H").Select Range("H1").Activate Columns("G:H").EntireColumn.AutoFit Columns("A:E").Select Range("E1").Activate Columns("A:E").EntireColumn.AutoFit Rows("1:1").Select Selection.Delete Shift:=xlUp Windows("Template.xlsx").Activate ActiveWindow.SmallScroll Down:=-9 Rows("1:9").Select Selection.Copy Windows("Rosters Macro.csv").Activate Selection.Insert Shift:=xlDown Windows("Template.xlsx").Activate ActiveWindow.SmallScroll Down:=6 Rows("22:27").Select Application.CutCopyMode = False Selection.Copy Windows("Rosters Macro.csv").Activate ActiveWindow.SmallScroll Down:=33 Range("A49").Select ActiveSheet.Paste End Sub |
In this code, I have the .csv file renamed to Rosters Macro. Again, the goal is to not have to change the name of the .csv file
This is also an issue for me with macros I created that only use the pulled report itself without any templates. If I don't rename them, I'll get "Run-time error: "9" Subscript out of range".
I have a few macros I've created that somehow do NOT require I change the name, which are perfect. Example here:
Sub NewEnrollment() ' ' NewEnrollment Macro ' ' Rows("1:3").Select Selection.Delete Shift:=xlUp Columns("C:G").Select Selection.Delete Shift:=xlToLeft Columns("D:O").Select Selection.Delete Shift:=xlToLeft ActiveWindow.ScrollColumn = 2 ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollColumn = 4 ActiveWindow.ScrollColumn = 5 ActiveWindow.ScrollColumn = 6 ActiveWindow.ScrollColumn = 7 ActiveWindow.ScrollColumn = 6 Columns("I:J").Select Selection.Delete Shift:=xlToLeft Columns("J:O").Select Selection.Delete Shift:=xlToLeft Columns("K:O").Select Selection.Delete Shift:=xlToLeft ActiveWindow.ScrollColumn = 5 ActiveWindow.ScrollColumn = 4 ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollColumn = 2 ActiveWindow.ScrollColumn = 1 Columns("C:C").Select Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove Range("C1").Select ActiveCell.FormulaR1C1 = "=RC[-2]&"" ""&RC[-1]" Range("C1").Select Selection.AutoFill Destination:=Range("C1:C40"), Type:=xlFillDefault Range("C1:C40").Select ActiveWindow.SmallScroll Down:=-18 Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Columns("A:B").Select Range("B1").Activate Application.CutCopyMode = False Selection.Delete Shift:=xlToLeft Columns("A:A").Select Columns("A:A").EntireColumn.AutoFit End Sub |
Any help in understanding is greatly appreciated!