Sometimes ones thinking gets stuck in a loop, and you miss the obvious.
Posts by mar050703
Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.
-
-
HI Carim
Thankyou so very much for your patience and helpful guidance.
It worked, but by changing the line
to,
as the first line now had the path twice (I guess because of fsopath and cmMyPath)?
Thanks again for aiding my learning.
-
-
Thanks Carim
I tested this, as you suggested in a module of its own, and first instead of opening workbook etc, I called a msgbox to give the file name. This worked, however...
When I implemented the line
again it opens the 1st file (Cash Management FEBRUARY 2019 TEST) on each next file.
Almost there, if you could suggest the issue I really would be grateful.
Thanks
-
Hi Carim, Yes I have done that. 1st thing I checked. However, I can now confirm by referencing fso like this:
CodeDim fso As Object Dim fsopath As Scripting.Folder Set fso = New FileSystemObject Set fsopath = fso.GetFolder(cmMyPath)
I now do not get any errors, but it keeps opening up the same file, it is not scrolling through to the next.
so
appears to be always the 1st file in the sub folder Maureen. (for testing purposes there are 4 files, and only 2 are like "Cash Management"
Do I need to reference fso.getfiles or something?
Thanks
-
Hi Carim Thanks for your comments, and no I don't mind, we are all learning, and as I am self taught in VB, even more so.
Your 3rd point Cell I3 is looking at the folder location - it reads:
I2 being the username.
This line works, as I use it in other codes within this project.Your 2nd Point, I refer to MyFile = "Cash Management v1.4.xltx", is what I need to do on the xlsx file I also do within this xltx file - Please note that that the xltx file is in folder refered to in I3, and the xlsx files will be in a subfolder "Maureen).
The xltx and the mentioned xlsx workbooks both have 2 sheets ("Expenses" and "Validate") the template become and xlsx workbook, when completed.
As requested, please find the entire code.
Please note that when I posted the original yesterday, I noticed I had forgotten to declare fso as an object.
Code
Display MorePrivate Sub CommandButton1_Click() Dim Sht As Worksheet Dim tbl As ListObjects Dim F As String Dim n As Integer Dim wb As Workbook Dim MyPath As String Dim MyFile As String Dim cmMyPath As String Dim cmMyFile As String Dim MyExt As String Dim count As Integer Dim file As Variant Dim myobj As Object Dim MySource As Object Dim fso As Object Dim fsopath As Scripting.Folder MyExt = "*.xlsx" MyPath = Sheets("Sheet1").Range("I3") '"C:\Users\Owner\Dropbox\Accounts Miqlat\" cmMyPath = Sheets("Sheet1").Range("I3") & "Maureen\" MyFile = "Cash Management v1.4.xltx" cmMyFile = Dir(cmMyPath & MyExt) 'MySource = myobj.GetFolder(cmMyPath) Set fso = FileSystemObject Set fsopath = fso.GetFolder(cmMyPath) '**** Find Data and Change **** If OptionButton1 = False And OptionButton2 = False Then MsgBox "Select Option" Frame1.SetFocus Exit Sub End If 'F is looking at the correct combobox 'n is looking for the correct column If OptionButton1 = True Then F = ComboBox1.Value n = 5 Else F = ComboBox2.Value n = 6 End If ' ***** This line of code down to "Next File" will go to the bottom when sorted **** 'Also when I do the 2 sheets as an array, it only makes the changes within the sheet "Expenses" - even though "Validate" requires the change too For Each file In fsopath.Files If cmMyFile Like "*Cash Management*" Then Set wb = Workbooks.Open(Filename:=cmMyPath & cmMyFile) With Worksheets("Expenses") 'ActiveWorkbook.Worksheets(Array("Validate", "Expenses")) .Cells.Replace what:=F, replacement:=TextBox1.Value, _ LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchCase:=True, _ SearchFormat:=False, ReplaceFormat:=False End With With Worksheets("Validate") .Cells.Replace what:=F, replacement:=TextBox1.Value, _ LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchCase:=True, _ SearchFormat:=False, ReplaceFormat:=False End With ActiveWorkbook.Save ActiveWorkbook.Close End If Next file Exit Sub 'This is the main workbook where the VB is. For Each Sht In Worksheets(Array("Centre", "Consolidated", "Validate1")) Sht.Cells.Replace what:=F, replacement:=TextBox1.Value, _ LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchCase:=True, _ SearchFormat:=False, ReplaceFormat:=False Next Sht 'Check Tables and Column 5 or 6 and replace For Each Sht In Worksheets(Array("Dollar", "KHCCash", "KHCSavings", "KHCConCash", "KHCConstruction", "KHCChecking", _ "THCCash", "THCSavings", "THCConCash", "THCConstruction", "THCChecking", _ "AdminCash", "AdminSavings", "AdminConCash", "AdminConstruction", "AdminChecking")) Sht.Columns(n).Replace what:=F, replacement:=TextBox1.Value, _ LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchCase:=True, _ SearchFormat:=False, ReplaceFormat:=False Next Sht 'Open CashManagement Template and change in the Validate sheet 'Only the Validate sheet will have any changes required. Workbooks.Open Filename:=MyPath & MyFile, Editable:=True With ActiveWorkbook.Sheets("Validate") .Cells.Replace what:=F, replacement:=TextBox1.Value, _ LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchCase:=True, _ SearchFormat:=False, ReplaceFormat:=False ActiveWorkbook.Save ActiveWorkbook.Close End With 'Open any UnImported Cash Management sheets and change 'The code mentioned above will go here Unload Me End Sub
Any pointers and help would be greatly appreciated.
-
Hi Experts
I am trying to write some code so that it will loop through all the files in the folder and do certain things is the file name is like...
My Code is
Code
Display MoreDim wb as workbook Dim cmMyPath As String Dim cmMyFile As String Dim MyExt As String Dim file As Variant Dim fso As Scripting.FileSystemObject Dim fsopath As Scripting.Folder MyExt = "*.xlsx" MyPath = Sheets("Sheet1").Range("I3") '"C:\Users\Owner\Dropbox\Accounts Miqlat\" cmMyPath = Sheets("Sheet1").Range("I3") & "Maureen\" MyFile = "Cash Management v1.4.xltx" cmMyFile = Dir(cmMyPath & MyExt) Set fsopath = fso.GetFolder(cmMyPath) - 'Error Here "Object Variable or with block variable not set" For Each file In fsopath.Files If cmMyFile Like "*Cash Management*" Then Set wb = Workbooks.Open(Filename:=cmMyPath & cmMyFile) ' DO stuff ' close End If Next file
I am sure I am missing something, but if someone could point the way, I would be grateful.
-
Hi there
I am trying to count the number of files in a particular folder whereby the filename is like something.
When I have the total count, I need the code to loop through each file and do something
I am having problems counting the files though.
My Code looks like this:Code
Display MoreDim cmMyPath As String Dim cmMyFile As String Dim MyExt As String Dim count As Integer MyExt = "*.xlsx" cmMyPath = Sheets("Sheet1").Range("I3") & "Maureen\" cmMyFile = Dir(cmMyPath & MyExt) If cmMyFile Like "*Cash Management*" Then cmMyfile.count End If MsgBox count & " :Files" Exit Sub
Please note there may be other files in said folder that do not contain "Cash Management"
Maybe I don't need to count, I just need to loop through each file
-
Hi KjBox.
Thanks for the code.
On sheet Validate1,I also have a ListObject, and is just the driving force for Columns 5 & 6 on the 16 sheets I refer to. It is essentially made up of:
Header Row - Being the Categorys (Column 5)
DataBodyRange - Being Sub Categorys (Column 6), so column 6 choice depends of the answer of Column 5.Wouldn't your code also the check ListObject(1) on the Validate1 Sheet?
Hope that make sense?
-
Hi There,
Thanks graha_karya I have implemented your solution, and managed to sort the issue. I am sure there is a better/quicker (runtime) than this, so if anyone has a suggestion I would be happy to read.
My final code is:
Code
Display MoreIf OptionButton1 = True Then F = ComboBox1.Value n = 5 Else F = ComboBox2.Value n = 6 End If For Each Sht In Worksheets(Array("Centre", "Consolidated", "Validate1")) Sht.Cells.Replace what:=F, replacement:=TextBox1.Value, _ LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchCase:=True, _ SearchFormat:=False, ReplaceFormat:=False Next Sht 'Check Tables and Column 5 or 6 and replace For Each Sht In Worksheets(Array("Dollar", "KHCCash", "KHCSavings", "KHCConCash", "KHCConstruction", "KHCChecking", _ "THCCash", "THCSavings", "THCConCash", "THCConstruction", "THCChecking", _ "AdminCash", "AdminSavings", "AdminConCash", "AdminConstruction", "AdminChecking")) Sht.Columns(n).Replace what:=F, replacement:=TextBox1.Value, _ LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchCase:=True, _ SearchFormat:=False, ReplaceFormat:=False Next Sht
Please note the second "For Each" statement is looking at the sheet and not the ListObject within that sheet (which was my ideal).
-
Hi, and Thanks for your comments.
Just to be clear, aside from the 3 sheets which are in the aforementioned Array, I have another 10 sheets each with an Excel Table (Listobject), where by I would need to locate combobox1.value within the 5th Column of that List object (ideally just within Databodyrange).
Therefore I am unable to change the above code, as that would just look at column 5 of the original sheets in the Array.
Apologies if I did not make myself clear.
-
Thanks Carim,
The final code was
CodeFor Each Sht In Worksheets(Array("Centre", "Consolidated", "Validate1")) Sht.Cells.Replace what:=ComboBox1.Value, Replacement:=TextBox1.Value, _ LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchCase:=True, _ SearchFormat:=False, ReplaceFormat:=False Next Sht
the error was simply the sheet name was "Consolidated" and not "Consolidate"
However, I now have a much larger problem, and looking for the quickest way for the code to work.
I have 10 sheets, all with Excel Tables, and I would like the code to only check a certain column (always column 5) and do the same find replace. (Table per sheet)
So how do I get it to loop through these 10 tables (please note within the workbook there are more than 10 tables.
Thanks for your help
-
Hi,
I am trying to write code that will find something based on Combobox, and replace with the textbox value. I am trying to do this over an array of sheets.
I am using the following code:
CodeFor Each Sheet In Array("Centre", "Consolidate", "Validate1") Sheet.Cells.Replace what:=ComboBox1.Value, Replacement:=TextBox1.Value, _ LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchCase:=True, _ SearchFormat:=False, ReplaceFormat:=False Next Sheet
however I get the object required error on the
line
once I get this working I will also need to do the same on multiple excel tables (10 of them).
Thanks for your help
-
All Solved, I changed the code so it looked at a filesystemobject, therefore the new code is:
-
Hi
I am trying to get save as PDF and the actual file, and if it the file already exists, then give it a version 1 (or more).
My code is as follows:
Code
Display MoreWith Sheets(Array("Admin", "KHC", "THC", "Consolidated")).Select FileN = Mth 'FileName If Dir(SvPath & FileN & ".pdf") <> "" Then Do Debug.Print n = n + 1 NewFile = FileN & " (v" & n & ")" 'New File if Exists Loop Until NewFile <> "" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SvPath & NewFile 'BckUp is the Backup Location ThisWorkbook.SaveAs (BckUp & NewFile), FileFormat:=xlOpenXMLWorkbookMacroEnabled ThisWorkbook.Close Else ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SvPath & FileN & ".pdf" 'BckUp is Backup Locartion ThisWorkbook.SaveAs (BckUp & FileN), FileFormat:=xlOpenXMLWorkbookMacroEnabled ThisWorkbook.Close
However, the coding is happy to give a (v1), but then says file already exists and does not increase it to (v2).
If I change
it just keeps scrolling through the loop
Any suggestions please?
-
Perfect.
Thanks again. It appears in the testing I did, I just forgot the ".index"
We live and learn!
-
Carim
Of course, I was being silly and used DataBodyRange before the ListColumns.
Sorry, one last question, where I choose field 7 as the filter column, can I refer again to the Column Name ("End Date")
Something like
-
Hi,
I trust someone will be able to help me,
I currently have a piece of code, which refers to an Excel Table, and I filter a column to give me a new range, but I am currently filtering using the column number;
What I would like to do is use the refer to the column name, but I keep getting Run Time Error 13.
The code I have is:
CodeDim FRng As Range Sheets("Input").ListObjects(1).Range.AutoFilter field:=7, Criteria1:="" Set FRng = Sheets("Input").ListObjects(1).DataBodyRange.Columns(2).SpecialCells(xlCellTypeVisible)
The error is the final line - I want to change columns(2) to Columns("Employee").
I have also tried ListColumns("Employee"), but that gives Run Time Error 438
The only reason for the change so that if columns are added to the left in the future, it will still use the correct column
-
Perfect, thanks so very much.
-
Hi There,
I am sure there is a better, and faster way for my code to work. What I am trying to do, is loop through 4 different ranges, and clear contents.
What I have at present is:
Code
Display MoreSub Clear() Dim Rng1 As Range Dim Rng2 As Range Dim Rng3 As Range Dim Rng4 As Range Set Rng1 = Sheets("Input").ListObjects("Data").ListColumns("Days Worked").DataBodyRange Set Rng2 = Sheets("Input").ListObjects("Data").ListColumns("O/T Hours Worked").DataBodyRange Set Rng3 = Sheets("Input").ListObjects("Data").ListColumns("Commission Value").DataBodyRange Set Rng4 = Sheets("Input").ListObjects("Data").ListColumns("Advance").DataBodyRange For Each cell In Rng1 cell.ClearContents Next For Each cell In Rng2 cell.ClearContents Next For Each cell In Rng3 cell.ClearContents Next For Each cell In Rng4 cell.ClearContents Next End Sub
Please can someone suggest a better way - I am sure it has to do looping through the ranges.
Thanks in advance