I have encountered a problem regarding the workbook.open function, when trying to open a workbook located on a network folder. The VBA macro leeds to an
"1004 Error"
without any specific reasons, only that the file path is not available.
I have used Google and this community for a very long time to solve this issue, below my steps I tried and my only solution at the end.
My question is: WHY does Excel behave like that and what can I do the next time?
Initially the user inputs the file path in a cell within the Source Workbook, saved locally on the computer. The VBA code take the input of the cell (I tried Range("K4") and also Range("K4").value) and aligns it to the string, which is visible (Variable Watch while Debugging) but failes when it comes to the Workbook.open function.
I tried to use the user specific network path (e.g. "G:/...") but also the Universal Network convention path ("\\xxx.xxx...") which is more accurate because not every user has mapped the network folder to the same drive letter.
At the end my only working solution was the hard coded path in the VBA editor with the UNC path.
Why is so? In this case the networkpath does not change, but when it comes to the moment where it is necessary that the folder must be written in a cell I will be lost.
Thank you for your feebdack!
EDIT:
Basically it's this code... I removed the unnecessary parts...
'Variablen
Dim MA$, Monat$, Fehltag$, Ort$, Projekt$, FilePlanung$, MainString$, NeuerString$
Dim LastRowM&, StartZelleP&, ProjektP&
Dim wb, wbP As Workbook
Dim wsK, wsS, wsM As Worksheet
Dim StartDatumM As Date
Dim array_monate As Variant
'Arbeitsblätter
Set wb = ThisWorkbook
Set wsK = wb.Sheets("Kopfblatt")
Set wsS = wb.Sheets("Stammdaten")
Set wsM = wb.ActiveSheet
'Fix
MA = wsK.Range("D2")
Monat = wsM.Name
FilePlanung = wsS.Range("K4")
Application.ScreenUpdating = False
Set wbP = Workbooks.Open(fileName:=FilePlanung)
'Set wbP = Workbooks.Open(FilePlanung) --> Tried also this and many other ways...
Set wsP = wbP.Sheets("aktuell")
This is the code I use:
Dim wb As Workbook
Set wb = Workbooks.Open(Worksheets("Sheet1").Range("A1").Value)
Related
I have a number of files that are broken up by steps in a folder. For example:
1.xlsx
2.xlsx
3.xlsx
(etc. to about 9 files)
Each of these files contain cells or graphs to copy to a monthly PowerPoint presentation that's already formatted and ready, just needs the data and would like to avoid using links.
I'm stuck at pasting data into the open file.
Sub summary()
Dim pindex As Long
Dim wb As Excel.Workbook
Dim ppt As PowerPoint.Application
Dim pprsn As PowerPoint.Presentations
Dim ppslide As PowerPoint.Slide
'Whats the date of folder?
folder = InputBox("Please enter date")
directory = "" + folder
'Define the path for the summary PPTX
pptfile = directory + "\summary.pptx"
'Define the steps in Excel WB's
sn = directory + "\1.xlsx"
pu = directory + "\2a.xlsx"
pe = directory + "\2b.xlsx"
in = directory + "\3"
'****1*****
Set ppt = CreateObject("PowerPoint.Application")
ppt.Presentations.Open (pptfile)
Workbooks.Open (sn)
'Copy cells from sn
Worksheets("Snapshot").Range("A1:L29").Copy
'Paste into first slide in active pwerpoint presentation
???
End Sub
I tried different guides here and other websites. Most are creating a new PPT instead of editing one in the folder.
Any time I try to identify the indexID or use any other method it errors.
I'd like to work down the list copying each of the necessary cells & graphs into the PowerPoint presentation to help the team from having to rebuild their linked variables each month.
Any ideas?
I've figured it out. I should have been pasting directly with shapes vs. trying to select a slide index. Secondly, my dim statement for pprsn had an S which changed the arguments it could handle slightly.
At work we get a number X of e-mails per day which must be handled by 4 people. I am trying to come up with a macro that, when run, takes X and splits it randomly and equally into 4 subfolders so that every person knows what they have to do. I don’t have any coding background and my Excel knowledge is basic at best. How would I start and is it even doable?
Thanks!
Below is my pseudo-code for your requirement. Can you understand what I am suggesting? Can you suggest improvements? Try to convert my pseudo-code to VBA and update your question with your progress. I will respond with comments on your code if I think you are going wrong.
A issue to consider is where should this code be placed. It could be an Outlook macro or an Excel macro. An Outlook macro would be easier to write. But to run it, you would have to open Outlook and run the macro. With an Excel macro, you could create a specific workbook with an auto-run macro. You would simply click the workbook to start it and the emails would be distributed. A final message would tell how many emails had been distributed. The Excel macro would require greater understanding of linking different Microsoft Office products and some understanding of events.
Dim FldrInbox as Folder
Dim FldrsTeam(1 To 4) as Folder
Dim InxEmail as Long
Dim InxTeam as Long
Set FldrInbox = Reference to Shared Inbox
Set FldrsTeam(1) = Reference To Alice’s folder
Set FldrsTeam(2) = Reference To Bernard’s folder
Set FldrsTeam(3) = Reference To Christine’s folder
Set FldrsTeam(4) = Reference To David’s folder
InxTeam = Random number between 1 and 4
For InxEmail = FldrInbox.Items.Count To 1 Step -1
FldrInbox.Items(InxEmail).Move FldrsTeam(InxTeam)
If InxTeam = 4 Then
InxTeam = 1
Else
InxTeam = InxTeam + 1
End If
Next
Edit 1
The code below demonstrates the second technique for finding folders. It creates a file on your desktop containing an indented list of every store and folder to which you have access. Note: some of these folders are “secret” and are not listed in your folder pane because there is nothing you, as a human, can do with these folders. However, you could write a macro to access these folders if you wish.
I have lots of Outlook modules which I name for their contents. In general, I have one module per task. I also have some general modules. This code comes from my “ModDemonstrations”. You are just starting so you may not need several modules, but I have been writing Outlook VBA for 15 years and have lots of macros and organising them is essential.
This routine needs a reference to Microsoft Scripting Library. If you do not know what that means, I will add an explanation.
Run ListStoresAndAllFolders() and a file will be created on your desktop named “ListStoresAndAllFolders.txt”. You do not need this code today, but I believe you will find it helpful for the future. It uses recursion. Look up recursion. If you do not understand the explanations you find, I will try to write my own explanation.
Note that I write Dim FldrCrnt As Outlook.Folder. There are two types of Folder: Outlook and Scripting. A Scripting.Folder is a disc folder. If you progress to copying information from Outlook folders to disc folders, you must be careful to specify which type of folder you mean.
Sub ListStoresAndAllFolders()
' Displays the name of every accessible store
' Under each store, displays an indented list of all its folders
' Technique for locating desktop from answer by Kyle:
' http://stackoverflow.com/a/17551579/973283
' Needs reference to Microsoft Scripting Runtime if "TextStream"
' and "FileSystemObject" are to be recognised
Dim FileOut As TextStream
Dim FldrCrnt As Outlook.Folder
Dim Fso As FileSystemObject
Dim InxFldrChild As Long
Dim InxStoreCrnt As Long
Dim Path As String
Dim StoreCrnt As Outlook.Folder
Path = CreateObject("WScript.Shell").specialfolders("Desktop")
Set Fso = CreateObject("Scripting.FileSystemObject")
Set FileOut = Fso.CreateTextFile(Path & "\ListStoresAndAllFolders.txt", True)
With Application.Session
For InxStoreCrnt = 1 To .Folders.Count
Set StoreCrnt = .Folders(InxStoreCrnt)
With StoreCrnt
FileOut.WriteLine .Name
For InxFldrChild = .Folders.Count To 1 Step -1
Set FldrCrnt = .Folders(InxFldrChild)
Call ListAllFolders(FldrCrnt, 1, FileOut)
Next
End With
Next
End With
FileOut.Close
End Sub
Sub ListAllFolders(ByRef Fldr As Folder, ByVal Level As Long, ByRef FileOut As TextStream)
' This routine:
' 1. Output name of Fldr
' 2. Calls itself for each child of Fldr
' It is designed to be called by ListStoresAndAllFolders()
Dim InxFldrChild As Long
With Fldr
FileOut.WriteLine Space(Level * 2) & .Name
For InxFldrChild = .Folders.Count To 1 Step -1
Call ListAllFolders(.Folders(InxFldrChild), Level + 1, FileOut)
Next
End With
End Sub
I'm currently using a fixed file name in my code however I'd like to replace this fixed name with the value of a particular cell
I've looked extensively across threads to no luck - I'm quite new to coding so I've found similar issues but nothing I'm able to apply to my coding or adapt for exactly my needs
I've tried using
Set wbBook2 = Workbooks(wsSheet1_1.Range("O35").Value)
and a few other similar steps but to no avail
Dim wbBook1 As Workbook
Dim wsSheet1_1 As Worksheet
Dim wsSheet1_2 As Worksheet
Dim wbBook2 As Workbook
Dim wsSheet2_1 As Worksheet
Set wbBook1 = Workbooks("Main File.xlsm")
Set wsSheet1_1 = wbBook1.Worksheets("Example1")
Set wsSheet1_2 = wbBook1.Worksheets("Example2")
Set wbBook2 = Workbooks("Look Up File.xlsm")
Set wsSheet2_1 = wbBook2.Worksheets("Example3")
The above allows me to use 'wbBook2.Activate' to switch to the workbook where the data is located & take the necessary actions however I'd like to be able to change from using "Look Up File.xlsm" to cell O35 on wsSheet1_1.
Currently I'm just ensuring my look-up file has the same name as is noted in the code but this would obviously lead to failure if this is accidentally re-named or a user titled this LookUp File (omitting the space) for example.
Note that 'wbBook2' will already be open when this code is used & the file name can change. The user separately defines the full file path & name (including .xlsm) which is then opened in a separate macro that needs to remain separate
It's safer to loop through Workbooks and look for a match
Private Function set_wb(ByVal toName As String) As Workbook
' Function set_wb(<String>), returns Workbook Object on match.
' - if no match found, return Nothing
' - invokation example: Set wb1 = set_wb("Book1")
Dim wb As Workbook
For each wb in Application.Workbooks
If wb.Name = toName Then
Set set_wb = wb ' wb found
Exit Function
End If
Next wb
Set set_wb = Nothing ' wb not found
End Function
And would be invoked in your case the following way:
Set wbBook2 = set_wb(wsSheet1_1.Range("O35"))
So after searching through numerous other pages im still having difficulty and admittedly its due to self teaching myself VBA, so forgive me :(
I have created a simple Phone call counter Access form with a table. My goal is to press a button and it exports the table to a specified directory to an existing XLSM (the data exported would open a new worksheet with the current date as the worksheet name and then save it.
database location is here:
L:\Reports\TestCalltoolmetric.accdb
existing Excel file is here:
L:\Reports\Callcounterreports\MonthlyCallCounter.xlsm
What i have tried so far...
1.
Dim strTable As String
Dim strWorksheetPath As String
strWorksheetPath = "L:\Reports\Callcounterreports"
strWorksheetPath = strWorksheetPath & Format(Date, "ddmmmyy") & "Callreport.xlsx"
This exports my database to a new file with the name of the file as the date+callreport. It works but i dont know if i can use this command to accomplish my goal above (filename isnt really important but at the end of the month i need to provide call metrics so i need every days export all in one workbook so i can create totals).
2.
I tried using this:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Table1", "L:\Reports\Callcounterreports\MonthlyCallCounter.xlsm"
But it errors out saying that Access database engine could not find the object 'Table1'
3.
I also found through some furious googling a similar request but it was based off exporting a query...in an less than educated mannor i tried adapting it but never really was able to get it to work..
Dim appXL As Object
Dim wb As Object
Dim wks As Object
Dim xlf As String
Dim rs As DAO.Recordset
xlf = "L:\Reports\Callcounterreports\MonthlyCallCounter.xlsm" 'Full path to Excel file
Set rs = CurrentDb.OpenRecordset("Query1") 'Replace Query1 with real query name
Set appXL = CreateObject("Excel.Application")
Set wb = appXL.Workbooks.Open(xlf)
Set wks = wb.Sheets & Format (Date, "ddmmmyy") ' Sheet name
wb.Save
wb.Close
appXL.Quit
Set wb = Nothing
rs.Close
Set rs = Nothing
I know i am by far not the first person to probably ask about how to do this, but any help would be fantastic!
SO....after futher digging i figured out what i was doing wrong with at least using
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Table1", "L:\Reports\Callcounterreports\MonthlyCallCounter.xlsm"
I first had to rename the table from "Table1" to another name, which i chose "DataTable".
Second i made a mistake on the filepath portion of the command, the location was a networked location which showed up a drive letter. after putting the full and correct file path the form worked like a charm.
https://msdn.microsoft.com/en-us/vba/access-vba/articles/docmd-transferspreadsheet-method-access
i used this website to verify my commands, which now i feel silly that i hadnt found it before.
Big thank you to #dbmitch i think frustration got the better of me instead of really reading the error info..
I get the error 'Subscript Out of Range' when I run the following code; debug points me to the last line:
Dim SrcBook As Workbook
Dim TrgBook As Workbook
Dim SrcSheet As Worksheet
Dim TrgSheet As Worksheet
Dim Sheet_Name As String
Workbooks.Open (CalendarFile)
Sheet_Name = MonthName(Month(SrcSheet.Cells(SrcRow, "D").Value), False)
MsgBox ("Sheet_Name Value is: " & Sheet_Name)
Set TrgSheet = Workbooks(CalendarFile).Worksheets(Sheet_Name)
I have repeatedly verified that CalendarFile is a valid file name (I use the full path filename). Sheet_Name is also the valid name of a sheet in that Workbook. I get a similar error if I try to access the worksheets via numeric indexing [ie, Workbooks(CalendarFile).Worksheets(11) vs Workbooks(CalendarFile).Worksheets(November)]. The MsgBox call verifies that I'm feeding the WorkSheets() method the proper sheet name.
Lastly, ScrRow is properly defined - I am able to use this code to manipulate target WorkSheets in the same WorkBook as the macro is called from in toy/testing applications, but for some reason it is failing when I try to manipulate target WorkSheets in other (open) WorkBooks.
Any help would be greatly appreciated! Thank You!
If CalendarFile is a valid filename, that's your issue. The index you need for Workbooks() is the Workbook.Name, not its file path. For example, if CalendarFile was C:\Foo\Bar.xlsx, you need to use Bar.xlsx.
As for the explanation above, it really doesn't matter because you should really just grab a the reference that Workbooks.Open returns and just use that:
Set TrgBook = Workbooks.Open(CalendarFile)
Sheet_Name = MonthName(Month(SrcSheet.Cells(SrcRow, "D").Value), False)
MsgBox ("Sheet_Name Value is: " & Sheet_Name)
Set TrgSheet = TrgBook.Worksheets(Sheet_Name)
Found the solution, at least to this problem:
Workbooks.Open (CalendarFile)
Requires the full-path-name of the file to open, but further references to the file require just the file name - without any of the path attached. That is,
Workbooks(file_name_without_path.xlsx).Worksheets(Sheet_Name)
This is extremely annoying and should be fixed.