Hey all! So I have some VBA that formats some data that I enter. But it formats it in the current tab. Then it messes up the tab and I cannot re-run the vba (linked to a button) without closing the spreadsheet, not saving it, then re-opening. I would like the data I put in to immediately be pasted into a new excel doc, then formatted with the following VBA:
Sub PushTheButton()
Filtername
Tidy
ActiveRangeBorders
InsertRows
Mergetoptwo
ColorCellsWhite
ColorTopRowBlue
End Sub
Sub Filtername()
Dim lr As Long
Dim ws As Worksheet
Set ws = Sheets("WeeklyWS")
lr = ws.Cells(Rows.Count, 9).End(xlUp).Row
For i = 2 To lr
If InStr(ws.Cells(i, 9).Value, "|") > 0 Then
ws.Cells(i, 9).Value = Left(ws.Cells(i, 9).Value, InStr(ws.Cells(i, 9).Value, "|") - 1)
End If
Next i
End Sub
Sub Tidy()
Dim lastcol As Integer
Dim c As Range
Application.ScreenUpdating = False
'Step 1: Clear first name
On Error Resume Next
For Each c In Range("E1:E100")
c.Value = Left(c.Value, InStr(1, c.Value, ",") - 1)
Next
On Error GoTo 0
'Step 2: insert new column at the end
Application.ActiveSheet.UsedRange
lastcol = Selection.SpecialCells(xlCellTypeLastCell).Column + 1
Cells(1, lastcol).Value = "Comment"
Cells(1, lastcol).Font.Color = vbWhite
'Step 4: check column 'I' for specific string
For Each c In Range("I3:I100")
If InStr(1, LCase(c.Value), "HN") > 0 Then
With Cells(c.Row, lastcol)
.Value = "HN"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "BS") > 0 Then
With Cells(c.Row, lastcol)
.Value = "BS"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "RE") > 0 Then
With Cells(c.Row, lastcol)
.Value = "RE"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "PE") > 0 Then
With Cells(c.Row, lastcol)
.Value = "PE"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "MA") > 0 Then
With Cells(c.Row, lastcol)
.Value = "MA"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "CN") > 0 Then
With Cells(c.Row, lastcol)
.Value = "CN"
.Font.Bold = True
End With
Else
With Cells(c.Row, lastcol)
.Font.Bold = False
.ClearContents
End With
End If
Next c
ActiveSheet.Columns("H").Delete
ActiveSheet.Columns("D").Delete
ActiveSheet.Columns("C").Delete
Application.ScreenUpdating = True
End Sub
Sub ActiveRangeBorders()
' Puts borders around only populated cells in a range
Application.ScreenUpdating = False
Dim lngLstCol As Long, lngLstRow As Long
lngLstRow = ActiveSheet.UsedRange.Rows.Count
lngLstCol = ActiveSheet.UsedRange.Columns.Count
For Each rngCell In Range("A1:A" & lngLstRow)
If rngCell.Value > "" Then
r = rngCell.Row
c = rngCell.Column
Range(Cells(r, c), Cells(r, "I")).Select 'lngLstCol was where "H" is now. Skipped two columns to the right for some reason.
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
Next
Application.ScreenUpdating = True
End Sub
Sub InsertRows()
Application.ScreenUpdating = False
'Insert 2 Rows Above Row 1
Rows("1:2").Insert Shift:=xlDown, _
CopyOrigin:=xlFormatFromLeftOrAbove
Application.ScreenUpdating = True
End Sub
Sub Mergetoptwo()
Application.ScreenUpdating = False
Range("A1:H1,A2:H2").Select
Range("H2").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Application.ScreenUpdating = True
End Sub
Sub ColorCellsWhite()
Application.ScreenUpdating = False
Dim Data As Range
Dim cell As Range
Set currentsheet = ActiveWorkbook.Sheets("WEEKLY WS")
Set Data = currentsheet.Range("A1:Z300")
For Each cell In Data
cell.Interior.ColorIndex = 2
Next
Application.ScreenUpdating = True
End Sub
Sub ColorTopRowBlue()
Application.ScreenUpdating = False
Dim Data As Range
Dim cell As Range
Set currentsheet = ActiveWorkbook.Sheets("WEEKLY WS")
Set Data = currentsheet.Range("A3:I3")
For Each cell In Data
If cell.Value <> "" Then
cell.Interior.ColorIndex = 33
End If
Next
Application.ScreenUpdating = True
End Sub
How would I do this?
Sub PushTheButton()
Filtername
Tidy
ActiveRangeBorders
InsertRows
Mergetoptwo
ColorCellsWhite
ColorTopRowBlue
End Sub
Sub Filtername()
Dim lr As Long
Dim ws As Worksheet
Set ws = Sheets("WeeklyWS")
lr = ws.Cells(Rows.Count, 9).End(xlUp).Row
For i = 2 To lr
If InStr(ws.Cells(i, 9).Value, "|") > 0 Then
ws.Cells(i, 9).Value = Left(ws.Cells(i, 9).Value, InStr(ws.Cells(i, 9).Value, "|") - 1)
End If
Next i
End Sub
Sub Tidy()
Dim lastcol As Integer
Dim c As Range
Application.ScreenUpdating = False
'Step 1: Clear first name
On Error Resume Next
For Each c In Range("E1:E100")
c.Value = Left(c.Value, InStr(1, c.Value, ",") - 1)
Next
On Error GoTo 0
'Step 2: insert new column at the end
Application.ActiveSheet.UsedRange
lastcol = Selection.SpecialCells(xlCellTypeLastCell).Column + 1
Cells(1, lastcol).Value = "Comment"
Cells(1, lastcol).Font.Color = vbWhite
'Step 4: check column 'I' for specific string
For Each c In Range("I3:I100")
If InStr(1, LCase(c.Value), "HN") > 0 Then
With Cells(c.Row, lastcol)
.Value = "HN"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "BS") > 0 Then
With Cells(c.Row, lastcol)
.Value = "BS"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "RE") > 0 Then
With Cells(c.Row, lastcol)
.Value = "RE"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "PE") > 0 Then
With Cells(c.Row, lastcol)
.Value = "PE"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "MA") > 0 Then
With Cells(c.Row, lastcol)
.Value = "MA"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "CN") > 0 Then
With Cells(c.Row, lastcol)
.Value = "CN"
.Font.Bold = True
End With
Else
With Cells(c.Row, lastcol)
.Font.Bold = False
.ClearContents
End With
End If
Next c
ActiveSheet.Columns("H").Delete
ActiveSheet.Columns("D").Delete
ActiveSheet.Columns("C").Delete
Application.ScreenUpdating = True
End Sub
Sub ActiveRangeBorders()
' Puts borders around only populated cells in a range
Application.ScreenUpdating = False
Dim lngLstCol As Long, lngLstRow As Long
lngLstRow = ActiveSheet.UsedRange.Rows.Count
lngLstCol = ActiveSheet.UsedRange.Columns.Count
For Each rngCell In Range("A1:A" & lngLstRow)
If rngCell.Value > "" Then
r = rngCell.Row
c = rngCell.Column
Range(Cells(r, c), Cells(r, "I")).Select 'lngLstCol was where "H" is now. Skipped two columns to the right for some reason.
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
Next
Application.ScreenUpdating = True
End Sub
Sub InsertRows()
Application.ScreenUpdating = False
'Insert 2 Rows Above Row 1
Rows("1:2").Insert Shift:=xlDown, _
CopyOrigin:=xlFormatFromLeftOrAbove
Application.ScreenUpdating = True
End Sub
Sub Mergetoptwo()
Application.ScreenUpdating = False
Range("A1:H1,A2:H2").Select
Range("H2").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Application.ScreenUpdating = True
End Sub
Sub ColorCellsWhite()
Application.ScreenUpdating = False
Dim Data As Range
Dim cell As Range
Set currentsheet = ActiveWorkbook.Sheets("WEEKLY WS")
Set Data = currentsheet.Range("A1:Z300")
For Each cell In Data
cell.Interior.ColorIndex = 2
Next
Application.ScreenUpdating = True
End Sub
Sub ColorTopRowBlue()
Application.ScreenUpdating = False
Dim Data As Range
Dim cell As Range
Set currentsheet = ActiveWorkbook.Sheets("WEEKLY WS")
Set Data = currentsheet.Range("A3:I3")
For Each cell In Data
If cell.Value <> "" Then
cell.Interior.ColorIndex = 33
End If
Next
Application.ScreenUpdating = True
End Sub
How would I do this?