Loop Through Columns to Insert SUM Formula - vba

I have a program where I need to find the sum of columns 7 through 30 (G through AD). What I am trying to do is loop through and insert the formula (=SUM(Columns(i)2:Columns(i)1000)) but obviously Columns(i) is not the letter but the number. This is not complying with the format required by SUM so I'm wondering what I can do instead.
I have a program where old sheets will be deleted and new ones added holding configuration data for a product. This means formulas cannot be in the sheet inself or any other sheet referring to it. Prices will be held in columns G through P and U through AD which I need to find the total for and place it in row 1 above the corresponding data. When I try:
For i = 7 To 30
wsNewSheet.Cells(1, i).Value = "=SUM(" & Columns(i).Select & "2:" & Columns(i).Select & "1000)"
Next i
Columns(i) is returning as "True" for some reason. I have also tried to place the totals in a different sheet as the new sheets (wsNewSheet) were being created.
For i = 2 To 30
For f = 7 To 30
wsTotals.Cells(1, i).Value = "=SUM(" & wsNewSheet & "!" & Columns(f).Select & "2:" & Columns(f).Select & "1000)"
Next f
Next i
However, this did not work either. This statement returned "Run-time error '438.' Object doesn't support this property or method." I attemped to do research on this error, but could not fix my situation. Thanks for any help.

You don't need loop here, use single line instead:
Range("G1:AD1").Formula = "=SUM(G2:G1000)"
Excel automatically adjust formulas:
in G1 you'd have =SUM(G2:G1000)
in H1 you'd have =SUM(H2:H1000)
....
in AD1 you'd have =SUM(AD2:AD1000)

Related

VBA code to select certain value in SAP GUI then replace with new value

I am needing assistance with the proper code for an SAP GUI script. I am needing to select a certain value "T09k.42A" and replace with "T09k.59E". The value is not always on the same line, which is where I am hitting a roadblock.
code fo far SAP example SAP example
Replace the Row value with a variable to cycle through all rows and find the value you are looking to change. With this in mind, you may have to use the description row and look for a blank to end the loop.
session.findbyid("wnd[0]/usr/tblSAPLCPDITCTRL_3400/ctxtPLPOD-ARBPL[2,2]")
is the same as
session.findbyid("wnd[0]/usr/tblSAPLCPDITCTRL_3400/ctxtPLPOD-ARBPL[2," & "2" & "]")
replace the second number with a variable
session.findbyid("wnd[0]/usr/tblSAPLCPDITCTRL_3400/ctxtPLPOD-ARBPL[2," & j & "]")
Now you can loop through to find the value you want to change
For j = 0 to 6 '7 visible rows in your picture
If session.findbyid("wnd[0]/usr/tblSAPLCPDITCTRL_3400/ctxtPLPOD-ARBPL[2," & j & "]").text = "T09k.42A" then
session.findbyid("wnd[0]/usr/tblSAPLCPDITCTRL_3400/ctxtPLPOD-ARBPL[2," & j & "]").text = "T09k.59E"
session.findbyid("wnd[0]/usr/tblSAPLCPDITCTRL_3400/ctxtPLPOD-ARBPL[2," & j & "]").setFocus
session.findById("wnd[0]").sendVKey 0
End if

Concatenate 2 Columns in VBA

