Extract Data from Multiple Workbooks - excel

I want to extract data in red boxes under column name (highlighted in green) under tab name (colored in yellow) from multiple workbooks using wildcard and end name (highlighted in blue). my code stopped after creating a workbook. (code below) and formatting it. please help on how can i get all data until last data
Private Sub CommandButton3_Click()
Dim JCN As String, Path As String
UserForm1.Hide
JCN = TextBox3.Value
ReportName = JCN & " PANEL NESTING REPORT"
UserName = Environ$("Username")
Path = "C:\Users\" & UserName & "\Desktop\"
Workbooks.Add
ActiveSheet.Name = "NESTING REPORT"
'Column Header
Range("B1").Value = "WBS Code"
Range("C1").Value = "Airline Code"
Range("D1").Value = "JCN"
Range("E1").Value = "'3000LVL"
Range("F1").Value = "MBOM"
Range("G1").Value = "Make Part"
Range("H1").Value = "LAV"
Range("I1").Value = "LC Number"
Range("J1").Value = "Rev"
Range("K1").Value = "Size"
Range("L1").Value = "Part Number"
Range("M1").Value = "Rev"
Range("N1").Value = "Qty"
Range("O1").Value = "Classification"
Range("P1").Value = "Type"
Range("Q1").Value = "Thickness"
Range("R1").Value = "Rawmat"
Range("S1").Value = "Remarks"
Range("T1").Value = "'.400 Code"
Range("U1").Value = "W.O."
'Format Column Header
Range("B1:U1").Select
Selection.Font.Bold = True
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = -0.499984740745262
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
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("B:U").EntireColumn.AutoFit
ActiveWindow.DisplayGridlines = False
'Extract Data
'Save Workbook
ActiveWorkbook.SaveAs Filename:=Path & ReportName & ".xlsx"
'Close Workbook
ActiveWorkbook.Close
End Sub

Related

What is the vba code to register employees' daily meals in a "list"?

