Convert cell text and filling to VBA macro - excel

I was wondering of there is a way to convert cell values and formatting to a macro.
In this example I only need text and the filling of cells.
Im looking for a way to analyse a range of cells to then convert to a marco, some sort of marco recorder but instead of recording it, it should analyse the cells and covert it to a macro.
If I run the recorder im getting this output:
Sub Macro3()
Range("E1,F2,G3,H4,I5,J6,F1:J1,E2:E7,F3:F4,G2:I2").Select
Range("G2").Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("S1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("T1").Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "Text here"
Range("E8,F5:F9,G4:G8,E9:E13,H3:M3,J2:N2,K1:P1,H5:H10").Select
Range("H5").Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("S2").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("T2").Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "Text here"
Range("T3").Select
End Sub
Im am looking to create the exact same code but not by recording the macro but by pulling the data from an exiting sheet.
Anyone an idea how to do this?
Kind regards
Raphael

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

Extract Data from Multiple Workbooks

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

Selecting and Formatting Borders in VBA

I'm trying to add thick borders on the left and right side every five columns, going all the way to the last row of the table (there are no blanks in any rows).
My code only adds the borders on the first and second rows.
I use following line twice:
Range(Selection, Selection.End(xlDown)).Select
Here's what a portion of the spreadsheet looks like. Note that the first row is a merged cell, and in the second row are table headers.
Application.DisplayAlerts = False
Dim lastCol As Integer
lastCol = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column
For i = 2 To lastCol Step 5
Range(Cells(1, i), Cells(1, i + 4)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
'Add thick borders
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select 'here's where I'm struggling
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Next i
Application.DisplayAlerts = True
The picture appears to be a list object (structured table in Excel). If so, you can work directly with the table object in VBA and avoid a ton of code writing and logic building.
Dim t as ListObject
Set t = ws.ListObjects("myTable")
Dim i as Long
For i = 1 to t.ListColumns.Count Step 5
With t.ListColumns(i).Range.Resize(t.ListRows.Count,5).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
'same for right border
Next

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

Resources