Need Help Selecting Only Visible Cells During Loop VBA

sax2play

Board Regular
Joined
Mar 13, 2021
Messages
63
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

I have a loop set up that works flawlessly until I attempt to filter a column and select only the visible cells. I have a configuration table that dynamically defines which columns I want to move to a new sheet. Any help allowing this to select only visible cells would be greatly appreciated!

VBA Code:
Do While WB.Sheets("Config_Review").Cells(includedCol, 1) <> ""
    
        If WB.Sheets("Config_Review").Cells(includedCol, 1) = "" Then
            Set rngFound = WB.Sheets("EaselBoard_1").Range("1:1").Find(What:=WB.Sheets("Config_Review").Cells(includedCol, 1), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=True)
        Else
            colString = WB.Sheets("Config_Review").Cells(includedCol, 1)
            Set rngFound = shEB.Range("1:1").Find(What:=colString, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=True)
        End If
                
        If Not rngFound Is Nothing Then
            shNewEB.Range(shNewEB.Cells(1, Col), shNewEB.Cells(last_row, Col)).Value = shEB.Range(shEB.Cells(1, rngFound.Column), shEB.Cells(last_row, rngFound.Column)).Value
            
        Else
            If colString = "" Then
                MsgBox "Cannot find column: " & WB.Sheets("Config_Review").Cells(includedCol, 1)
                End
            Else
                MsgBox "Cannot find column matching: " & colString
                
            End If
            
            Col = Col - 1
                       
        End If
        
        Col = Col + 1
        includedCol = includedCol + 1
        
    Loop

Thanks!!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Nevermind - just needed to step away for a few minutes! I used below:

VBA Code:
Do While WB.Sheets("Config_Review").Cells(includedCol, 1) <> ""
    
        If WB.Sheets("Config_Review").Cells(includedCol, 1) = "" Then
            Set rngFound = WB.Sheets("EaselBoard_1").Range("1:1").Find(What:=WB.Sheets("Config_Review").Cells(includedCol, 1), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=True)
        Else
            colString = WB.Sheets("Config_Review").Cells(includedCol, 1)
            Set rngFound = shEB.Range("1:1").Find(What:=colString, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=True)
        End If
                
        If Not rngFound Is Nothing Then
            last_row2 = shEB.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count
            shNewEB.Range(shNewEB.Cells(1, Col), shNewEB.Cells(last_row2, Col)).Value = shEB.Range(shEB.Cells(1, rngFound.Column), shEB.Cells(last_row2, rngFound.Column)).Value
            
        Else
            If colString = "" Then
                MsgBox "Cannot find column: " & WB.Sheets("Config_Review").Cells(includedCol, 1)
                End
            Else
                MsgBox "Cannot find column matching: " & colString
                
            End If
            
            Col = Col - 1
                       
        End If
        
        Col = Col + 1
        includedCol = includedCol + 1
        
    Loop
 
Upvote 0
Solution

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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