Good night,
I have a screen made in excel for the canteen employees to register the employees who go to lunch daily.
Sub test()
Range("N5:Q5").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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 = xlMedium
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Range("N5:Q5").Select
ActiveCell.FormulaR1C1 = "Request"
Range("N6:Q21").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Range("N21:Q21").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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 = xlMedium
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("N21").Select
ActiveCell.FormulaR1C1 = "TOTAL:"
Range("Q21").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-14]C:R[-1]C)"
Range("Q7:Q21").Select
Range("Q21").Activate
Selection.Style = "Currency"
Range("N7").Select
ActiveCell.FormulaR1C1 = "Daily menu"
Range("Q7").Select
ActiveCell.FormulaR1C1 = "$4 "
Range("N8").Select
ActiveCell.FormulaR1C1 = "Extra drink"
Range("Q8").Select
ActiveCell.FormulaR1C1 = "$1 "
Range("N9").Select
ActiveCell.FormulaR1C1 = "Extra dessert"
Range("Q9").Select
ActiveCell.FormulaR1C1 = "$1.20 "
Range("Q10").Select
ActiveCell.FormulaR1C1 = "Code employee:"
Range("F6").Select
ActiveCell.FormulaR1C1 = "Name employee:"
Range("G5:J5").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("G6:J6").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Columns("F:F").EntireColumn.AutoFit
Range("P4").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Range("P4").Select
Selection.NumberFormat = "dd/mm/yyyy hh:mm:ss"
End Sub
This screen registers the menu and on the right side appears the summary with the price and at the end of the column the total. However, I would like to record this information in an additional sheet "List" where the employee's code, his name, the time he had lunch, what he had for lunch and the price of the meal, but I don't know which code to use, because as the employee registers, in the list it should appear on the following line. As you register on the home page, you will save in the "list".
Try this: You can save each column by changing CELLNAME to the cell that you want to save.
Sub SaveLunch()
Dim LunchRow As Long
'Determine New or Existing Invoice
If Sheet1.Range("XX999").Value = Empty Then 'Check if first record
LunchRow = Sheet2.Range("A99999").End(xlUp).Row + 1 'First Available Row
Else: 'Add row
LunchRow = Sheet1.Range("XX999").Value 'Existing Lunch Row
End If
Sheet2.Range("A" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 1
Sheet2.Range("C" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 2
Sheet2.Range("D" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 3
Sheet2.Range("E" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 4
Sheet2.Range("F" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 5
Sheet2.Range("G" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 6
End If
Sheet1.Range("XX999").Value = Sheet1.Range("XX999").Value + 1
End Sub

What's the vba code to change the color of the first line in multiple excel sheets from 6 onwards?

I need to format (create a title, select the vendor code according to the table...) a set of cells at the beginning of each sheet from sheet 6. I already have the VBA code to insert the lines above the table, now I just needed the code to format the cells from sheet 6 at the same time. Can anyone help me?
My code for inserting lines is as follows:
Sub insert_rows()
Application.DisplayAlerts = False
Dim i As Integer, a As Integer
a = 6
For i = Sheets.Count To 6 Step -1
If Sheets.Count = 6 Then
Exit Sub
End If
a = a + 1
Sheets(i).Range("1:1").Insert
Sheets(i).Range("2:2").Insert
Sheets(i).Range("3:3").Insert
Sheets(i).Range("4:4").Insert
Sheets(i).Range("5:5").Insert
Sheets(i).Range("6:6").Insert
Sheets(i).Range("7:7").Insert
Sheets(i).Range("8:8").Insert
Application.DisplayAlerts = True
Next
End Sub
The formatting I want is the one below. I need the code to apply this formatting from sheet 6 onwards, because the first sheets are support sheets for the tables and, therefore, do not need this "header".
Sub Format()
'
' Format Macro
Range("B1:J2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("B1:J2").Select
With Selection.Font
.Color = -10066432
.TintAndShade = 0
End With
Range("B1:J2").Select
ActiveCell.FormulaR1C1 = "TITLE"
Range("B4").Select
ActiveCell.FormulaR1C1 = "Name:"
Range("B5").Select
ActiveCell.FormulaR1C1 = "Code:"
Range("B6").Select
ActiveCell.FormulaR1C1 = "Date:"
Range("C4").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("C5").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("C6").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveWindow.DisplayGridlines = False
End Sub
Thank you!
How to Get Rid of Select/Activate When Possible
Option Explicit
Sub CreateHeadersAfterSheet5()
Const ProcName As String = "CreateHeadersAfterSheet5"
On Error GoTo ClearError
Const FirstWorksheetIndex As Long = 6
Application.ScreenUpdating = False
With ThisWorkbook
Dim LastWorksheetIndex As Long: LastWorksheetIndex = .Worksheets.Count
If LastWorksheetIndex < FirstWorksheetIndex Then Exit Sub
Dim ash As Object: Set ash = .ActiveSheet
Dim n As Long
For n = FirstWorksheetIndex To LastWorksheetIndex
CreateHeaders .Worksheets(n)
Next n
ash.Select
End With
Application.ScreenUpdating = True
MsgBox "Headers created."
ProcExit:
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume ProcExit
End Sub
Sub CreateHeaders(ByVal ws As Worksheet)
Const ProcName As String = "CreateHeaders"
On Error GoTo ClearError
With ws
.Select ' cannot be avoided only because of the following line
ActiveWindow.DisplayGridlines = False
.Range("1:8").Insert
With .Range("B1:J2")
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
'.MergeCells = True
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlNone
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
With .Font
.Color = -10066432
.TintAndShade = 0
End With
End With
.Range("B1").Value = "TITLE"
.Range("B4").Value = "Name:"
.Range("B5").Value = "Code:"
.Range("B6").Value = "Date:"
With .Range("C4")
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlNone
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
With .Range("C5")
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
With .Range("C6")
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
End With
ProcExit:
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume ProcExit
End Sub

EXCEL VBA macro for putting a style to a range and then ucase

i'm newbie, sorry in advance for myy long question
So, i have two macros (one recorded and pasted in personal macro) and other i found in google
The first one, with my selection fills the color to orange and adds bolds borders
The second one with the selection, upercases all the range.
However, when i run this two macro together with another sub (calling the subs) the text does not shows up, i need to change of cell then select again and run the macro again in order to function.
Sub text ()
Dim rng As Range
Dim sAddr As String
Set rng = Selection
Selection.Merge
ActiveCell.FormulaR1C1 = _
"=""additional due for "" & TEXT(TODAY(),""MMMM "") & ""end of month"""
sAddr = rng.Address
rng = Evaluate("index(upper(" & sAddr & "),)")
Selection.NumberFormat = "General"
End Sub
Then the filling up sub (which is a little long)
Sub ORANGE()
Selection.Font.Bold = True
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
'CAMBIO 2
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Selection.NumberFormat = "General"
End Sub
The way i use both macros is simply calling first ORANGE and then TEXT, beacuse the other way does not works, when i try them in VBA run macro option it works fine.
When i use the button in the ribbon i need to change of cell, select it again and it will work. i use this macro a lot but it simply makes me repeat it each time.
Does anybody knows who to perform both task at once without the result being an empty orange cell?
Thanks!
Try this. Read comments inside the code:
Public Sub AddTextAndFormat()
Dim selectedRange As Range
Set selectedRange = Selection
' Merges the selection
selectedRange.Merge
' Adds the formula to the first selection's cell
selectedRange.Formula = "=""additional due for "" & TEXT(TODAY(),""MMMM "") & ""end of month"""
' Uppercase that first cell
selectedRange.Cells(1, 1).Value = UCase$(selectedRange.Cells(1, 1).Value)
' Apply formats
With selectedRange
.Font.Bold = True
' Borders:
.BorderAround LineStyle:=xlContinuous, ColorIndex:=0, Weight:=xlMedium
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
' Other format:
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
'CAMBIO 2
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
'.MergeCells = False -> This line unmerges the first cells merge
.NumberFormat = "General"
End With
With selectedRange.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub

Excel corrupted vba macro "We found a problem with some content in "

My excel is being corrupted.
When I open excel the following message appears:
"We found a problem with some content in "". Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes"
And when I click "yes" opens excel with the "input4" tab misconfigured
I think it may be something related to the macros I added below.
I can't figure out where the problem is, can someone help me please?
Sub gera_quadro_inp4()
Dim n As Integer
Dim u As Integer
Dim LastR As Integer
Dim Cart As String
With Workbooks("TAB AUTO.xlsm").Sheets("input4").Range("B7:B100").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="mar/2008,jun/2008,set/2008,dez/2008,mar/2009,jun/2009,set/2009,dez/2009,mar/2010,jun/2010,set/2010,dez/2010,mar/2011,jun/2011,set/2011,dez/2011,mar/2012,jun/2012,set/2012,dez/2012,mar/2013,jun/2013,set/2013,dez/2013,mar/2014,jun/2014,set/2014,dez/2014,mar/2015,jun/2015,set/2015,dez/2015,mar/2016,jun/2016,set/2016,dez/2016,mar/2017,jun/2017,set/2017,dez/2017,mar/2018,jun/2018,set/2018,dez/2018,mar/2019,jun/2019,set/2019,dez/2019,mar/2020,jun/2020,set/2020,dez/2020,mar/2021,jun/2021,set/2021,dez/2021,mar/2022,jun/2022,set/2022,dez/2022,mar/2023,jun/2023,set/2023,dez/2023,mar/2024,jun/2024,set/2024," & _
"dez/2024,mar/2025,jun/2025,set/2025,dez/2025,mar/2026,jun/2026,set/2026,dez/2026,mar/2027,jun/2027,set/2027,dez/2027,mar/2028,jun/2028,set/2028,dez/2028,mar/2029,jun/2029,set/2029,dez/2029,mar/2030,jun/2030,set/2030,dez/2030,mar/2031,jun/2031,set/2031,dez/2031,mar/2032,jun/2032,set/2032,dez/2032,mar/2033,jun/2033,set/2033,dez/2033,mar/2034,jun/2034,set/2034,dez/2034,mar/2035,jun/2035,set/2035,dez/2035,mar/2036,jun/2036,set/2036,dez/2036,mar/2037,jun/2037,set/2037,dez/2037,mar/2038,jun/2038,set/2038,dez/2038,mar/2039,jun/2039,set/2039,dez/2039,mar/2040,jun/2040,set/2040,dez/2040"
End With
LastR = Workbooks("TAB AUTO.xlsm").Sheets("input1").Range("C8").End(xlDown).row
'Armazena carteiras em Cart
For Each Value In Workbooks("TAB AUTO.xlsm").Sheets("input1").Range("B8:B" & LastR)
Cart = Cart & "," & Value
Next Value
'Cria lista de opção de carteiras
With Workbooks("TAB AUTO.xlsm").Sheets("input4").Range("C7:C100").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Cart
End With
'Cria variáveis do quadro
cen = Workbooks("TAB AUTO.xlsm").Sheets("input3").Range("F6").Value
Workbooks("TAB AUTO.xlsm").Sheets("input4").Range(Workbooks("TAB AUTO.xlsm").Sheets("input4").Cells(6, 2), Workbooks("TAB AUTO.xlsm").Sheets("input4").Cells(6, 100)).Value = ""
Workbooks("TAB AUTO.xlsm").Sheets("input4").Range("B6").Value = "PERIODO_TRI_AJUSTE"
Workbooks("TAB AUTO.xlsm").Sheets("input4").Range("C6").Value = "CARTEIRA"
Workbooks("TAB AUTO.xlsm").Sheets("input4").Range("D6").Value = "VALOR_ANCL_ANT"
For u = 1 To cen
Workbooks("TAB AUTO.xlsm").Sheets("input4").Cells(6, 4 + u).Value = "OVERRIDE_TRI_C" & u
Workbooks("TAB AUTO.xlsm").Sheets("input4").Cells(6, 4 + cen + u).Value = "OVERRIDE_ANO_C" & u
Next u
Workbooks("TAB AUTO.xlsm").Sheets("input4").Range("B6").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
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
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Selection.Font.Bold = True
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
End Sub
'Gera input4
Sub gera_input4()
Dim nework As Workbook
Workbooks("TAB AUTO.xlsm").Sheets("input4").Range("B6").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Set nework = Workbooks.Add
nework.Sheets(1).Range("A1").PasteSpecial xlPasteValues
nework.Sheets(1).Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "mmm-yy"
Cells.Select
Cells.EntireColumn.AutoFit
nework.Sheets(1).Range("A1").Select
End Sub

Excel: Combine reformatting and mail macros

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+q
'
Rows("1:6").Select
Selection.Delete Shift:=xlUp
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0.499984740745262
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Cells.Select
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
Cells.EntireColumn.AutoFit
Rows("1:1").Select
Selection.Font.Bold = True
Selection.AutoFilter
End Sub
2nd one
Option Explicit
Private Sub CommandButton1_Click()
sendmail
End Sub
Public Function sendmail()
On Error GoTo ende
Dim esubject As String, sendto As String, ccto As String, ebody As String, newfilename As String
Dim apps As Object, itm As Object
esubject = "Systematic and Manually Created ASN"
sendto = "ooooooo#hp.com"
ccto = "iiiiiiii#hp.com"
ebody = "Hello All" & vbCrLf & _
"Please find the Systematically and Manually created ASN for the last month" & _
vbCrLf & "With Regards" & vbCrLf & "Tarak"
newfilename = "C:\Stuff.XLS"
Set apps = CreateObject("Outlook.Application")
Set itm = apps.createitem(0)
With itm
.Subject = esubject
.To = sendto
.cc = ccto
.body = ebody
.attachments.Add (newfilename)
.display
.Send
End With
Set apps = Nothing
Set itm = Nothing
ende:
End Function
Perhaps something like this
Option Explicit
Private Sub CommandButton1_Click()
Rows("1:6").Select
Selection.Delete Shift:=xlUp
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0.499984740745262
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Cells.Select
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
Cells.EntireColumn.AutoFit
Rows("1:1").Select
Selection.Font.Bold = True
Selection.AutoFilter
sendmail
End Sub
Public Function sendmail()
On Error GoTo ende
Dim esubject As String, sendto As String, ccto As String, ebody As String, newfilename As String
Dim apps As Object, itm As Object
esubject = "Systematic and Manually Created ASN"
sendto = "ooooooo#hp.com"
ccto = "iiiiiiii#hp.com"
ebody = "Hello All" & vbCrLf & _
"Please find the Systematically and Manually created ASN for the last month" & _
vbCrLf & "With Regards" & vbCrLf & "Tarak"
newfilename = "C:\Stuff.XLS"
Set apps = CreateObject("Outlook.Application")
Set itm = apps.createitem(0)
With itm
.Subject = esubject
.To = sendto
.cc = ccto
.body = ebody
.attachments.Add (newfilename)
.display
.Send
End With
Set apps = Nothing
Set itm = Nothing
ende:
End Function

Resources