Creare Workbook-uri

3
Option Explicit Public Sub CopySheetToAllWorkbooksInFolder() Dim sourceSheet As Worksheet Dim folder As String, filename As String Dim destinationWorkbook As Workbook 'Worksheet in active workbook to be copied as a new sheet to the 160 workbooks Set sourceSheet = ActiveWorkbook.Worksheets("Sheet1") 'Folder containing the 160 workbooks folder = "F:\temp\excel\" filename = Dir(folder & "*.xls", vbNormal) While Len(filename) <> 0 Debug.Print folder & filename Set destinationWorkbook = Workbooks.Open(folder & filename) sourceSheet.Copy before:=destinationWorkbook.Sheets(1) destinationWorkbook.Close True filename = Dir() ' Get next matching file Wend End Sub Sub CopyWorkbook() Dim currentSheet as Worksheet Dim sheetIndex as Integer sheetIndex = 1 For Each currentSheet in Worksheets Windows( "SOURCE WORKBOOK" ).Activate currentSheet. Select currentSheet.Copy Before:=Workbooks( "TARGET WORKBOOK" ).Sheets(sheetIndex) sheetIndex = sheetIndex + 1 Next currentSheet End Sub Dim strFileName strFileName=Sheets("SheetName").Range("B1") '...... your statements

description

Creare Workbook-uri multiple

Transcript of Creare Workbook-uri

Page 1: Creare Workbook-uri

Option Explicit

Public Sub CopySheetToAllWorkbooksInFolder()

Dim sourceSheet As Worksheet Dim folder As String, filename As String Dim destinationWorkbook As Workbook 'Worksheet in active workbook to be copied as a new sheet to the 160 workbooks Set sourceSheet = ActiveWorkbook.Worksheets("Sheet1") 'Folder containing the 160 workbooks folder = "F:\temp\excel\" filename = Dir(folder & "*.xls", vbNormal) While Len(filename) <> 0 Debug.Print folder & filename Set destinationWorkbook = Workbooks.Open(folder & filename) sourceSheet.Copy before:=destinationWorkbook.Sheets(1) destinationWorkbook.Close True filename = Dir() ' Get next matching file Wend

End Sub

Sub CopyWorkbook()

Dim currentSheet as Worksheet Dim sheetIndex as Integer sheetIndex = 1

For Each currentSheet in Worksheets

Windows("SOURCE WORKBOOK").Activate currentSheet.Select currentSheet.Copy Before:=Workbooks("TARGET WORKBOOK").Sheets(sheetIndex)

sheetIndex = sheetIndex + 1

Next currentSheet

End Sub

Dim strFileName

strFileName=Sheets("SheetName").Range("B1")

'...... your statements

wb.SaveAs strFileName

Page 2: Creare Workbook-uri

Sub copysheetfromanotherworkbook()Dim info'we check whether the workbook is open using a function that is shown belowinfo = IsWorkBookOpen("C:\takyar\Desktop\def.xlsm")If info = True ThenMsgBox "File is being used"

ElseMsgBox "file is closed!"End If' we open the workbook if it is closedIf info = False ThenWorkbooks.Open FileName:="C:\Users\takyar\Desktop\def.xlsm"End If' we copy the worksheet d into abc.xlsm after the sheet cSheets("d").Copy after:=Workbooks("abc.xlsm").Sheets("c")'we use the inputbox to rename the copied sheetSheets(Sheets.Count).Name = InputBox("Assign a new name")'finally we close the opened workbookWorkbooks("def.xlsm").Close

End Sub

' This function checks to see if a workbook is open or not. If the workbook is   ' open, it returns True. If the workbook is not open, it returns   ' false. Else, a run-time error occurs since  there is   ' some other problem accessing the workbook and we capture that error number for further action.

Function IsWorkBookOpen(FileName As String)

Dim FF As Integer, ErrNum As Integer

On Error Resume Next  ' We turn off error checking FF = FreeFile()  ' The inbuilt function gets a free file number.Open FileName For Input Lock Read As #FF 'we try to open the file and lock itClose FF ' Close the fileErrNum = Error ' capture the error number On Error GoTo 0  ' Turn on error checking

Page 3: Creare Workbook-uri

'Find which error happenedSelect Case ErrNum' No error' File is not  open Case 0: IsWorkBookOpen = False ' Error  for "Permission Denied." ' File already opened by another userCase 70: IsWorkBookOpen = True' Some other error occurred. Capture the error number for further actionCase Else: Error ErrNumEnd SelectEnd Function