Pasting text on Visible range error

rex759

Well-known Member
Joined
Nov 8, 2004
Messages
608
Office Version
  1. 365
Platform
  1. Windows
Hello,

The goal of the snippet below is to filter out the rows that do not meet the criteria and add the acronym of “HDP” in column F for the visible rows. If I step through using F8, it works as expected. If run it using F5, is pastes the acronym “HDP” starting with the row past the visible range for another 2000 rows. The workbook has many tabs and the input page is 2000 rows. It seems like its picking up the range on the Input tab. I can add code to clear the contents if needed but not sure why its picking up the range of another sheet.

VBA Code:
Dim Xrow As Long, dng As Range
With newSheet
Xrow = Cells(Rows.Count, "A").End(xlUp).Row

'This is the Column to paste "HDP"
Set dng = .Range("F2:F" & Xrow)
'Filter out just stores that start with a 2 or 3
.Range("a1:G1").AutoFilter Field:=3, Criteria1:= _
"=inventory - 2****", Operator:=xlOr, Criteria2:="=inventory - 3****"

If Range("a1:a" & Xrow).SpecialCells(xlCellTypeVisible).Count < 2 Then
  GoTo 77
End If

'Filter out store that don’t have cycle count.
.Range("A1:G1" & Xrow).AutoFilter Field:=4, Criteria1:="cycle count audit"

If .Range("a1:a" & Xrow).SpecialCells(xlCellTypeVisible).Count < 2 Then
    GoTo 77
End If

'Paste HDP in visbile cells
dng.SpecialCells(xlCellTypeVisible) = "HDP"
End With
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
How about
VBA Code:
Dim Xrow As Long, dng As Range
With NewSheet
   Xrow = .Cells(Rows.Count, "A").End(xlUp).Row
   
   'This is the Column to paste "HDP"
   Set dng = .Range("F2:F" & Xrow)
   'Filter out just stores that start with a 2 or 3
   .Range("a1:G1").AutoFilter Field:=3, Criteria1:= _
      "=inventory - 2****", Operator:=xlOr, Criteria2:="=inventory - 3****"
     
   'Filter out store that don’t have cycle count.
   .Range("A1:G1" & Xrow).AutoFilter Field:=4, Criteria1:="cycle count audit"
   
   If .Range("a1:a" & Xrow).SpecialCells(xlCellTypeVisible).Count > 1 Then
      .AutoFilter.Range.Offset(1).Columns(6).Resize(Xrow - 1).Value = "HDP"
   End If
End With
 
Upvote 0
Solution
You're welcome & thanks for the feedback.

For future reference when Marking as solution, you need to select the post that helped the most, not your post saying it works. I have changed it for you this time.
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,462
Members
448,965
Latest member
grijken

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