VBA to Copy Specific Data From One Workbook

11
1. VBA to copy specific data from one workbook to another based on today's date? Update...I no longer need to have a date parameter - I just need my code revised to copy from one workbook to another, rather than one worksheet to another. Thank you!!!!! Hello, This is my first post to this forum (be kind! ) and I am relatively new to VBA and need some help. I am currently using VBA to copy specific data from one worksheet to another, but would like to change it to copy the same specific data from one workBOOK to another. My workbooks are named "PM-Test" (source) and "Changes"(destination).I really hope someone can help me! The code I am using currently to copy from one sheet to another follows. Thanks in advance for your assistance! Sub Copy1to2() Dim InSH As Worksheet Dim OutSH As Worksheet Dim i As Long Set InSH = Sheets("LOG SHEET") Set OutSH = Sheets("CLIENT REVIEW") Application.ScreenUpdating = False Application.EnableEvents = False OutSH.Range("A2:Y30000").ClearContents With Intersect(InSH.UsedRange, InSH.Columns("f")) .AutoFilter 1, "=Revision-DRG" .Offset(1).EntireRow.Copy OutSH.Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues .AutoFilter End With Application.CutCopyMode = False Application.EnableEvents = True Application.ScreenUpdating = True End Sub

description

VBA code to import excel

Transcript of VBA to Copy Specific Data From One Workbook

Page 1: VBA to Copy Specific Data From One Workbook

1. VBA to copy specific data from one workbook to another based on today's date?

Update...I no longer need to have a date parameter - I just need my code revised to copy from one workbook to another, rather than one worksheet to another.

Thank you!!!!!

Hello,

This is my first post to this forum (be kind! ) and I am relatively new to VBA and need some help. I am currently using VBA to copy specific data from one worksheet to another, but would like to change it to copy the same specific data from one workBOOK to another. My workbooks are named "PM-Test" (source) and "Changes"(destination).I really hope someone can help me! The code I am using currently to copy from one sheet to another follows. Thanks in advance for your assistance!

Sub Copy1to2() Dim InSH As Worksheet Dim OutSH As Worksheet Dim i As Long Set InSH = Sheets("LOG SHEET") Set OutSH = Sheets("CLIENT REVIEW") Application.ScreenUpdating = False Application.EnableEvents = False OutSH.Range("A2:Y30000").ClearContents With Intersect(InSH.UsedRange, InSH.Columns("f")) .AutoFilter 1, "=Revision-DRG" .Offset(1).EntireRow.Copy OutSH.Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues .AutoFilter End With Application.CutCopyMode = False Application.EnableEvents = True Application.ScreenUpdating = True End Sub

Page 2: VBA to Copy Specific Data From One Workbook

I'm looking for a macro that opens an other workbook (source.xlsm), loops through colum A in sheet 1 looking for a specifc text string ("Nordic") and if found copy the whole row to the open workbook from which the macro is run (nordic.xlsm).

The closed workbook contains thousands of rows of which maybe a few hundred are named "nordic" (the exact amount of

"nordic" rows changes every month) . The "nordic" rows should be copy to row 10 and donwards in the open wb.

Option Explicit

Sub ImportNORDIC()Dim wbSRC As Workbook, wsMASTER As WorksheetDim fPATHNAME As String, MyFilter As String, LR As Long

fPATHNAME = "C:\Path\To\File\source.xlsm"Set wsMASTER = ThisWorkbook.Sheets("Master") 'edit this sheetname as neededMyFilter = wsMASTER.Range("AA1").Value 'criteria to gather from the source file

On Error Resume NextwsMASTER.UsedRange.Offset(9).Clear 'clears prior entries, resets the Master sheet

Set wbSRC = Workbooks.Open(fPATHNAME)If wbSRC Is Nothing Then MsgBox "The Source.xlsm file could not be found" Exit SubEnd If

