Selecting visible cells only on grouped sheet

picklefactory

Well-known Member
Joined
Jan 28, 2005
Messages
508
Office Version
  1. 365
Platform
  1. Windows
Hello all
Having some problems with some code and hoping for some help if possible please? Bit of a newbie, so please go easy if my code is rubbish.

OK, I'm creating an analysis sheet that needs to draw data from one of a selection of workbooks chosen by the user, basically a simple copy/paste function. The source w/books will all be in the same format, so the source sheet will be defined by the code, but the user needs to select the latest updated workbook to draw from, I'm opening a dialogue box for this purpose in the correct drive and folder for ease of use.

My issue is that I cannot get the code to simply select the cells to copy. The dailogue box works fine, the selected workbook opens and the designated sheet is activated, but I then get a "400" VBA error (That's all the message box says, no description) and it falls over on the range.select function. I cannot fathom it I'm afraid, although I bet it's something stupid I'm doing.
The source sheet has had sub-totals grouped and I only want to select and copy the visible grouped cells, so I wonder if that is affecting anything. I created it via the macro recorder and everything worked OK as I stepped through that. I can't even get it to select a single cell range. Struggling.

I've enclosed my code below, please excuse my simplistic comments, they are for my reference and also my lousy error handling at this point, I'm trying to feel way my through that too, but I'll worry about that later, hence why I've commented that out for now.
Thanks folks and hope that all makes sense.

Code:
Sub Open_SFMOP_File()
'This code pulls year and month selection from Sheet 3 and concatenates them into
'the file path to search for the required SFMOP sheet to open via dialog box
    
'On Error GoTo ErrHandler:

Dim Year As String
Dim Month As String
Dim FPath As String
Year = Range("C2").Value ' Takes Year value from C2
Month = Range("C3").Value ' Takes month value from C3
ChDrive "S" ' Changes to drive to search, this must be done before file path can be found below
ChDir "S:\Operations_Share\Business Reports\Weekly Planning\" & Year & "\" & Month & "\SFMOP\" ' Select desired path INCLUDING inserted values
Application.Dialogs(xlDialogOpen).Show ' Open dialog box

'Copy and paste data from SFMOP to this work book
    Sheets("Labour & SBH").Select
    Range("B1:N265").Select
    Range("N265").Activate
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    ActiveWindow.Close
    Windows("MinMaxAnomalies090211.xlsm").Activate
    Sheets("Sheet4").Select
    Range("A1").Select
    ActiveSheet.Paste
    Range("A1").Select
    
    
'ErrHandler:
 '   If Err.Number = 76 Then
        ' path does not exist, file name needs correcting
'MsgBox "File path is not correct check the file name and folders are correctly named on Share drive"
'Exit Sub
'    End If
'    MsgBox "Error.......... Bugger!!!"
'    Exit Sub
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Sussed it folks!!
I thought it would be something stupid, I simply needed to add ActiveSheet to the range.select functions. I have another question, but probably should be a seperate thread, so I'll start a new one.
Thanks
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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