You are here:

Excel/Workbook open update

Advertisement


Question
QUESTION: Hi Jerry Beaucaire ,

I have some coding which copy the data from existing excel file and then update to the other excel file, If the other file is not open, it should open and update. If not, it should update with opened file.  The below is the code, i am trying for...but, it doesn't work me.

Can you please help me on this....

===================================
Sub Update_Tracker()

Dim wb As Workbook
Dim sFullPath As String, pFullPath As String
Dim Tracker As String
Dim Datatype1 As String


Tracker = MsgBox("Do you want to update Tracker ?", vbYesNo, "Tracker Update")

If Tracker = vbYes Then

DataType1 = Sheets("Data Form").Range("B12").Value

With ActiveWorkbook
   sFullPath = .Sheets("Main Menu").Range("E7").Value      
End With
         
         
  Select Case DataType1
         
         Case "P"
         
         With ActiveWorkbook
         .Sheets("Tracker Master").Range("B10:Z10").Copy
         End With
         
         Set wb = Workbooks.Open(sFullPath)

         wb.Worksheets("O Type").Range("C" & Rows.Count).End(xlUp).Offset         (1).PasteSpecial _
         Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False
         
         Application.CutCopyMode = False
         
         With Worksheets("O Type")
         .Activate
         End With

         Case "O"
         With ActiveWorkbook
         .Sheets("Tracker Master").Range("B10:Z10").Copy
         End With
         
         Set wb = Workbooks.Open(sFullPath)

         wb.Worksheets("O Type").Range("C" & Rows.Count).End(xlUp).Offset         (1).PasteSpecial _
         Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False
         
         Application.CutCopyMode = False
         
         With Worksheets("O Type")
         .Activate
         End With
         
         
         End Select
 Else
 End If
 
End Sub
===================================
Your help will be greatly appreciated.

Thnx...

ngr.

ANSWER: Something like this:


On Error Resume Next
Set wb = Workbooks(sFullPath)
If wb Is Nothing Then Set wb = Workbooks.Open(sFullPath)


---------- FOLLOW-UP ----------

QUESTION: Superb..Thanks., However, i see another issue which i am bit not clear and poor here..hope you can point out where i am missing...

In the above case, i use the below code with your tip...but, it is not copying and pasting another value, in the another row...My goal is to copy and paste the value multiple times..

----
Case "P"
         With ActiveWorkbook
         .Sheets("Tracker Master").Range("B6:Z6").Copy
         
         On Error Resume Next
         Set wb = Workbooks(sFullPath)
         
         If wb Is Nothing Then
         
         Set wb = Workbooks.Open(sFullPath)
         
         wb.Worksheets("P Type").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial _
         Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False
         Else
         
         wb.Worksheets("P Type").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial _
         Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False
    
         End If
         End With
-------------------
Your help would be highly apprciated.

Thanks in advance,

ngr.

Answer
You can simplify that down to:

        Case "P"
            ActiveWorkbook.Sheets("Tracker Master").Range("B6:Z6").Copy
            On Error Resume Next
            Set wb = Workbooks(sFullPath)
            If wb Is Nothing Then Set wb = Workbooks.Open(sFullPath)
            wb.Worksheets("P Type").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues



As for pasting multiple times, you can use the RESIZE function to expand the nunmber of rows that get pasted INTO:

            wb.Worksheets("P Type").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(3).PasteSpecial xlPasteValues

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Jerry Beaucaire

Expertise

Excel Formulas, macros, automation. Microsoft Excel MVP - 2010. Code site with free code snippets and techniques: http://sites.madrocketscientist.com/jerrybeaucaires-excelassistant/files

Experience

Microsoft Excel MVP - 2010. I have my own extensive Excel help/code site: http://sites.madrocketscientist.com/jerrybeaucaires-excelassistant/files ===================== I have been offering free assistance as an Excel aid on many web sites for many years: (http://www.excelforum.com - JBeaucaire) ======== (http://www.askmehelpdesk.com/spreadsheets - JBeaucaire) ======= (http://www.mrexcel.com/forum - jbeaucaire)

Education/Credentials
Bachelor's Degree from Azusa Pacific University in Mathematics and Music Composition

Awards and Honors
Microsoft Excel MVP 2010

©2016 About.com. All rights reserved.