With wbSRC.Sheets("Sheet1") 'edit the sheetname as needed where the data resides .AutoFilterMode = False 'remove any prior filters .Rows(9).AutoFilter 'add a new filter .Rows(9).AutoFilter 1, MyFilter 'apply the 'nordic' filter test to column A LR = .Range("A" & .Rows.Count).End(xlUp).Row If LR > 9 Then 'if rows found, copy them all to the MASTER .Range("A10:Y" & LR).Copy 'copy the data in A:Y wsMASTER.Range("A10").PasteSpecial xlPasteValues 'paste the values only in the MASTER End IfEnd With

wbSRC.Close False 'close the source file, no changes saved

End Sub

Page 3: VBA to Copy Specific Data From One Workbook

Hi, I am a VBA novice and attempting to write a macro to copy a

worksheet "Sheet1" from a closed workbook into a similarly named

worksheet "Sheet1" in my active workbook.  I have the code (see below)

which does this fine, but I would like it to select the filename and path from a

cell in the active workbook which I can link to a drop-down list to make it

dynamic. I do not know how to integrate this in to my existing vba code and

would be most grateful if anyone out there might be able to help

Existing code below works well, but one needs to select a file from the

browser rather than having the path and file name extracted from a

predetermined cell in the active workbook. Any suggestions?

Option Explicit

Sub Sample()

    Dim wb1 As Workbook, wb2 As Workbook

    Dim Ret1, Ret2

    Set wb1 = ActiveWorkbook

    '~~> Get the File

    Ret1 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _

    , "Please select file")

    If Ret1 = False Then Exit Sub

    Set wb2 = Workbooks.Open(Ret1)

    wb2.Sheets(1).Cells.Copy wb1.Sheets(1).Cells

    wb2.Close SaveChanges:=False

    Set wb2 = Nothing

    Set wb1 = Nothing

End Sub

You can open file in separate session with parameter visible = false, but

you open it anyway.

Page 4: VBA to Copy Specific Data From One Workbook

If you want to take data from cells witch Closed file (without open) you can

use GetValue function. It use old method ExecuteExcel4Macro

Take look on this code:

'--- my example

Sub getValie()

Dim p$, F$, s$, r&, c&, a, ile_wierszy&, ile_kolumn&

Const FileName$ = "C:\file_name.xls" '<- change this file with path

p = Left(FileName, Len(FileName) - Len(Dir(FileName)) - 1)

F = Dir(FileName)

s = "Arkusz1" '<- dest. worksheet name - change it

ile_wierszy = 0 'how many rows

Application.ScreenUpdating = False

'check rows count

For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row

a = Cells(r, 1).Address

If GetValue(p, F, s, a) <> 0 Then

ile_wierszy = ile_wierszy + 1

Else

Exit For

End If

Next r

'check column counts

For c = 1 To Cells(1, Columns.Count).End(xlToLeft).Column

a = Cells(1, c).Address

If GetValue(p, F, s, a) <> 0 Then

Page 5: VBA to Copy Specific Data From One Workbook

ile_kolumn = ile_kolumn + 1

Else

Exit For

End If

Next c

'copy date

For r = 1 To ile_wierszy

For c = 1 To ile_kolumn

a = Cells(r, c).Address

If GetValue(p, F, s, a) = 0 Then

Cells(r, c) = ""

Else

Cells(r, c) = GetValue(p, F, s, a)

End If

Next c

Next r

Application.ScreenUpdating = True

End Sub

'----------------

Private Function GetValue(path$, file$, sheet$, ref$)

Dim arg$

If Right(path, 1) <> "\" Then path = path & "\"

If Dir(path & file) = "" Then

GetValue = "No file find?"

Exit Function

Page 6: VBA to Copy Specific Data From One Workbook

End If

arg = "'" & path & "[" & file & "]" & sheet & "'!" & range(ref).range("a1").Address(, , xlR1C1)