I have 2 columns (column F and G), let's say they both go up to row 10. In row S, I want a concatenate statement: F(G)
I've tried:
SHEET.range("S2:S2" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"
and
SHEET.column(18).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"
The problem with either is that after the 10th row, the formula keeps running such that every cell goes "()". How do I edit the macro to end when there's no more data in column F and G?
with:
range("S2:S2" & LastRow)
if LastRow is 10 then the string will be S2:S210 as you have the 2 in the second reference. You want to remove that:
range("S2:S" & LastRow)
So:
SHEET.range("S2:S" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"

How can i copy text data in a cell to another cell dependent upon a specific text from the same row?

In my excel sheet I have 5000 rows in 5 columns, A,B,C,D,E.
column A is a 6 digit build number which varies, columns B,C,D,E have a drop down box, which you can select either complete or shortage.
what I am trying to achieve is to copy the specific text(build number) from column A in the various cell to cell H4,I5,J6 etc only when shortages is selected in columns B,C,D,E. and removed from those cells when de-selected from shortage to complete.
see example
Trim Shop Shortages
Seat Build Sequence Log Build Number 246125 123456
Build Number Drivers Passengers Drivers Passengers
246125 COMPLETE COMPLETE COMPLETE SHORTAGE
245874 COMPLETE COMPLETE COMPLETE COMPLETE
123456 COMPLETE SHORTAGE COMPLETE COMPLETE
The formula for a conditional copy is straightforward:
=IF(B4="SHORTAGE";$A4;"")
and gives the following result:
But it seems to me you have some special requirement about the copy destination (H4, I5, J6) which is not totally clear to me.
Could you better clarify the cell destination criteria?
Sub copyvalues()
For i = 1000
If Range("B" & i).Value = Shortage or Range("C" & i).Value = Shortage_
or Range("D" & i).Value = Shortage or Range("E" & i).Value = Shortage _
Then Cell('Enter destination here, by using a formula for i').Value = ("A" & i)
End If
Next i
End Sub`
If you can use 4 more columns to the right of your existing ones the you can input this formula :
=IF(B5="COMPLETE", "COMPLETE", $A5)
This assumes your data starts in cell A5 and this formula should be pasted into cell F5, dragged across to I5 and then copied down for however many rows you need.

Need to put multiple values from VBA to cells using loop/for

Referring to a previos question that now has turned to something more advanced (Insert an advanced formula into a cell);
I have created a list using Me.TerminalID1 (through 14).List to populate 14 dropdown lists in my UserForm where reach drowndown is named cboTerminalID1 through 14.
I want to use a loop function to add the content from cboTerminalID1 through 14 to populate cells inside Excel, C4 through C17.
I've tried this code; but it obvious not work;
For TIDtoCell = 4 To 17 Step 1
Sheets("Rapport SNN").[C & TIDtoCell & ].FormulaLocal = "=cboTerminalID" & TIDtoCell & ""
Next TIDtoCell
The idea is that it should be;
Sheets("Rapport SNN").[C4].FormulaLocal =cboTerminalID4
Any ideas ? So hopeless to be a newbie...
Assuming the comboboxes contain formulas that you wish to write to the worksheet:
For TIDtoCell = 4 To 17
Sheets("Rapport SNN").Cells(TIDtoCell, 3).FormulaLocal = "=" & Me.Controls("cboTerminalID" & TIDtoCell - 3)
Next
Note: this also assumes that the formulas, as stored in the comboboxes, do not include the initial equal sign.
Note: this assumes this code will be placed into the userform code module.
If any of these assumptions are incorrect, I am happy to alter the answer. Just let me know.
UPDATE
Based on new information that the values in the comboboxes are not formulas, this is the solution:
For TIDtoCell = 4 To 17
Sheets("Rapport SNN").Cells(TIDtoCell, 3) = Me.Controls("cboTerminalID" & TIDtoCell - 3).Value
Next
For TIDtoCell = 4 To 17 Step 1
Sheets("Rapport SNN").["C" & TIDtoCell].FormulaLocal = "=cboTerminalID" & TIDtoCell & ""
Next TIDtoCell

Using .formula to sum values in dynamic ranges

I created a macro that changes the layout of a raw excel file and displays some usefull information to the user. But I am stuck now.
I want to display the sum of some cells in a column, but the cell where the sum is and the column itself is set dynamically.
My layout looks like this:
Year1 Year2 Year3
Sum1 100
Sum2 50 48
Sum3 72 81
Sum4 26
------------------------------------------
Total
I want to display the sum of (sum1, sum2, sum3, sum4) in the Total row under each Year.
The thing is my years are set dynamically depending on the value of a parameter (i.e. I can have 2, 3 or 5 years).
I know I can do it using Range.WorksheetFunction.Sum but the problem is my values are subject to some changes after the macro has been used and if I use this function the values won't change afterward.
That's why I want to use the function Range.Formula to display these values as I can enter the sum like I did for the values already in the sheet.
But unlike these values (that are the sum of values in an unique column easy to select), I am not able to select the colum dynamically.
Here is what I tried to give you an idea of what I want to do if it is unclear to you:
For i=0 to Duration-1
Sheet2.Range("D" & RNumber + 7 + Duration).Offset(0,1+2*i).Formula="=SUM(Sheet2!" & Range("D" & RNumber + 6).Offset(0,1+2*i) & ":Sheet2!" & Range("D" & RNumber + 6 + Duration).Offset(0,1+2*i) & ")"
Next i
But it doesn't work as the sum doesn't recognize the range. I know I should have something like
"=SUM(Sheet2!E" & Firstrow & ":Sheet2!E" & Lastrow & ")"
but then i won't be able to select my colum dynamically.
It looks like your problem stems from the fact that you can't do things like "Sheet2!" & D+3+1 for columns the same way you can do "Sheet2!D" & 5+6+2 for rows. My opinion is that this is one of the many flaws created by the insane A1 reference style, and the best way to attack this problem at the root is to simply switch to R1C1 at the beginning of the macro.
With Application
If .ReferenceStyle = xlA1 Then
.ReferenceStyle = xlR1C1
End If
End With
Some day, I plan to start a non-profit effort to rid the world of the mis-begotten $A4:B$3 craziness altogether. But too many people are used to A1. And if you send your boss or co-worker a spreadsheet with numbers for columns, she might get upset or confused.
So the other solution is to introduce some ADDRESS() elements into your SUMs. The ADDRESS function takes only numbers (well, string for sheet reference) and will return a cell reference in whichever style you want.
For i=0 to Duration-1
Sheet2.Cells(RNumber+7+Duration, 5+2*i).Formula = _
"=SUM(INDIRECT(ADDRESS(" & RNumber + 6 & "," & 5+(2*i) & ",1,TRUE,""Sheet2""),1):" _
& "INDIRECT(ADDRESS(" & RNumber + 6 + Duration & "," 5+(2*i) & _
",1,TRUE,""Sheet2""),1))"
Next i
Or you can use Application.WorksheetFunction.Address() and avoid having the INDIRECT()s cluttering up your fomulas.