VBA removing heading in spreadsheet

JadeRogers

New Member
Joined
Jul 17, 2024
Messages
2
Office Version
  1. 365
Hi, needing some assistance on a code i currently have.
When i leave my code as follows it works as wanted but removes header rows. when changing to "A2:A" it hides all rows.

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
With ws.Range("A1:A" & lastRow)
.AutoFilter Field:=1, Criteria1:="0"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).Value = ""
.AutoFilter
End With

Im assuming it may be placed incorrectly :| Full code as follows:

' SAPMonday Macro
'

'
Cells.Select
Selection.ColumnWidth = 8.43
Columns("A:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("U:U").Select
Selection.Copy
Columns("A:A").Select
ActiveSheet.Paste
Range("B2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=VALUE(RC[-1])"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2", Range("B2").End(xlDown))
Range("B2", Range("B2").End(xlDown)).Select
Selection.Copy
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
With ws.Range("A2:A" & lastRow)
.AutoFilter Field:=1, Criteria1:="0"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).Value = ""
.AutoFilter
End With
Columns("B:B").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You might try something like this.
VBA Code:
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim FilterRange As Range, DataRange As Range, ClearRange As Range
    
    Set ws = ThisWorkbook.Sheets("Sheet1")
    ws.AutoFilterMode = False
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Set FilterRange = ws.Range("A1:A" & LastRow) 'includes header row
    With FilterRange
        Set DataRange = .Offset(1).Resize(.Rows.Count - 1) 'excludes header row
        .AutoFilter Field:=1, Criteria1:="0"
        On Error Resume Next
        Set ClearRange = DataRange.SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not ClearRange Is Nothing Then
            ClearRange.Value = ""
        End If
    End With
    ws.AutoFilterMode = False

(Tip: For future posts , please try to use code tags like I did above when posting code. It makes your code easier to read and copy.
)
 
Upvote 0
You might try something like this.
VBA Code:
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim FilterRange As Range, DataRange As Range, ClearRange As Range
  
    Set ws = ThisWorkbook.Sheets("Sheet1")
    ws.AutoFilterMode = False
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Set FilterRange = ws.Range("A1:A" & LastRow) 'includes header row
    With FilterRange
        Set DataRange = .Offset(1).Resize(.Rows.Count - 1) 'excludes header row
        .AutoFilter Field:=1, Criteria1:="0"
        On Error Resume Next
        Set ClearRange = DataRange.SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not ClearRange Is Nothing Then
            ClearRange.Value = ""
        End If
    End With
    ws.AutoFilterMode = False

Unfortunately Run-time error '1004' occurs here:
VBA Code:
        Set DataRange = .Offset(1).Resize(.Rows.Count - 1) 'excludes header row
 
Upvote 0
Unfortunately Run-time error '1004' occurs here:

tmpabc.xlsm
ABCDEFGH
1Data Col 1Data Col 2Data Col 3Data Col 4Data Col 5Data Col 6Data Col 7Data Col 8
2
3
4
5
6
7
8
9
10
Sheet1

If your sheet1 has no data, that is the error you will get. Please use XL2BB to post some example data so I can see how you have organized it.

 
Upvote 0

Forum statistics

Threads
1,224,742
Messages
6,180,685
Members
452,993
Latest member
FDARYABEE

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top