GetValue = ExecuteExcel4Macro(arg)

End Function

Thanks Guys, although perhaps more than I need at the moment. All I was

looking for was how to integrate the syntax to address a "Path" & "File" &

"Sheet" that is included in my destination workbook into the below code

Option Explicit

Sub Sample()

    Dim wb1 As Workbook, wb2 As Workbook

    Dim Ret1, Ret2

    Set wb1 = ActiveWorkbook

    '~~> Get the File

    Ret1 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _

    , "Please select file")

    If Ret1 = False Then Exit Sub

    Set wb2 = Workbooks.Open(Ret1)

    wb2.Sheets(1).Cells.Copy wb1.Sheets(1).Cells

    wb2.Close SaveChanges:=False

    Set wb2 = Nothing

    Set wb1 = Nothing

End Sub

Definitely Workbooks.Open is not "copy worksheet from closed workbook"

method.

Sub Sample() 'code your use

Page 7: VBA to Copy Specific Data From One Workbook

Dim wb1 As Workbook, wb2 As Workbook

Set wb1 = ActiveWorkbook

'Dim Ret1 As Variant

'Ret1 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", , _

"Please select file")

'If Ret1 = False Then Exit Sub

'---------------

Dim wks As Worksheet: Set wks = ActiveSheet

Dim Ret1 As String

Ret1 = wks.Range("a1") 'your file with path like c:\path\file.xlsx

If FileExists(Ret1) = False Then

MsgBox "File not exists!" & vbCr & "Check file and filepath", _

vbExclamation, "Err"

Exit Sub

End If

'---------------

Set wb2 = Workbooks.Open(Ret1)

wb2.Sheets(1).Cells.Copy wb1.Sheets(1).Cells

wb2.Close False

Set wb2 = Nothing

Set wb1 = Nothing

End Sub

Page 8: VBA to Copy Specific Data From One Workbook

Public Function FileExists(FilePath As String) As Boolean

On Error GoTo blad

If Len(FilePath) = 0 Then Exit Function

FileExists = Len(Dir(FilePath, vbDirectory Or vbHidden Or vbSystem)) > 0

Exit Function

blad:

End Function

Guys, thanks for the very kind help. Unfortunately I still can't get it to work.

I folded the above vba code into the file, but got an error message

"File not found

Check path and filepath"

I suspect that I need to amend the code to incorporate the named cells

that contain the path, file and sheet names that I want pulled in, but I am

unclear clear as to how to do this.   Below I have included the route with

the named cells to the left.

Named CellDrive C:

Directory \Document\NPD\Path 'C:\Document\NPD\

FileName VOD-LN

FileType .XLSMFile [VOD-LN.XLSM]

FileRoute 'C:\Document\NPD\[VOD-LN.XLSM]Sheet Sheet1

SheetRoute 'C:\Document\NPD\[VOD-LN.XLSM]Sheet1'!

RangeFrom A1RangeTo B059

CellRange A1:B059FullRoute 'C:\Document\NPD\[VOD-LN.XLSM]Sheet1'!A1:B059

re:Category- would change to Question, but it seems that I would lose the

historic info if I did that, so I shall keep this in mind for future posts

Page 9: VBA to Copy Specific Data From One Workbook

kind regards

Now I do not know what you trying to do

If you how link two namerange in one string:

Ret1 = wks.Range("Drive") & _

wks.Range("Directory") & _

wks.Range("FileName") & _

wks.Range("FileType")

Apologies if I have not made myself clear. I have a macro in vba (incl in

first post) which copies "Sheet1" of a file in another workbook into the

"Sheet1" worksheet of the active workbook that I am in. So far so good. 

However, I wish to change the way I select the target workbook (the one

which I want to copy the "Sheet1" from) so that it takes the path and file

name from a named cell in my destination (the active) worksheet.  What I

am struggling with is the syntax to enable the macro to extract the path &

file & sheet from the similarly named cells.