TryingHere
New Member
- Joined
- Mar 19, 2025
- Messages
- 9
- Office Version
- 365
- Platform
- Windows
Hi All,
I wrote some of this recorded some and pasted some that people had already written so its a bit sloppy. I'm trying to get better at VBA.
What I am trying to do is copy data from sheet 1 numbers paste to a new sheet in the same workbook so formula data is captured. Delete any columns that don't have * in row A and clear any data in rows below the last cell in column A with data. Hopefully I explained this ok. The current code I'm using below does the trick but takes about 1 minute 15 seconds to run which seems way to slow. Any thoughts for how to speed this up/ remove any unnecessary steps I'm taking?
Thank you.
Public Sub Manditory_Columns_Only()
ActiveSheet.Copy After:=Worksheets(Sheets.Count)
On Error Resume Next
ActiveSheet.Name = "1B Data"
Sheets("1B Data").Activate
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'delete columns
Dim c As Long
Sheets("1B Data").Activate
Application.ScreenUpdating = False
' Loop through columns backwards
For c = 105 To 1 Step -1
If Cells(1, c).Value <> "*" Then Cells(1, c).EntireColumn.Delete
Next c
Application.ScreenUpdating = True
'PricingRemoveRows()
Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long
Set WS = ThisWorkbook.ActiveSheet
With WS
Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
LastCellRowNumber = LastCell.Row
Rows(LastCellRowNumber + 1 & ":" & Rows.Count).Delete
End With
'AddButton
Dim MyButton As button
'Add a button and assign a macro
Set MyButton = ActiveSheet.Buttons.Add(6, 7.5, 109, 22.5)
With MyButton
.Name = "RunmyCode"
.OnAction = "Delete_Tab_1B_Data" 'macro
.Characters.Text = "Delete Sheet" 'button caption
End With
Range("A2").Select
End Sub
I wrote some of this recorded some and pasted some that people had already written so its a bit sloppy. I'm trying to get better at VBA.
What I am trying to do is copy data from sheet 1 numbers paste to a new sheet in the same workbook so formula data is captured. Delete any columns that don't have * in row A and clear any data in rows below the last cell in column A with data. Hopefully I explained this ok. The current code I'm using below does the trick but takes about 1 minute 15 seconds to run which seems way to slow. Any thoughts for how to speed this up/ remove any unnecessary steps I'm taking?
Thank you.
Public Sub Manditory_Columns_Only()
ActiveSheet.Copy After:=Worksheets(Sheets.Count)
On Error Resume Next
ActiveSheet.Name = "1B Data"
Sheets("1B Data").Activate
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'delete columns
Dim c As Long
Sheets("1B Data").Activate
Application.ScreenUpdating = False
' Loop through columns backwards
For c = 105 To 1 Step -1
If Cells(1, c).Value <> "*" Then Cells(1, c).EntireColumn.Delete
Next c
Application.ScreenUpdating = True
'PricingRemoveRows()
Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long
Set WS = ThisWorkbook.ActiveSheet
With WS
Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
LastCellRowNumber = LastCell.Row
Rows(LastCellRowNumber + 1 & ":" & Rows.Count).Delete
End With
'AddButton
Dim MyButton As button
'Add a button and assign a macro
Set MyButton = ActiveSheet.Buttons.Add(6, 7.5, 109, 22.5)
With MyButton
.Name = "RunmyCode"
.OnAction = "Delete_Tab_1B_Data" 'macro
.Characters.Text = "Delete Sheet" 'button caption
End With
Range("A2").Select
End Sub