VBA to open workbook and copy selected cells

StuartWhi

Board Regular
Joined
Sep 1, 2011
Messages
75
Hi Guys,

After reviewing many threads, grabbing code from several unfortunately I’m still stuck and look forward to any suggestions.
- Used Excel for years and only now trying to learn VBA.

Running Excel 2007 on Vista.

What I would like to do,
- From an open file copy data from an excel file the user is prompted to open "ActiveWorkbook",
- ActiveWorkbook will have up to 6 WorkSheets to copy from, 1 x “Description” (always) and 5 x “Data” but the “Description” cells has to be added in front of the “Data” cells copied,
- Cells to be copied from both “Description” and “Data” worksheets are not consecutive or grouped thus I will have to manually code all these in (I will add this later).
- “Data” Worksheet name may vary (by the end users before submitting) and often not all are required, thus it would be good to check for any value in cell “E15” if yes process if not stop.
- Copy data into ThisWorkbook per row for each set of “Description”&”Data” worksheet (moving to the next row for the next copy and next workbook).
- At the end of each row place the Workbook & WorkSheet name.
I have included the below code as this addresses many areas but getting issues (I hope this will help you understand what I’m trying to do), look forward to any ideas on adjustment or totally new.

Thanks in advance and love the detail I can get from this website.

Code:
Option Explicit
Sub GetData()
Dim myFile As Workbook, sn As String, sn2 As String, NR As Long

Range("A1").Value = Application.GetSaveAsFilename()
myFile = ActiveWorkbook.Name

' Sheetname(s) to be copied
sn = Sheet6     '"Discription"
sn2 = Sheet2    '"Data_1"
'sn3 = Sheet5    '"Data_2"
'sn4 = Sheet12   '"Data_3"
'sn5 = sheet13   '"Data_4"
'sn6 = sheet14   '"Data_5"

    With ThisWorkbook.Sheets("Summary")
      NR = .Cells(Rows.Count, 1).End(xlUp).Row + 1
      
      'D39, L34 from worksheet "Description"
      With .Range("A" & NR)
        .Formula = "='" & myFile & sn & "'!D39"
        .Value = .Value
      End With
      With .Range("B" & NR)
        .Formula = "='" & myFile & sn & "'!L34"
        .Value = .Value
      End With
      
      'F14, G14 from worksheet "Data_1"
      With .Range("D" & NR)
        .Formula = "='" & myFile & sn2 & "'!F14"
        .Value = .Value
      End With
      With .Range("E" & NR)
        .Formula = "='" & myFile & sn2 & "'!G14"
        .Value = .Value
        .Offset(, 1).Value = myFile
      End With
    End With
  
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,215,507
Messages
6,125,212
Members
449,214
Latest member
mr_ordinaryboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top