I am new to VBA programming and recently I have come accross this line of code at work and I couldn't find any answer for it from my colleagues, as none of them are coders.
S2_path = "\\wswvnascti0005\fin_pol_pcRegion\MENA\S2_reports\"
S2_file = "CEEMEA-StandardPLCEEMEA_with_daily-20" & year & month & day & ".csv"
folder_to = "\\wswvnascti0005\fin_pol_pcRegion\MENA\ALL\"
pnl_new = "MENA_Consolidated_PnL_20" & year & "-" & month & "-" & day & ".xlsm"
Application.Workbooks.Open S2_path & S2_file, No
The line of code that I'm talking about is the last line, where you can find the word No. What is the meaning of that word in this case?
Thanks for help guys and sorry for my English!
It's (incorrectly) saying not to update links (UpdateLinks) - should say False here.
The parameters as delineated by commas (as per below) and because it's after the first comma, it's related to UpdateLinks.
Another way that code could have been written would be Application.Workbooks.Open S2_path & S2_file, UpdateLinks:=False
Related
I am trying to write some VBA to update a report with the most current data daily from a table. This report collects all the data from this table and shows it in a presentable manner but I don't want it to grab previous days data, only update from 12:00am to 12:00pm for instance every day, then incorporate this with my code to export this report as a pdf (which already works, but just shows the whole table constantly). It should be possible as my report has dates that are stored as values, I just don't know how to go about it with If statements etc. Here is my code for a module that is connected to a macro that is automatically run daily.
Function Reportmacro()
On Error GoTo Reportmacro_Err
Dim fpath As String
' Check for year folder and create if needed
If Len(Dir("H:\TEST\" & Year(Date), vbDirectory)) = 0 Then
MkDir "H:\TEST\" & Year(Date)
End If
' Check for month folder and create if needed
If Len(Dir("H:\TEST\" & Year(Date) & "\" & MonthName(Month(Date), False), vbDirectory)) = 0 Then
MkDir "H:\TEST\" & Year(Date) & "\" & MonthName(Month(Date), False)
End If
' Check for day folder and create if needed
fpath = "H:\TEST\" & Year(Date) & "\" & MonthName(Month(Date), False) & "\" & Day(Date)
If Len(Dir(fpath, vbDirectory)) = 0 Then
MkDir fpath
DoCmd.OutputTo acOutputReport, "Changeover Car Report", "PDFFormat(*.pdf)", fpath & "\" & "CCReport" & Day(Date) & "_" & Month(Date) & "_" & Year(Date) & ".pdf", False, "", , acExportQualityPrint
End If
Reportmacro_Exit:
Exit Function
Reportmacro_Err:
MsgBox Error$
Resume Reportmacro_Exit
End Function
For some more background: My table includes a ChangeoverID, Formdate(a date that a corresponding form is completed then recorded in table), Formtime(same as form date but just time), CardID(card scanner id), EmployeeID, CarID, etc. I suppose the time here wont matter because I am aiming to get it reported daily, hopefully without changing the original table, just the report code?
Sorry for the confusion
The issue you're having is because your report itself is not filtered. Try changing the report itself to have a default filter of Formdate = Date -1, which I assume is how you are determining which information counts as "Yesterday."
The other alternative is to open the report with a WhereCondition, then output the open form, then close it. All of that can be done in VBA with a single function. If you need help writing that function, let me know and I'll edit this post.
Apologies guys, I have actually found a solution based on what most of you said. I ended up thinking about a filtering option for my report (especially since I just wanted to grab specifics without manipulating the raw data itself) Going into the report layout view I found the option to sort by date by right clicking the field and selecting one of the many options... "today" was one of them. Then I had the issue of this filter not being loaded on start up of the report... So I enabled that in the properties view which makes everything work as intended! Filter is loaded on startup, every day and the report is auto generated to save on a network share. Thanks for your help, it did lead me on the right track.
I don't know the syntax for handling double quotation marks within a formula.
Here is my code:
ActiveCell.Formula = "=IF($W9="A",1,IF($W9="B",2, IF($W9="C",3,0))))"
Any help is greatly appreciated.
You need to change the formula to
ActiveCell.Formula = "=IF($W9=""A"",1,IF($W9=""B"",2, IF($W9=""C"",3,0)))"
So, first you need to double quote and in your case you also need to count the number of "(" and ")"
You need to double quotes for them to appear correctly in the formula
So: ActiveCell.Formula = "=IF($W9=""A"",1,IF($W9=""B"",2, IF($W9=""C"",3,0))))"
should give you the following formula in the ActiveCell:
=IF($W9="A",1,IF($W9="B",2, IF($W9="C",3,0))))
What I typically do is use the chr function in vba. You give the function the ASCII code for the desired character, this case 34, and vba fills it in for you.
ActiveCell.Formula = "=IF($W9=" & chr(34) & "A" & chr(34) & ",1,IF($W9=" & chr(34) & "B" & chr(34) & ",2, IF($W9=" & chr(34) & "C" & chr(34) & ", 3,0)))"
Cybernetic.nomad's answer works well, but in my experience (and mainly with Access) being super explicit gets the point across for the application, even though readability is practically non-existent.
I would posit that the best solution would to make the if statements in vba (instead of using excel functions) and return the value to the active cell. Then double quotes shouldn't be an issue at all in this case.
I am trying to get my VB script to enter an Excel formula into a cell. Unfortunately I had no luck so far.
Sheets("ABC").Select
Range("B2").Value = "=IF(Mat_Location!F2=0;"";Mat_Location!F2)"
After some googling and searching on this site I figured it would be because of the quotation marks and so I tried changing the quotation marks to double quotation marks.
Sheets("ABC").Select
Range("B2").Value = "=IF(Mat_Location!F2=0;"""";Mat_Location!F2)"
That also didn't help.
Any help is appreciated. Thanks in advance.
Replace your multiple " with Chr(34), it's easier to debug it this way.
Also, there's no need to Select the sheet first.
Sheets("ABC").Range("B2").FormulaLocal = "=IF(Mat_Location!F2=0;" & Chr(34) & Chr(34) & ";Mat_Location!F2)"
Note: it seems your Excel's regional settings is having ; as a delimeter inside the formula. The default settings is , , so for my Excel setting (maybe also yours) it might be:
Sheets("ABC").Range("B2").Formula = "=IF(Mat_Location!F2=0," & Chr(34) & Chr(34) & ",Mat_Location!F2)"
I'm having an issue. Currently I have a couple vb modules working off one another that when executed will increment a drop down list, save a version of each option on the drop down list, and print out a copy as well.
Right now I'm using this filepath.
Sub G5()
'Update 20141112
Dim Path As String
Dim filename As String
Path = "C:\Users\MY.Name\Documents\Testing\" & _
Range("G5") & "\"
filename = Range("G5")
If ActiveSheet.Range("G5").Value = "" Then End
If ActiveSheet.Range("G5").Value = "NAMES" Then Exit Sub
ActiveWorkbook.SaveAs filename:=Path & filename & "-" & Format(Date, "mmddyyyy") & ".xlsm", FileFormat:=52
End Sub
So cell G5 contains the name (Last, First) of the person whose voucher this is. Each name is data validated and is identical to the name of their individual folder. Currently the script will save to their folder, but within those folders are 12 sub folders, one for each month. Is there any way for me to get the files to save into the correct month folder?
Cell I10 is the only cell that mentions the month by name, but in the format of "June Transit Reimbursement"
Any help would be appreciated. The script above runs in conjunction with two others, and although its doing 95% of what I need it to do, if I can get past this final hurdle the process will be 100% automated.
I'm trying to read through Like Operators and Option Compare Statements, but I'm struggling, and after reading so many posts here am hoping someone can help
Get the month by taking the first word from I10 and then put it in the file path assuming your folders use the same name formats that show up in I10.
parts = Split(Range("I10"), " ")
theMonth = parts(0) & " " & parts(1)
Path = "C:\Users\MY.Name\Documents\Testing\" & Range("G5") & "\" & theMonth & "\"
Updated to use first 2 words from cell using Mat's Mugs comments.
From this question and many other examples on the internet one can figure out how to automate the text addition to a MS Word table:
ProductTable.Cell(2, 1).Range.Text = "whatever text here"
But the problem is that the process is so slow that you can even see the lines being added one by one.
This is not a problem for a small application, but in my case I need to add about 6000 rows of text.
Is there any faster way to do this?
You can try, whether it is faster, if you first add the complete table content in CSV manner and then convert it to table with Range.ConvertToTable method.
E.g. in Word VBA:
With ActiveDocument
Set myRange = .Paragraphs(.Paragraphs.Count).Range
myRange.Text = _
"data11,data12,data13" & vbCrLf & _
"data21,data22,data23" & vbCrLf & _
"data31,data32,data33" & vbCrLf
myRange.ConvertToTable Separator:=wdSeparateByCommas 'not necessarily commas, depends on the Locale properties
End With
Greetimgs
Axel