picklefactory
Well-known Member
- Joined
- Jan 28, 2005
- Messages
- 508
- Office Version
- 365
- Platform
- 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.
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