After finishing the macro for my client, he informed me that he had a Mac. I have tried to adjust the code for Macs but I do not have a Mac to test it on. My friend with a Mac was trying to help me but he did not have any luck.
I need to find all the file names in the Matters folder and put them in column B.
I need to find all the file names in the RCTI folder and put them in column D.
I hoped this line of code would do the trick but it hasn't.
strFile = Dir(strPath, MacID("XLSX"))
My friend ran the macro and got an error. He couldn't really explain the error to me.
Matters = Range("B3").Text
strPath = Matters
strFile = Dir(strPath, MacID("XLSX"))
Row = 7
Do While strFile <> ""
Range("B" & Row).Value = strFile
Row = Row + 1
strFile = Dir 'This moves the value of strFile to the next file.
Loop
Matters = Range("B3").Text
strPath = Matters
strFile = Dir(strPath, MacID("CSV"))
'strFile = Dir(strPath & "*.csv")
Do While strFile <> ""
Range("B" & Row).Value = strFile
Row = Row + 1
strFile = Dir 'This moves the value of strFile to the next file.
Loop
RCTI = Range("B4").Text
strPath = RCTI
strFile = Dir(strPath, MacID("XLSX"))
Row = 7
Do While strFile <> ""
Range("D" & Row).Value = strFile
Row = Row + 1
strFile = Dir 'This moves the value of strFile to the next file.
Loop
RCTI = Range("B4").Text
strPath = RCTI
strFile = Dir(strPath, MacID("CSV"))
Do While strFile <> ""
Range("D" & Row).Value = strFile
Row = Row + 1
strFile = Dir 'This moves the value of strFile to the next file.
Loop
Related
I made a post Copying cells from multiple files in one folder.
This answer was correct however I need to change it.
The code from this:
Sub Macro()
Dim StrFile As String, TargetWb As Workbook, ws As Worksheet, i As Long, StrFormula As String
Const strPath As String = "\\pco.X.com\Y\OPERATIONS\X\SharedDocuments\Regulatory\Z\X\" 'take care of the ending backslash
Set TargetWb = Workbooks("X.xlsm")
Set ws = TargetWb.Sheets("Macro")
i = 3
StrFile = Dir(strPath & "*.xls*") 'it returns all files having extensions as xls, xlsx, xlsm, xlsa, xlsb
Dim sheetName As String: sheetName = "S"
Do While Len(StrFile) > 0
StrFormula = "'" & strPath & "[" & StrFile & "]" & sheetName
ws.Range("B" & i).Value = Application.ExecuteExcel4Macro(StrFormula & "'!R24C3")
ws.Range("A" & i).Value = Application.ExecuteExcel4Macro(StrFormula & "'!R3C2")
i = i + 1
StrFile = Dir() 'needed to continue the iteration up to the last file
Loop
End Sub
In the folder where I pull the two data points from there are over 1000 workbooks. I only need the data from around 20/30 of these.
I was planning on getting all the data from this folder and then doing a quick play around to get to the stuff I need. The macro to pull from these 1000 docs is causing Excel to crash.
Is it possible to only pull the data from these files if part of the file name matches with a list of codes in the master sheet?
For example, in column B there are 20 codes listed "3333", "44444" , "562872" etc. and the only files I want are "ABCD 3333 BDBD", "AJKP 4444" and "hhhhh 562872 ha".
Using the function InStr() and an array could do the trick:
Sub Macro()
Dim StrFile As String, TargetWb As Workbook, ws As Worksheet, i As Long, StrFormula As String
Const strPath As String = "\\pco.X.com\Y\OPERATIONS\X\SharedDocuments\Regulatory\Z\X\" 'take care of the ending backslash
'this is the range where the filename codes are. Change as needed
Dim arr_files As Variant: arr_files = ThisWorkbook.Sheets("Master").Range("B2:B20")
Set TargetWb = Workbooks("X.xlsm")
Set ws = TargetWb.Sheets("Macro")
i = 3
StrFile = Dir(strPath & "*.xls*") 'it returns all files having extensions as xls, xlsx, xlsm, xlsa, xlsb
Dim sheetName As String: sheetName = "S"
Do While Len(StrFile) > 0
If Not file_to_process(StrFile, arr_files) Then GoTo skip_file
StrFormula = "'" & strPath & "[" & StrFile & "]" & sheetName
ws.Range("B" & i).Value = Application.ExecuteExcel4Macro(StrFormula & "'!R24C3")
ws.Range("A" & i).Value = Application.ExecuteExcel4Macro(StrFormula & "'!R3C2")
i = i + 1
skip_file:
StrFile = Dir() 'needed to continue the iteration up to the last file
Loop
End Sub
Private Function file_to_process(file_name As String, arr_files As Variant) As Boolean
Dim Key As Variant
For Each Key In arr_files
If InStr(1, file_name, Key, vbTextCompare) > 0 Then
file_to_process = True
Exit For
End If
Next Key
End Function
I've created a little function to check every filename for every code in the arr_files so if one filename has a code in the string, will check as true and get the data.
I want to run through a specific sheet (from & to) save those ws as a new file in a folder, if the folder doesn't exist then create.
I'm able to do it to one sheet.
ActiveSheet.Next.Select
If Range("F3").Value = "" Then
Windows("Import OT.xlsm").Activate
Sheets("Cash").Select
Dim filename101 As String
Dim path101 As String
Application.DisplayAlerts = False
path101 = Environ("UserProfile") & "\Dropbox\A271\5 Oppgjor\" & 2020 & "\"
filename101 = Range("B1").Value & ".xlsx"
ActiveWorkbook.SaveAs path101 & Range("A2") & "\" & Range("A1") & " " & filename101,xlOpenXMLWorkbook
Application.DisplayAlerts = True
Else
Cells.Select
Range("F3").Activate
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Dim Path1 As String
Dim fpathname1 As String
Path1 = Environ("UserProfile") & "\Dropbox\A271\4 Lonnslipper\"
fpathname1 = Path1 & Range("F3") & "\" & Range("F2") & " " & Range("B3") & ".xlsx"
path01 = Environ("UserProfile") & "\Dropbox\A271\4 Lonnslipper\" & Range("F3")
Dim path001 As String
Dim Folder As String
Folder = Dir(path01, vbDirectory)
If Folder = vbNullString Then
VBA.FileSystem.MkDir (path01)
ActiveWorkbook.SaveAs filename:=fpathname1, FileFormat:=51
ActiveWorkbook.Close
Sheets("Cash").Select
Else
ActiveWorkbook.SaveAs filename:=fpathname1, FileFormat:=51
ActiveWorkbook.Close
Sheets("Cash").Select
End If
End If
End Sub
I want this as a loop is because I have a few tens of sheets. For it to work I think I need to write it specific time, but with loop I learned I don't need to do that.
Excel file sheet
https://onedrive.live.com/view.aspx?resid=AF6FF2618C09AC74!29027&ithint=file%2cxlsx&authkey=!AHcJjYCu8D0NTNY
According to your comment where you wrote the steps:
Read the comments
Try to run the code using F8 key and see where you need to change it.
As you're learning, please note to first write the steps in plain English Norsk and then develop your code.
See how I just followed your steps with readable code.
Code:
Public Sub GenerateCustomersFiles()
' 1) Active sheet (oppgjør 1-20)
Dim targetSheet As Worksheet
For Each targetSheet In ThisWorkbook.Sheets
' Check only sheets with string in name
If InStr(targetSheet.Name, "Oppgjør") > 0 Then
' 2) look if value in F3 is empty
If targetSheet.Range("F3").Value = vbNullString Then
' 3) if it is, do select "cash" sheet and save this file (its name and path are given above what it should be named)
Dim fileName As String
Dim filePath As String
Dim folderPath As String
folderPath = Environ("UserProfile") & "\Dropbox\A271\5 Oppgjor\" & 2020 & "\"
fileName = targetSheet.Range("B1").Value & ".xlsx"
filePath = folderPath & targetSheet.Range("A2") & "\" & targetSheet.Range("A1") & " " & fileName
ThisWorkbook.Worksheets("Cash").Select
ThisWorkbook.SaveAs filePath, xlOpenXMLWorkbook
Else
' 4) if it doesn't, do open selected sheet to a new workbook and save that in clients name folder (folder and path given above in code section)
folderPath = Environ("UserProfile") & "\Dropbox\A271\4 Lonnslipper\" & targetSheet.Range("F3")
fileName = targetSheet.Range("F2") & " " & targetSheet.Range("B3") & ".xlsx"
filePath = folderPath & "\" & fileName
' 5) check if clients folder exist or not for the file to be saved in.
' if folder doesnt exist,
' create new and save file there.
CreateFoldersInPath folderPath
' if folder exist just save the file there
Dim targetWorkbook As Workbook
Set targetWorkbook = Workbooks.Add
targetSheet.Copy before:=targetWorkbook.Sheets(1)
targetWorkbook.SaveAs filePath, 51
targetWorkbook.Close
End If
End If
Next targetSheet
End Sub
' Credits: https://stackoverflow.com/a/31034201/1521579
Private Sub CreateFoldersInPath(ByVal targetFolderPath As String)
Dim strBuildPath As String
Dim varFolder As Variant
If Right(targetFolderPath, 1) = "\" Then targetFolderPath = Left(targetFolderPath, Len(targetFolderPath) - 1)
For Each varFolder In Split(targetFolderPath, "\")
If Len(strBuildPath) = 0 Then
strBuildPath = varFolder & "\"
Else
strBuildPath = strBuildPath & varFolder & "\"
End If
If Len(Dir(strBuildPath, vbDirectory)) = 0 Then MkDir strBuildPath
Next varFolder
'The full folder path has been created regardless of nested subdirectories
'Continue with your code here
End Sub
Let me know how it goes
I want to set the value of a cell to be the hyperlink to another workbook.
I set the variable "strPath" to be the path that leads to the workbook I want to copy.
However, I am not sure my syntax is correct. Any help would be much appreciated.
For index = 3 To 37
If sheet.Cells(index, 3) = "OPEN" Then
dateColumn = FindDate(portfolioDate)
portfolioName = ActiveSheet.Range("A" & index & "").Value
strPath = "G:\Risk\Risk Reports\VaR-Stress test\" & portfolioDate & "\" & portfolioName & ""
path = "strPath.xls"
Set wb = Workbooks.Open(strPath)
sheet.Cells(index, dateColumn).Hyperlink = path
wb.Close Savechanges:=False
End If
Next
I have a Code, that copies Data from other files and put them in to a table (opened Workbook). How can i exclude opened Workbook (from where i starting the macro) and for example some specific files (C:\Users\Desktop\text.xlsm) from the loop? I tried with If strFile <> ActiveWorkbook.Name Then but it doesn't work, it still try to open the opened Workbook.
Here a part of the Code with the Loop:
Set oWks0 = ActiveSheet
aCells = Split(Zellen, ",")
iNextLine = iStartZeile
For ialngFolders = LBound(avntFolders) To UBound(avntFolders)
strFile = Dir$(avntFolders(ialngFolders) & "*.xlsm")
Do Until strFile = vbNullString
Set oWkb1 = Workbooks.Open(avntFolders(ialngFolders) & strFile)
Set oWks1 = oWkb1.Sheets(1)
For i = 0 To UBound(aCells)
'If strFile <> ActiveWorkbook.Name Then
oWks0.Cells(iNextLine, iStartSpalte).Offset(0, i).Value = _
oWks1.Range(aCells(i)).Value
'End If
Next
Call oWkb1.Close(SaveChanges:=False)
iNextLine = iNextLine + 1
strFile = Dir$
Loop
Next
Workbooks.Open is the method to open workbook
Using
If strFile <> ActiveWorkbook.Name Then
before the line
Set oWkb1 = Workbooks.Open(avntFolders(ialngFolders) & strFile)
I am trying to loop through all emails in a mailbox and extract the attachment from each. The loop works as expected if I don't have the delete included. With the delete included it works for the first email and on the "next" statement exits the for loop, skipping the remaining email. I could separate out the delete to a new loop but that seems inefficient.
For Each itm In Inbox.Items
For Each objAtt In itm.Attachments
sEmailDate = Format(itm.ReceivedTime, "dd/mm/yyyy")
sDataDate = Format(DateAdd("d", -1, CDate(sEmailDate)), "dd/mm/yyyy")
sFileName = objAtt.Filename
sSubject = itm.Subject
'Check if the report was sent today
If sEmailDate = sTodayDate And sFileName = "Report.csv" Then
bToday = True
End If
'Look for Report file
If sFileName = "Report.csv" Then
'Save it to the save folder, as the DisplayName. This will overwrite previously saved copies of this file
objAtt.SaveAsFile saveFolder & "\" & "report" & sSubject & "_" & Format(sDataDate, "yyyymmdd") & ".csv"
If Err.Number = 0 Then
itm.Delete 'without this istwill loop correctly
iReportCount = iReportCount + 1
Else
GoTo ExitLoop
End If
End If
Next objAtt
Next itm
Use the For loop instead of For Each one:
Dim items as Outlook.Items
Set items = Inbox.Items
For i = items.Count to 1 Step -1
For Each objAtt In itm.Attachments
sEmailDate = Format(itm.ReceivedTime, "dd/mm/yyyy")
sDataDate = Format(DateAdd("d", -1, CDate(sEmailDate)), "dd/mm/yyyy")
sFileName = objAtt.Filename
sSubject = itm.Subject
'Check if the report was sent today
If sEmailDate = sTodayDate And sFileName = "Report.csv" Then
bToday = True
End If
'Look for Report file
If sFileName = "Report.csv" Then
'Save it to the save folder, as the DisplayName. This will overwrite previously saved copies of this file
objAtt.SaveAsFile saveFolder & "\" & "report" & sSubject & "_" & Format(sDataDate, "yyyymmdd") & ".csv"
If Err.Number = 0 Then
itm.Delete
iReportCount = iReportCount + 1
Else
GoTo ExitLoop
End If
End If
Next
Next