+ Reply to Thread
Results 1 to 5 of 5

While writing files from another workbook, keep empty cells empty!

Hybrid View

nivoe While writing files from... 11-09-2012, 11:35 AM
Norie Re: While writing files from... 11-09-2012, 11:48 AM
nivoe Re: While writing files from... 11-09-2012, 12:06 PM
Norie Re: While writing files from... 11-09-2012, 12:17 PM
nivoe Re: While writing files from... 11-09-2012, 12:25 PM
  1. #1
    Registered User
    Join Date
    10-11-2012
    Location
    switzerland
    MS-Off Ver
    Excel 2010
    Posts
    38

    While writing files from another workbook, keep empty cells empty!

    Hello everyone

    First off the code:
    Sub Get_Data()
        Workbooks.Open Filename:="C:\work\A.xlsx"
        Windows("Main.xlsm").Activate
        Sheets("Sheet1").Select
        Range("A1:ET2000").Select
        Selection.FormulaR1C1 = "=[A.xlsx]Sheet1!R1C1:R2000C150"
    End Sub
    With this code, I write all Data from Sheet1 in A.xlsx into Sheet1 in Main.xlsx.
    It is working perfectly with one exception.

    If, e.g. in A.xlsx there are empty cells, the cells in Main.xlsx show zeros (=0).

    How could I make it that empty cells in A show as empty cells in Main?

    Help is much appreciated
    Cheers

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: While writing files from another workbook, keep empty cells empty!

    Do you want to have formulas linked to A.xlsx?

    Or would the values be OK?
    Sub Get_Data()
    Dim wbA As Workbook
    
        Set wbA = Workbooks.Open Filename:="C:\work\A.xlsx"
    
        wbA.Worksheets("Sheet1").Range("A1:ET2000).Copy
    
        ThisWorkbook.Sheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
    
        wbA.Close SaveChanges:=False
    End Sub
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    10-11-2012
    Location
    switzerland
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: While writing files from another workbook, keep empty cells empty!

    All I want is to get the values (but to keep the conditional formatting in Main.xlsx)

    If your above code is like a manual copy and paste (which overwrites cond. form.) I cannot use it.

    Cheers

    btw. you above line (wbA.Worksheets("Sheet1").Range("A1:ET2000").Copy
    ) throws an exception:

    Run Time Error 91, Object Variable or With Block Variable not set
    Last edited by nivoe; 11-09-2012 at 12:11 PM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: While writing files from another workbook, keep empty cells empty!

    Why can't you use what I posted?

    All it does is copy and paste values, it doesn't overwrite any formatting.

    there were a couple of typos.
    Sub Get_Data()
    Dim wbA As Workbook
    
        Set wbA = Workbooks.Open(Filename:="C:\A.xlsx")
    
        wbA.Worksheets("Sheet1").Range("A1:ET2000").Copy
    
        ThisWorkbook.Sheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
        
        Application.CutCopyMode = False
        
        wbA.Close SaveChanges:=False
    End Sub

  5. #5
    Registered User
    Join Date
    10-11-2012
    Location
    switzerland
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: While writing files from another workbook, keep empty cells empty!

    Norie

    You are a life saver. It works. Thank you very much!

    Have a nice day

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1