VBA, iterate over filtered table and use values

peterpaul

New Member
Joined
Jun 2, 2021
Messages
7
Office Version
  1. 2019
I would like to filter my table, then use a macro to go over every ID that is still visible, past this on the form of the next sheet and print that sheet. I made the following piece of code and it works as I want it too. I opened the file to day and tried to save it but I couldn't save it. So, I copied everything to a new workbook including my VBA and the same thing happened. After I closed it and opened it again, I got an error. So my question is, is there something wrong with my code?

VBA Code:
Sub Print_Form_Visible_Ids()

   Dim tbl As ListObject
   Dim rngTable As Range
   Dim rngArea As Range
   Dim rngRow As Range

   Set tbl = ActiveSheet.ListObjects(1)
   Set rngTable = tbl.DataBodyRange.Columns(1).SpecialCells(xlCellTypeVisible)
   
   Debug.Print "Filtered table: " & rngTable.Address

   For Each rngArea In rngTable.Areas
       Debug.Print "  Area: " & rngArea.Address

         For Each rngRow In rngArea.Rows
             Debug.Print "    Row: " & rngRow.Address
            
         '# Here
         Range(rngRow.Address).Select
         Selection.Copy
         Sheets("Sheet2").Select
         Range("B2").Select
         ActiveSheet.Paste
         ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
             IgnorePrintAreas:=False
         Sheets("Sheet1").Select
         Next
   Next

End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
The first error I got is something along the line of "Errors occurred while saving <Filepath> The file may still be saved by Microsoft Excel if you remove or restore certain features". So I tried that but Excel told me the file was to heavily damaged. When I go to VBA, I can't open or delete the module.
 
Upvote 0

Forum statistics

Threads
1,215,862
Messages
6,127,386
Members
449,382
Latest member
DonnaRisso

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