I want to search for an open file named "OTIF"[...].
When the macro finds the workbook, it should paste the file name into "sheet1" Cell A1 in another workbook (wb2).
Sub Filename()
Dim Wb1 As Workbook, wb2 As Workbook, wB As Workbook
For Each wB In Application.Workbooks
If Left(wB.Name, 4) = "OTIF" Then
Set Wb1 = wB
Exit For
End If
Next
If Not Wb1 Is Nothing Then
Set wb2 = ThisWorkbook
End If
With wb2.Sheets("AAA")
.Range("A1").Value = Dir(Wb1.FullName)
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "DONE!"
End Sub
I looked at your code and it can be simplified like below.
Sub Filename()
Dim wB As Workbook
For Each wB In Application.Workbooks
If Left(wB.Name, 4) = "OTIF" Then
ThisWorkbook.Sheets("AAA").Range("A1").Value = wB.FullName
Exit For
End If
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "DONE!"
End Sub
What do you want to achieve with
Dir(Wb1.FullName)
That is the source of your problem. Just delete the Dir and the () and its running: .Range("A1").Value = Wb1.FullName
Full Edited code:
Option Explicit
Sub Filename()
Dim Wb1 As Workbook, wb2 As Workbook, wB As Workbook
Dim ws As Worksheet
Set wb2 = ThisWorkbook
On Error Resume Next
Set ws = wb2.Worksheets("AAA")
On Error GoTo 0
If ws Is Nothing Then ' make sure "AAA" sheet exists
MsgBox "AAA sheet does not exist, or has been renamed", vbCritical
Exit Sub
End If
For Each wB In Application.Workbooks
If Left(wB.Name, 4) = "OTIF" Then
' for DEBUG ONLY
ws.Range("A1").Value = wB.Sheets.Count
' 2nd Debug option
ws.Range("A1").Value = "Test"
' ws.Range("A1").Value = wB.Name
MsgBox "DONE!"
Exit For
End If
Next
End Sub
Related
I'm simply trying to find the workbook beginning with D and containing the sheet "Bob".
This is my code, but it doesn't like wb.Name when I call the function. What am I supposed to put there so it works?
Sub View_Email()
'Select report workbook D*
For Each wb In Application.Workbooks
If wb1.Name Like "D*" And WorksheetExists("Bob", wb.Name) Then
Ct = Ct + 1
wb1.Activate
Set WB_rep = ActiveWorkbook
Exit For
End If
Next wb1
If Ct = 0 Then
MsgBox "Could not find D* file (report). Code will end."
Exit Sub
End If
'Copy Burn-Down chart from report
End Sub
Function WorksheetExists(shtName As String, Optional wb As Workbook) As Boolean
Dim sht As Worksheet
If wb Is Nothing Then Set wb = ThisWorkbook
On Error Resume Next
Set sht = wb.Sheets(shtName)
On Error GoTo 0
WorksheetExists = Not sht Is Nothing
End Function
Thank you for your help. The following code is working now:
Sub View_Email()
Dim wb As Workbook
'Select report workbook D*
For Each wb In Application.Workbooks
If wb.Name Like "D*" And WorksheetExists("Bob", wb) Then
Ct = Ct + 1
wb.Activate
Set WB_rep = ActiveWorkbook
Exit For
End If
Next wb
If Ct = 0 Then
MsgBox "Could not find D* file (report). Code will end."
Exit Sub
End If
'Copy Burn-Down chart from report
End Sub
Function WorksheetExists(shtName As String, Optional wb As Workbook) As Boolean
Dim sht As Worksheet
If wb Is Nothing Then Set wb = ThisWorkbook
On Error Resume Next
Set sht = wb.Sheets(shtName)
On Error GoTo 0
WorksheetExists = Not sht Is Nothing
End Function
Currently i am new on studying VBA for reporting and im still learning from it. moving on, may i ask a help on this one? :), my scenario is this.
i have data on 20 workbooks (POLY, BAYO, PROPO, TIPAS, CITRO....etc) with sheet name (Sheet1)
i have a single workbook for summary with many sheets, its sheet name is based on 20 workbook file name but not in alphabetical order. (Sheet name = CITRO, BAYO, PROPO, POLY, TIPAS....etc)
i want to copy the data on each workbook and paste it to their respective sheet name based on file name and specific cell ("B2:F2")
is it doable?
here's the code im trying to work on, the problem is, it is creating its own sheet instead of pasting it to my desire sheet.
Private Sub CommandButton1_Click()
Dim SourceBook As Workbook
Dim CurrentBook As Workbook
application.screenupdating = false
Set CurrentBook = ThisWorkbook
Set SourceBook = Workbooks.Open("C:\CITRO.xlsx")
SourceBook.Sheets("Sheet1").Range("B2:F2").Copy Destination:=CurrentBook.Sheets("CITRO").Range("R2:V2")
Set SourceBook = Workbooks.Open("C:\BAYO.xlsx")
SourceBook.Sheets("Sheet1").Range("B2:F2").Copy Destination:=CurrentBook.Sheets("BAYO").Range("R2:V2")
Set SourceBook = Workbooks.Open("C:\PROPO.xlsx")
SourceBook.Sheets("Sheet1").Range("B2:F2").Copy Destination:=CurrentBook.Sheets("PROPO").Range("R2:V2")
MsgBox "Completed"
Application.Workbooks("CITRO").Close
Application.Workbooks("BAYO").Close
Application.Workbooks("PROPO").Close
'SourceBook.Close
'Set SourceBook = Nothing
'Set CurrentBook = Nothing
'ThisWorkbook.Activate
'Application.Worksheets("Summary").Activate
'Application.Worksheets("Summary").Range("B2:F2").Select
End Sub
You need to close the SourceBook before opening a new one with SourceBook.Close SaveChanges:=False
Private Sub CommandButton1_Click()
Dim SourceBook As Workbook
Dim CurrentBook As Workbook
Application.ScreenUpdating = False 'don't forget to activate it in the end
Set CurrentBook = ThisWorkbook
Set SourceBook = Workbooks.Open("C:\CITRO.xlsx")
SourceBook.Worksheets("Sheet1").Range("B2:F2").Copy Destination:=CurrentBook.Worksheets("CITRO").Range("R2:V2")
SourceBook.Close SaveChanges:=False
Set SourceBook = Workbooks.Open("C:\BAYO.xlsx")
SourceBook.Worksheets("Sheet1").Range("B2:F2").Copy Destination:=CurrentBook.Worksheets("BAYO").Range("R2:V2")
SourceBook.Close SaveChanges:=False
Set SourceBook = Workbooks.Open("C:\PROPO.xlsx")
SourceBook.Worksheets("Sheet1").Range("B2:F2").Copy Destination:=CurrentBook.Worksheets("PROPO").Range("R2:V2")
SourceBook.Close SaveChanges:=False
Application.ScreenUpdating = True
MsgBox "Completed"
End Sub
Alternatively you can use a procedure to shorten it:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False 'don't forget to activate it in the end
CopyIntoThisWorkbook "C:\CITRO.xlsx", "CITRO"
CopyIntoThisWorkbook "C:\BAYO.xlsx", "BAYO"
CopyIntoThisWorkbook "C:\PROPO.xlsx", "PROPO"
Application.ScreenUpdating = True
MsgBox "Completed"
End Sub
Private Sub CopyIntoThisWorkbook(ByVal SourceFileName As String, ByVal DestinationSheetName As Range)
Dim SourceBook As Workbook
Set SourceBook = Workbooks.Open(SourceFileName)
SourceBook.Worksheets("Sheet1").Range("B2:F2").Copy Destination:=ThisWorkbook.Worksheets(DestinationSheetName).Range("R2:V2")
SourceBook.Close SaveChanges:=False
End Sub
And if the sheet name CITRO is always the filename CITRO.xlsx then you can even use an array with a loop:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False 'don't forget to activate it in the end
Dim SheetNameList() As Variant
SheetNameList = Array("CITRO", "BAYO", "PROPO") 'easily extendable
Dim SheetName As Variant
For Each SheetName In SheetNameList
CopyIntoThisWorkbook SheetName
Next SheetName
Application.ScreenUpdating = True
MsgBox "Completed"
End Sub
Private Sub CopyIntoThisWorkbook(ByVal DestinationSheetName As String)
Dim SourceBook As Workbook
Set SourceBook = Workbooks.Open("C:\" & DestinationSheetName & ".xlsx")
SourceBook.Worksheets("Sheet1").Range("B2:F2").Copy Destination:=ThisWorkbook.Worksheets(DestinationSheetName).Range("R2:V2")
SourceBook.Close SaveChanges:=False
End Sub
I wrote a code that is opening a window in which I can select a the excel workbook (#2) I want to copy and import the worksheet from.
The Code is then checking whether the wanted worksheet (named "Guidance") exists in the opened workbook (#2).If so it should be copied and pasted into the current workbook (#1).
After pasting the worksheet the workbook (#2) should be closed again.
So far the code does what I want it to do, as it opens the window and lets me select the wanted worksheet (named "Guidance") but I have the bug (not sure if the translation is correct)
"Runtime error '9': index out of range"
where the worksheet is supposed to be copied and pasted.
Any help on that would be very much appreciated! Thanks in advance.
Private Function SheetExists(sWSName As String, Optional InWorkbook As Workbook) As Boolean
If InWorkbook Is Nothing Then
Set InWorkbook = ThisWorkbook
End If
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets(sWSName)
If Not ws Is Nothing Then SheetExists = True
On Error GoTo 0
End Function
Sub GuidanceImportieren()
Dim sImportFile As String, sFile As String
Dim sThisWB As Workbook
Dim vFilename As Variant
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set sThisWB = ActiveWorkbook
sImportFile = Application.GetOpenFilename("Microsoft Excel Workbooks,
*xls; *xlsx; *xlsm")
If sImportFile = "False" Then
MsgBox ("No File Selected")
Exit Sub
Else
vFilename = Split(sImportFile, "|")
sFile = vFilename(UBound(vFilename))
Application.Workbooks.Open (sImportFile)
Set wbWB = Workbooks("sImportFile")
With wbWB
If SheetExists("Guidance") Then
Set wsSht = .Sheets("Guidance")
wsSht.Copy Before:=sThisWB.Sheets("Guidance")
Else
MsgBox ("No worksheet named Guidance")
End If
wbWB.Close SaveChanges:=False
End With
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
The issue is here
Set wbWB = Worksheets("Guidance") '<-- this should be a workbook not a worksheet?
With wbWB '<-- this with is useless until …
If SheetExists("Guidance") Then
Set wsSht = .Sheets("Guidance") '<-- … until Sheets here starts with a dot
wsSht.Copy Before:=sThisWB.Sheets("Guidance") 'if the error is here then there is no sheet "Guidance" in sThisWB
Else
MsgBox ("No worksheet named Guidance")
End If
wbWB.Close SaveChanges:=False
End With
Also note that SheetExists("Guidance") does not check in a specific workbook (which may fail). I recommend to extend the function to:
Private Function SheetExists(WorksheetName As String, Optional InWorkbook As Workbook) As Boolean
If InWorkbook Is Nothing Then
Set InWorkbook = ThisWorkbook 'fallback if not set
End If
Dim ws As Worksheet
On Error Resume Next
Set ws = InWorkbook.Worksheets(WorksheetName)
SheetExists = Not ws Is Nothing
On Error Goto 0 'necessary because the Err.Number will not be cleared on End Function
End Function
So you can test if a worksheet exists in a specific workbook like
SheetExists("Guidance", sThisWB)
SheetExists("Guidance", wbWB)
Sub GuidanceImportieren()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim sImportFile As String
sImportFile = Application.GetOpenFilename("Microsoft Excel Workbooks, *xls; *xlsx; *xlsm")
If sImportFile = False Then 'false should not be "false"
MsgBox "No File Selected"
Exit Sub
Else
Dim vFilename As Variant
vFilename = Split(sImportFile, "|")
Dim sFile As String
sFile = vFilename(UBound(vFilename))
Dim ImportWorkbook As Workbook
Set ImportWorkbook = Application.Workbooks.Open(sImportFile)
If SheetExists("Guidance", ImportWorkbook) Then
ImportWorkbook.Sheets("Guidance").Copy Before:=ThisWorkbook.Sheets("Guidance")
'you might need to change it into something like this:
Else
MsgBox "No worksheet named Guidance"
End If
ImportWorkbook.Close SaveChanges:=False
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Sub delete()
Dim sh As Worksheet, wb As String, c As Range
wb = InputBox("work book name")
Set sh = Workbooks(wb).Sheets
For Each Sheet In sh
If IsEmpty(sh.UsedRange) Then
sh.delete
End If
Next
End Sub
I am unable to delete the empty sheets using above code.
The below code deletes all empty sheets in the currently opened workbook
try this instead
Sub delete()
Application.DisplayAlerts = False
Dim sh As Worksheet
For Each sh In Sheets
If IsEmpty(sh.UsedRange) Then sh.delete
Next
Application.DisplayAlerts = True
End Sub
if you want to specify the full path with the name use
Sub delete()
Dim wb As Workbook, s As String
s = InputBox("Full workbook path & name")
Dim fileExists As Boolean
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fileExists = fso.fileExists(s)
If fileExist Then
Set wb = Workbooks.Open(s)
For Each Sheet In sh
If IsEmpty(sh.UsedRange) Then
sh.delete
End If
Next
Else
MsgBox "File doesn't exist", vbCritical, "Error"
End If
End Sub
I have an existing worksheet "StudentSheet1" which I need to add as many times as a user needs.
For eg, if a user enters 3 in cell "A1", saves it and closes the workbook.
I want to have three sheets: "StudentSheet1" , "StudentSheet2" and "StudentSheet3" when the workbook is opened next time.
So I will have the Code in "Workbook_Open" event. I know how to insert new sheets, but cant insert this particular sheet "StudentSheet1" three times
Here is my code:
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets.Add(Type:=xlWorksheet, After:=Worksheets(1))
Application.ScreenUpdating = True
End Sub
EDIT
Sorry I misread the question, try this:
Private Sub Workbook_Open()
Dim iLoop As Integer
Dim wbTemp As Workbook
If Not Sheet1.Range("A1").value > 0 Then Exit Sub
Application.ScreenUpdating = False
Set wbTemp = Workbooks.Open(Filename:="//Ndrive/Student/Student.xlsm")
wbTemp.Sheets("StudentSheet1").Copy After:=ThisWorkbook.Sheets(Sheets.Count)
wbTemp.Close
Set wbTemp = Nothing
With Sheet1.Range("A1")
For iLoop = 2 To .Value
Sheets("StudentSheet1").Copy After:=ThisWorkbook.Sheets(Sheets.Count)
ActiveSheet.Name = "StudentSheet" & iLoop
Next iLoop
.Value = 0
End With
Application.ScreenUpdating = True
End Sub
Why are you wanting to add sheets on the workbook open? If the user disables macros then no sheets will be added. As Tony mentioned, why not add the sheets when called by the user?
EDIT
As per #Sidd's comments, if you need to check if the sheet exists first use this function:
Function SheetExists(sName As String) As Boolean
On Error Resume Next
SheetExists = (Sheets(sName).Name = sName)
End Function
user793468, I would recommend a different approach. :)
wbTemp.Sheets("StudentSheet1").Copy After:=ThisWorkbook.Sheets(Sheets.Count)
is not reliable. Please see this link.
EDIT: The above code will fail if the workbook has defined names. Otherwise it is absolutely reliable. Thanks to Reafidy for catching that.
I just noticed OP's comment about the shared drive. Adding amended code to incorporate OP's request.
Tried and Tested
Option Explicit
Const FilePath As String = "//Ndrive/Student/Student.xlsm"
Private Sub Workbook_Open()
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim TempName As String, NewName As String
Dim ShtNo As Long, i As Long
On Error GoTo Whoa
Application.ScreenUpdating = False
Set wb1 = ActiveWorkbook
Set ws1 = wb1.Sheets("Sheet1")
ShtNo = ws1.Range("A1")
If Not ShtNo > 0 Then Exit Sub
Set wb2 = Workbooks.Open(FilePath)
Set ws2 = wb2.Sheets("StudentSheet1")
For i = 1 To ShtNo
TempName = ActiveSheet.Name
NewName = "StudentSheet" & i
If Not SheetExists(NewName) Then
ws2.Copy After:=wb1.Sheets(Sheets.Count)
ActiveSheet.Name = NewName
End If
Next i
'~~> I leave this at your discretion.
ws1.Range("A1").ClearContents
LetsContinue:
Application.ScreenUpdating = True
On Error Resume Next
wb2.Close savechanges:=False
Set ws1 = Nothing
Set ws2 = Nothing
Set wb2 = Nothing
Set wb1 = Nothing
On Error GoTo 0
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
'~~> Function to check if sheet exists
Function SheetExists(wst As String) As Boolean
Dim oSheet As Worksheet
On Error Resume Next
Set oSheet = Sheets(wst)
On Error GoTo 0
If Not oSheet Is Nothing Then SheetExists = True
End Function