I'm working to automate as much as possible a rather simple process that starts after I download a report, never more than once a day. With a lot of learning by MUCH Googling, trial and many, many errors, I have a set of vba routines that run through about half the steps. Being ignorant and of bad memory (unable to recall what I did to use "Call" in an earlier project), what a joy, probably some unnecessary items, probably some redundant, but it's working LOL, I have them all in a routine:
Public Sub m_BiReport()
I open the .xlsm file.
I call the routine that asks for the file name to be opened.
It then copies out the single worksheet into the .xlsm file and closes the other workbook
The .xlsx worksheet has 5 merged header rows that include the created date/time.
I use the date to rename the worksheet
Public Sub BiNameSheet()
Range("A2").Select
Selection.NumberFormat = "m/d/yyyy"
Range("A1").Select
Selection.NumberFormat = "mm dd yy"
Range("A1").Select 'redundant?
ActiveCell.FormulaR1C1 = "=TEXT(R[1]C,""m dd yy"")"
ActiveSheet.Name = ActiveSheet.Range("A1").Value
End Sub
Then I delete the 5 header rows and the blank/unneeded columns.
I enter 3 columns on the left, enter column titles, and a formula that renders a "Yes" if the specified cell on the row has a date greater than or equal to 6 months earlier than today. Formula is copied down to the last row.
Copy and paste as values.
So far so good.
Now I want to name a range for reporting and I can't figure out how to name the filled range using the worksheet name and a prefix. Should I use code similar to what I used to rename the worksheet to create a string "11_9_16Ovr6" and use that to name the range before I delete the rows? And, uh, how would I do that?
Better ideas?
Batting zero, time to ask for "help"!
TIA
rON
Just in case anyone is interested or has more elegant code:
Public Sub m_OpenCopySheetClose()
Application.Calculate
'ThisWorkbook.Worksheets("Transition Input").Select
Dim GetFilenameFromPath As String
Dim i As Integer, wb As Workbook
Dim TransitionInput_FileNAME
Dim Path_TransitionInput
Dim Name_TransitionInput
Dim newFileName As String
TransitionInput_FileNAME = Application.GetOpenFilename
Set wbk1 = ActiveWorkbook
Set wbk2 = Workbooks.Open(TransitionInput_FileNAME)
wbk2.Close savechanges:=False
End Sub
__________________________________________________________________
Public Sub m_BiHeadings()
Columns("A:C").Select
Range("C1").Activate
Selection.Insert Shift:=xlToRight
Range("A1").Select
ActiveCell.FormulaR1C1 = "New to Report"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Dropped Off Report"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Over Six Months"
Range("D1").Select
Selection.Copy
Range("A1:C1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A:J").Select
Selection.WrapText = False
Selection.Columns.AutoFit
Selection.Rows.AutoFit
Range("A1").Select
End Sub
_________________________________________________
Public Sub m_bicleandata()
'bicleandata
Dim LastRow As Long
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Cells.Select
Selection.UnMerge
Rows("1:5").Select
Selection.Delete Shift:=xlUp
Range("D:D,F:F,K:K,G:G,H:H,I:I,J:J").Select
Selection.Delete Shift:=xlToLeft
Columns("I:I").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Clear
Application.Run "m_delem"
Cells.Select
Selection.WrapText = False
Selection.Columns.AutoFit
Selection.Rows.AutoFit
Range("A2:J" & LastRow).Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With ActiveWindow
.DisplayGridlines = True
.GridlineColorIndex = 15
.DisplayHeadings = True
End With
Range("E:H").Select
Selection.NumberFormat = "m/d/yy"
Range("A1").Select
End Sub
__________________________________________________________________________________
Public Sub m_delem()
'http://stackoverflow.com/questions/31092719/delete-empty-rows-using-vba-ms-excel?rq=1
Dim last As Long
Dim current As Long
Dim col As Long
Dim retain As Boolean
last = Cells(Rows.Count, "B").End(xlUp).Row
For current = last To 1 Step -1
retain = False
For col = 3 To 26
If Cells(current, col).Value <> vbNullString Then
retain = True
Exit For
End If
Next col
If Not retain Then Rows(current).Delete
Next current
End Sub
_______________________________________________________________
Public Sub m_BiOverDate()
LastRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row
Range("C2").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 1
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = _
"=IF(RC[5]<=DATE(YEAR(NOW()),MONTH(NOW())-6,DAY(NOW())),""Yes"","""")"
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C" & LastRow)
Range("A1").Select
End Sub
Public Sub m_BiReport()
'Module name: bireport
Application.Run "m_OpenCopySheetClose"
Application.Run "m_BiNameSheet"
Application.Run "m_BiCleanData"
Application.Run "m_BiHeadings"
Application.Run "m_BiOverDate"
Application.Run "m_BiNameSheet"
Application.Run "m_BiCleanData"
Application.Run "m_BiHeadings"
Application.Run "m_BiOverDate"
'Application.Run "m_
End SubI open the .xlsm file.
I call the routine that asks for the file name to be opened.
It then copies out the single worksheet into the .xlsm file and closes the other workbook
The .xlsx worksheet has 5 merged header rows that include the created date/time.
I use the date to rename the worksheet
Public Sub BiNameSheet()
Range("A2").Select
Selection.NumberFormat = "m/d/yyyy"
Range("A1").Select
Selection.NumberFormat = "mm dd yy"
Range("A1").Select 'redundant?
ActiveCell.FormulaR1C1 = "=TEXT(R[1]C,""m dd yy"")"
ActiveSheet.Name = ActiveSheet.Range("A1").Value
End Sub
Then I delete the 5 header rows and the blank/unneeded columns.
I enter 3 columns on the left, enter column titles, and a formula that renders a "Yes" if the specified cell on the row has a date greater than or equal to 6 months earlier than today. Formula is copied down to the last row.
Copy and paste as values.
So far so good.
Now I want to name a range for reporting and I can't figure out how to name the filled range using the worksheet name and a prefix. Should I use code similar to what I used to rename the worksheet to create a string "11_9_16Ovr6" and use that to name the range before I delete the rows? And, uh, how would I do that?
Better ideas?
Batting zero, time to ask for "help"!
TIA
rON
Just in case anyone is interested or has more elegant code:
Public Sub m_OpenCopySheetClose()
Application.Calculate
'ThisWorkbook.Worksheets("Transition Input").Select
Dim GetFilenameFromPath As String
Dim i As Integer, wb As Workbook
Dim TransitionInput_FileNAME
Dim Path_TransitionInput
Dim Name_TransitionInput
Dim newFileName As String
TransitionInput_FileNAME = Application.GetOpenFilename
Set wbk1 = ActiveWorkbook
Set wbk2 = Workbooks.Open(TransitionInput_FileNAME)
'wbk1.Sheets(1) is the Report sheet
wbk2.Sheets.Copy After:=wbk1.Sheets(1)wbk2.Close savechanges:=False
End Sub
__________________________________________________________________
Public Sub m_BiHeadings()
Columns("A:C").Select
Range("C1").Activate
Selection.Insert Shift:=xlToRight
Range("A1").Select
ActiveCell.FormulaR1C1 = "New to Report"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Dropped Off Report"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Over Six Months"
Range("D1").Select
Selection.Copy
Range("A1:C1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A:J").Select
Selection.WrapText = False
Selection.Columns.AutoFit
Selection.Rows.AutoFit
Range("A1").Select
End Sub
_________________________________________________
Public Sub m_bicleandata()
'bicleandata
Dim LastRow As Long
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Cells.Select
Selection.UnMerge
Rows("1:5").Select
Selection.Delete Shift:=xlUp
Range("D:D,F:F,K:K,G:G,H:H,I:I,J:J").Select
Selection.Delete Shift:=xlToLeft
Columns("I:I").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Clear
Application.Run "m_delem"
Cells.Select
Selection.WrapText = False
Selection.Columns.AutoFit
Selection.Rows.AutoFit
Range("A2:J" & LastRow).Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With ActiveWindow
.DisplayGridlines = True
.GridlineColorIndex = 15
.DisplayHeadings = True
End With
Range("E:H").Select
Selection.NumberFormat = "m/d/yy"
Range("A1").Select
End Sub
__________________________________________________________________________________
Public Sub m_delem()
'http://stackoverflow.com/questions/31092719/delete-empty-rows-using-vba-ms-excel?rq=1
Dim last As Long
Dim current As Long
Dim col As Long
Dim retain As Boolean
last = Cells(Rows.Count, "B").End(xlUp).Row
For current = last To 1 Step -1
retain = False
For col = 3 To 26
If Cells(current, col).Value <> vbNullString Then
retain = True
Exit For
End If
Next col
If Not retain Then Rows(current).Delete
Next current
End Sub
_______________________________________________________________
Public Sub m_BiOverDate()
'
' BiOverDate Macro
'
Dim LastRow As LongLastRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row
Range("C2").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 1
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = _
"=IF(RC[5]<=DATE(YEAR(NOW()),MONTH(NOW())-6,DAY(NOW())),""Yes"","""")"
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C" & LastRow)
Range("A1").Select
End Sub