ArisaAnsar
asked on
Excel - VBA (Formula- Updated)
SiddharthRout helped me with the following VBA codes last week. I have since added more columns to the spreadsheet so the calculations will have to be done on differnet columns. The attached spreadsheet includes the updated formulas. The Calculations should be done on the Destination worksheet, 3 rows after the last data row.
Everything else should remain the same.
Codes:
Sub Sample()
Dim wb As Workbook, ws As Worksheet
Dim LastRow As Long, DestRow As Long
Dim StrFile As String
Dim msgFooter As String
'~~> Footer Message
msgFooter = "Note: Account detail reports were posted to the BOM and Service Reports Portal in the Specials section." & Chr(10) & _
"Reports are called:" & Chr(10) & _
"Connect Edelivery-New Accts Enrolled April 1-15" & Chr(10) & _
"Connect SAC-New Accts With SAC April 1-15" & Chr(10) & _
"Connect FreeForever IRA-New Accts Enrolled April 1-15"
StrFile = Dir$(MyFolder & "*.xls")
Do While Len(StrFile)
Set wb = Workbooks.Open(MyFolder & StrFile)
Set ws = Sheets("DESTINATION")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
DestRow = LastRow + 3
ws.Range("D" & DestRow) = "Complex Totals"
ws.Range("G" & DestRow).Formula = "=SUM(G6:G" & DestRow - 1 & ")"
ws.Range("G" & DestRow).Copy
ws.Range("T" & DestRow & ":V" & DestRow & ",P" & DestRow & ":R" & DestRow & ",M" & DestRow & _
":N" & DestRow & ",J" & DestRow & ":K" & DestRow & ",H" & DestRow).PasteSpecial Paste:=xlPasteFormulas, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ws.Range("I" & DestRow).Formula = "=H" & DestRow & "/G" & DestRow
ws.Range("L" & DestRow).Formula = "=(K" & DestRow & "/J" & DestRow & ")-$I$" & DestRow
ws.Range("O" & DestRow).Formula = "=(N" & DestRow & "/M" & DestRow & ")-$I$" & DestRow
ws.Range("S" & DestRow).Formula = "=(Q" & DestRow & "+R" & DestRow & ")/P" & DestRow
ws.Range("W" & DestRow).Formula = "=(U" & DestRow & "+V" & DestRow & ")/T" & DestRow
ws.Range("X" & DestRow).Formula = "=O" & DestRow & "+S" & DestRow & "+W" & DestRow
ws.Rows(DestRow).NumberFor mat = "0"
ws.Range("I" & DestRow & ",L" & DestRow & ",O" & DestRow & ",S" & DestRow & _
",W" & DestRow & ",X" & DestRow).NumberFormat = "0.00%"
'~~> Add the relevent Text to the Footer
With ws.PageSetup
.LeftFooter = msgFooter
End With
Application.StatusBar = " File " & wb.Name & " is being processed..."
wb.Close SaveChanges:=True
StrFile = Dir
Loop
MsgBox "Done"
Set ws = Nothing
Set wb = Nothing
End Sub
31-WEST-52ND-ST.xls
Everything else should remain the same.
Codes:
Sub Sample()
Dim wb As Workbook, ws As Worksheet
Dim LastRow As Long, DestRow As Long
Dim StrFile As String
Dim msgFooter As String
'~~> Footer Message
msgFooter = "Note: Account detail reports were posted to the BOM and Service Reports Portal in the Specials section." & Chr(10) & _
"Reports are called:" & Chr(10) & _
"Connect Edelivery-New Accts Enrolled April 1-15" & Chr(10) & _
"Connect SAC-New Accts With SAC April 1-15" & Chr(10) & _
"Connect FreeForever IRA-New Accts Enrolled April 1-15"
StrFile = Dir$(MyFolder & "*.xls")
Do While Len(StrFile)
Set wb = Workbooks.Open(MyFolder & StrFile)
Set ws = Sheets("DESTINATION")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
DestRow = LastRow + 3
ws.Range("D" & DestRow) = "Complex Totals"
ws.Range("G" & DestRow).Formula = "=SUM(G6:G" & DestRow - 1 & ")"
ws.Range("G" & DestRow).Copy
ws.Range("T" & DestRow & ":V" & DestRow & ",P" & DestRow & ":R" & DestRow & ",M" & DestRow & _
":N" & DestRow & ",J" & DestRow & ":K" & DestRow & ",H" & DestRow).PasteSpecial Paste:=xlPasteFormulas, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ws.Range("I" & DestRow).Formula = "=H" & DestRow & "/G" & DestRow
ws.Range("L" & DestRow).Formula = "=(K" & DestRow & "/J" & DestRow & ")-$I$" & DestRow
ws.Range("O" & DestRow).Formula = "=(N" & DestRow & "/M" & DestRow & ")-$I$" & DestRow
ws.Range("S" & DestRow).Formula = "=(Q" & DestRow & "+R" & DestRow & ")/P" & DestRow
ws.Range("W" & DestRow).Formula = "=(U" & DestRow & "+V" & DestRow & ")/T" & DestRow
ws.Range("X" & DestRow).Formula = "=O" & DestRow & "+S" & DestRow & "+W" & DestRow
ws.Rows(DestRow).NumberFor
ws.Range("I" & DestRow & ",L" & DestRow & ",O" & DestRow & ",S" & DestRow & _
",W" & DestRow & ",X" & DestRow).NumberFormat = "0.00%"
'~~> Add the relevent Text to the Footer
With ws.PageSetup
.LeftFooter = msgFooter
End With
Application.StatusBar = " File " & wb.Name & " is being processed..."
wb.Close SaveChanges:=True
StrFile = Dir
Loop
MsgBox "Done"
Set ws = Nothing
Set wb = Nothing
End Sub
31-WEST-52ND-ST.xls
ASKER
Hi Sid,
I'm getting the following error message:
"Run Time Error '104."
Unable to set the LeftFooter propery of the PageSetup Class."
I'm getting the following error message:
"Run Time Error '104."
Unable to set the LeftFooter propery of the PageSetup Class."
I haven't touched that part of the code.
Let me go through the code gain.
Sid
Let me go through the code gain.
Sid
ASKER
Hi,
I'm will be traveling within the next hour but will be able to test after 9:00 PM tonight.
I'm will be traveling within the next hour but will be able to test after 9:00 PM tonight.
ASKER
The files will have the old footer from last time I ran the code. I know there is a character limit so not sure if the code is trying to paste again to the footer instead of over writing the the existing.
No that is ok.
Well, I deleted some text from
'~~> Footer Message
msgFooter = "Note: Account detail reports were posted to the BOM and Service Reports Portal in the Specials section." & Chr(10) & _
"Reports are called:" & Chr(10) & _
"Connect Edelivery-New Accts Enrolled April 1-15" & Chr(10) & _
"Connect SAC-New Accts With SAC April 1-15" & Chr(10) & _
"Connect FreeForever IRA-New Accts Enrolled April 1-15"
and the macro ran perfectly.
Sid
Well, I deleted some text from
'~~> Footer Message
msgFooter = "Note: Account detail reports were posted to the BOM and Service Reports Portal in the Specials section." & Chr(10) & _
"Reports are called:" & Chr(10) & _
"Connect Edelivery-New Accts Enrolled April 1-15" & Chr(10) & _
"Connect SAC-New Accts With SAC April 1-15" & Chr(10) & _
"Connect FreeForever IRA-New Accts Enrolled April 1-15"
and the macro ran perfectly.
Sid
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much. I will test it again in one hour.
ASKER
Thank you! Thank you!
You are truely a Genius!!
This was perfect.
You are truely a Genius!!
This was perfect.
ASKER
Hi Sid,
In this section of the code, where the columns are being Sum, you have some columns identify with a semicolon and some identify with a comma. What is the difference? I bolded two examples below.
I have to update the codes to calculate on more columns but did not know if I should use a coma or a semi colon.
ws.Range("D" & DestRow) = "Complex Totals"
ws.Range("G" & DestRow).Formula = "=SUM(G6:G" & DestRow - 1 & ")"
ws.Range("G" & DestRow).Copy
ws.Range("T" & DestRow & ":V" & DestRow & ",P" & DestRow & ":R" & DestRow & ",M" & DestRow & _
":N" & DestRow & ",J" & DestRow & ":K" & DestRow & ",H" & DestRow).PasteSpecial Paste:=xlPasteFormulas, _
In this section of the code, where the columns are being Sum, you have some columns identify with a semicolon and some identify with a comma. What is the difference? I bolded two examples below.
I have to update the codes to calculate on more columns but did not know if I should use a coma or a semi colon.
ws.Range("D" & DestRow) = "Complex Totals"
ws.Range("G" & DestRow).Formula = "=SUM(G6:G" & DestRow - 1 & ")"
ws.Range("G" & DestRow).Copy
ws.Range("T" & DestRow & ":V" & DestRow & ",P" & DestRow & ":R" & DestRow & ",M" & DestRow & _
":N" & DestRow & ",J" & DestRow & ":K" & DestRow & ",H" & DestRow).PasteSpecial Paste:=xlPasteFormulas, _
If you want to select say cells A1, B1, C1 then you can write them in two ways
A1:C1
or
A1,B1,C1
Please note that ":" is used if the cells are continuous like A~>B~>C
Now let's take another example.
If you want to select A1, C1, E1 then you cannot use
A1:E1 as this will include Cells B1 and D1. In such a case you will use "," so it becomes
A1,C1,D1
Hope this helps :)
Sid
A1:C1
or
A1,B1,C1
Please note that ":" is used if the cells are continuous like A~>B~>C
Now let's take another example.
If you want to select A1, C1, E1 then you cannot use
A1:E1 as this will include Cells B1 and D1. In such a case you will use "," so it becomes
A1,C1,D1
Hope this helps :)
Sid
ASKER
Thank you very much. That helps.
Can you close the question I just posted for this same issue. I received a werid email from Expert Exchange so I taught you would not be able to see this?
Also, can you look at my other question which I posted a few days ago regard Access VBA. It really is for Excel VBA. I don't understand the help I'm currently getting.
>>> I don't understand the help I'm currently getting.
Arisa
In reference to this thread (I guess you are referring to https://www.experts-exchange.com/questions/26964864/Microsoft-Access-VBA-Updating-Code.html )
Akoster is an experienced Expert. Since he has already started helping, I would suggest that you address him directly regarding the problems that you are facing and I am sure he will be able to clear them out. If he is not then I will step in :) I will however still have a look at that thread :)
Sid
Arisa
In reference to this thread (I guess you are referring to https://www.experts-exchange.com/questions/26964864/Microsoft-Access-VBA-Updating-Code.html )
Akoster is an experienced Expert. Since he has already started helping, I would suggest that you address him directly regarding the problems that you are facing and I am sure he will be able to clear them out. If he is not then I will step in :) I will however still have a look at that thread :)
Sid
Open in new window
Sid