Results 1 to 14 of 14

Thread: Importing From Closed Excel Workbook

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    8

    Importing From Closed Excel Workbook

    Hi everyone,

    I was wondering if someone could help out with some code. I'm doing a project at the moment and having difficulty starting it!!!!! (Newbie!) Basically i've got a blank workbook, with a button in it. When this button is clicked i want it to open up a dialog box so i can choose an excel file, and i want it then to copy the contents of that excel file and copy it to the workbook which contains the button.

    Any help would be much appreciated as im very new to this and still at the early learning stages!

    Thanks in advance

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Importing From Closed Excel Workbook

    Any help would be much appreciated as im very new to this and still at the early learning stages!
    Hi byrnen5

    Search the forums on the following

    1) getopenfilename
    2) Also see this tutorial.
    http://www.vbforums.com/showthread.php?t=391665

    Understand how to work with excel.

    Once the above is done, experiment with code and if you get stuck then post the code that you have tried and we will definitely help you
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    8

    Re: Importing From Closed Excel Workbook

    hey thanks for the links.

    ok so ive pretty much got it working, here's my code:

    Sub CopyRanges()
    Dim wb As Workbook
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets

    ws.UsedRange.ClearContents

    Next ws


    fName = Application.GetOpenFilename("*,*.xls", , "Please select file to open")


    If wb Is Nothing Then
    Set wb = Workbooks.Open(fName)
    End If
    wb.ActiveSheet.Range("A1:Z1000").Copy
    ThisWorkbook.ActiveSheet.Range("A5:Z200").PasteSpecial Paste:=xlValues

    wb.Close

    End Sub

    Theres one thing that i want to change but not sure how to exactly. When it pastes in everything, i want cell values, formatting etc to be pasted also, so if a cell has a border its pasted in, how do i go about doing this?

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Importing From Closed Excel Workbook

    Change this

    Code:
    ThisWorkbook.ActiveSheet.Range("A5:Z200").PasteSpecial Paste:=xlValues
    to

    Code:
    ThisWorkbook.ActiveSheet.Range("A5:Z200").PasteSpecial Paste:=xlPasteAll, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    8

    Re: Importing From Closed Excel Workbook

    cheers that worked perfectly, one other thing i forgot to mention, when pasting in, how can i get it to just paste in values, some cells use formulas to get there values, i dont wont to copy this in, just the value thats currently in the cell

    thanks a million

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Importing From Closed Excel Workbook

    Need to issue different commands for different results.

    Either do paste values in the relevant cells first and then do paste all or vice versa but you need to give two different commands.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    8

    Re: Importing From Closed Excel Workbook

    i dont really know what you mean, sorry its just im not the best at this, i appreciate the help though

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Importing From Closed Excel Workbook

    Ok give me the cell names where you want to just paste values and cell where you want to paste all.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    8

    Re: Importing From Closed Excel Workbook

    Columns h and j are the ones that get their values from formulas, every other value is directly inputted

    so is there no way to paste everything as their values without specifying exactly which ones you want to do? like the last adjustment you made to my piece for pasting in formatting etc., is there nothing i can add to that to say paste in values only?

    Thanks in advance

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Importing From Closed Excel Workbook

    every other value is directly inputted
    When you say every other value, what range are we talking about?

    Can you upload your workbook after zipping it?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    8

    Re: Importing From Closed Excel Workbook

    I attached a sample file. The file i'll be importing will change slightly everytime (number of students and courses might change between courses or years) so therefore i cant give an exact range when importing the file (which is why i import a1:z200, just to be sure i get everything) from my file.

    The column m values are gotten from a formula, but when my macro pastes this into the workbook im working on it gives me formula errors for column m's, which i why i want to just input the values. The formulas aren't needed when i paste them in, the original values suffice.
    Attached Files Attached Files

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Importing From Closed Excel Workbook

    Is this what you are trying? If not then do let me know...

    Code:
      '~~> Paste with All formatting
    ThisWorkbook.ActiveSheet.Range("A5:Z200").PasteSpecial Paste:=xlPasteAll, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
      '~~> Convert Column A to L into Values
    ThisWorkbook.ActiveSheet.Columns("A:L").PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
      '~~> Convert Column N to Z into Values. this will ensure
    '~~> that Column M will retain formulas
    ThisWorkbook.ActiveSheet.Columns("N:Z").PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    8

    Re: Importing From Closed Excel Workbook

    I've got it working now, using:


    ThisWorkbook.ActiveSheet.Range("A5:Z200").PasteSpecial Paste:=xlPasteAll, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    ThisWorkbook.ActiveSheet.Columns("A5:Z200").PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    Worked perfectly

    Thanks for your help

  14. #14
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Importing From Closed Excel Workbook

    Great, if your query is solved then do remember to mark your thread resolved
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width