Solved

Opening file to copy columns

Posted on 2014-02-04
4
199 Views
Last Modified: 2014-02-05
I need help when I open the workbook to copy columns]]
   on this line.
   Workbooks.Open(DIV).Activate  ''>>>>>>>>>>>>>What do I need to place here to reopen workbook??????????????????
The excel file that is opened will have a different name each time - so used DIV.


VAR_DIV = DIV ----------VAR_DIV is a text box



Thanks in advance

Sub SELECT_Div()
Dim DIV As String
 Dim Lastrow As Integer

    DIV = Application.GetOpenFilename(FileFilter:="xls*.Files (*.xls*), *.xls*", Title:="Please select a file")
   
    If DIV = "" Then
        MsgBox "No File Selected"
            Exit Sub
        Else
    End If
      'browse boxes may be on the main parm sheet i.e. Sheets

     Sheets("Variances").VAR_DIV = DIV  'sheet has text box VAR_DIV
 Workbooks.Open (DIV)
         
   ' Cells.Copy
       Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row
 
     Range("A4:E & Lastrow").Select
    Selection.Copy
    Windows("Template.xlsm").Activate
    Range("B3").Select
    ActiveSheet.Paste
   
   Workbooks.Open(DIV).Activate  ''>>>>>>>>>>>>>What do I need to place here to reopen workbook??????????????????

Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row
 
    Range("G4:G7 & Last row").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("Template.xlsm").Activate
     Range("L3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
       
End Sub
0
Comment
Question by:leezac
  • 2
  • 2
4 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39834309
Note how I set a workbook variable to DIV when you open it. You can then refer to wb when you want to close that workbook or activate it.
Sub SELECT_Div()
Dim DIV As String
Dim wb As Workbook            'Brad added this statement
Dim Lastrow As Integer

    DIV = Application.GetOpenFilename(FileFilter:="xls*.Files (*.xls*), *.xls*", Title:="Please select a file")
    
    If DIV = "" Then
        MsgBox "No File Selected"
            Exit Sub
        Else
    End If
      'browse boxes may be on the main parm sheet i.e. Sheets

     Sheets("Variances").VAR_DIV = DIV  'sheet has text box VAR_DIV
Set wb = Workbooks.Open(DIV)             'Brad changed this statement
         
   ' Cells.Copy
       Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
 
     Range("A4:E & Lastrow").Select
    Selection.Copy
    Windows("Template.xlsm").Activate
    Range("B3").Select
    ActiveSheet.Paste
   
   wb.Activate  'Brad changed this statement to use a workbook variable to point to DIV

Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
 
    Range("G4:G7 & Last row").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("Template.xlsm").Activate
     Range("L3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
       
End Sub

Open in new window

0
 

Author Comment

by:leezac
ID: 39835600
Thanks but am getting an error on this line


Range("A4:E & Lastrow").Select


VBA error 1004 - select method of range class failed
0
 
LVL 81

Expert Comment

by:byundt
ID: 39835616
That line should have been:
Range("A4:E" & Lastrow).Select

Note how the last doublequote moved.

A similar problem occurs on statement 31, which should read:
Range("G4:G" & Lastrow).Select
0
 

Author Comment

by:leezac
ID: 39835899
I see - thanks
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question