I want to create a loop for the following code . The only thing i need the to loop to do is move to the next column (which would be column F). the rows will stay the same. any suggestions?
xlSheet.Range("e23") = (Me.overrings)
xlSheet.Range("e33") = (Me.overingTax)
xlSheet.Range("e27") = (Me.Sampling)
xlSheet.Range("e28") = (Me.Waste)
xlSheet.Range("e29") = (Me.Promo)
xlSheet.Range("e42") = (Me.Online)
xlSheet.Range("e34") = (Me.freebieTax)
xlSheet.Range("e49") = (Me.totalDepo)
xlSheet.Range("e38") = (Me.CreditCards)
If you really want the same value in the adjacent cell, you can modify your existing code like this (no loop required):
xlSheet.Range("e23:f23") = (Me.overrings)
For x= 5 to 6
With xlSheet.columns(x)
.cells(23).value=Me.overrings
'etc
End with
Next x
Related
I need some help with vba code. I'm self-lerning so please be understanding for simply cases ;)
I'm trying to create macro which will be looking for some records/cells in one workbook (FileA) (determined by 3 conditions) and then paste values to another workbook (FileB) and after that find in another tab in FileB some values where condition will be pasted value to match them with looking value (I belivie it could be done somehow with Vlookup but I get stuck).
Below problematic for me part of code (I'm working on some files found in work, no one use it now).
First issue is with Set Update, I don't know why it takes it as "Nothing"... conditions are true - I mean "pp1" is existing in column A in FileA.
Second issue shows when I change i start range to some "later" numbers, eg From i = 2280, macro is ignoring last line where should assign some values (again shows update2 as "nothing") but value if pp2 is existing in W column in tab data...
Dim name As String
name = "[path to file on sharepoint]"
Application.ScreenUpdating = False
Workbooks.Open Filename:=name
a = 1
For i = 2263 To 14000
If Workbooks("FileA").Sheets("Main").Cells(i, 11) = "CANCEL" And Workbooks("FileA").Sheets("Main").Cells(i, 6) = "DENIS" And Workbooks("FileA").Sheets("Main").Cells(i, 5) > 1301358454 Then
pp1 = Workbooks("FileA").Sheets("Main").Cells(i, 1)
If pp1 > 0 Then
Set Update = Workbooks("FileA").Worksheets("Main").Range("A:A").Find(pp1, lookat:=xlPart)
If Update > 0 Then
Update = Update.Row
Workbooks("FileB").Worksheets("lost").Cells(a, 1).Value = Workbooks("FileA").Worksheets("Main").Cells(Update, 5)
pp2 = Workbooks("FileB").Worksheets("lost").Cells(a, 1)
update2 = Workbooks("FileB").Worksheets("data").Range("W:W").Find(pp2, lookat:=xlPart).Row
Workbooks("FileB").Worksheets("lost").Cells(a, 5) = Workbooks("FileB").Worksheets("data").Cells(update2, 43)
I have been trying to get Excel to apply a formula over a set of columns and then extend the pattern across the entire set of rows.
This has led to the following code:
For i = 0 To avgsheetNames.Count - 1
If Contains(CStr(avgsheetNames(i)), "Scores") = True Then
With mainWorkBook.Worksheets(avgsheetNames(i))
strFormulas(1) = "=SUM(Aggregated_Internal_Scores!I2:I7)/6"
strFormulas(2) = "=SUM(Aggregated_Internal_Scores!J2:J7)/6"
strFormulas(3) = "=SUM(Aggregated_Internal_Scores!K2:K7)/6"
strFormulas(4) = "=SUM(Aggregated_Internal_Scores!L2:L7)/6"
strFormulas(5) = "=SUM(Aggregated_Internal_Scores!M2:M7)/6"
strFormulas(6) = "=SUM(Aggregated_Internal_Scores!N2:N7)/6"
strFormulas2(1) = "=SUM(Aggregated_Internal_Scores!I8:I13)/6"
strFormulas2(2) = "=SUM(Aggregated_Internal_Scores!J8:J13)/6"
strFormulas2(3) = "=SUM(Aggregated_Internal_Scores!K8:K13)/6"
strFormulas2(4) = "=SUM(Aggregated_Internal_Scores!L8:L13)/6"
strFormulas2(5) = "=SUM(Aggregated_Internal_Scores!M8:M13)/6"
strFormulas2(6) = "=SUM(Aggregated_Internal_Scores!N8:N13)/6"
mainWorkBook.Worksheets(avgsheetNames(i)).Range("C2:H2").Formula = strFormulas
mainWorkBook.Worksheets(avgsheetNames(i)).Range("C3:H3").Formula = strFormulas2
mainWorkBook.Worksheets(avgsheetNames(i)).Range("C2:H3").AutoFill Destination:=mainWorkBook.Worksheets(avgsheetNames(i)).Range("C2:H32")
End With
End If
As you can see I have tried to provide the pattern I am going for where the values extracted from the "Aggregated_Internal_Scores" sheet should follow the pattern I2:I7 > I8:I13 > I14:I19 and so on.
However, when the macro has been executed what I get is I2:I7 > I8:I13 > I4:I9 > I10:I15?
It seems Excel is taking the block C2:H3 as the pattern and just incrementing by 2 at the start of every block.
Can you anyone explain where I have gone wrong and how I can specify that I want the extraction of sheet values to follow a certain pattern?
Thank you in advance!
Use:
mainWorkBook.Worksheets(avgsheetNames(i)).Range("C2:H32").Formula = "=SUM(INDEX(Aggregated_Internal_Scores!I:I,(ROW($ZZ1)-1)*6+2):INDEX(Aggregated_Internal_Scores!I:I,(ROW($ZZ1)-1)*6+7))/6"
Replace everything inside the If with that.
If one has Office 365 with dynamic array formula then use:
mainWorkBook.Worksheets(avgsheetNames(i)).Range("C2:H32").Formula2 = "=SUM(INDEX(Aggregated_Internal_Scores!I:I,SEQUENCE(6,,(ROW($ZZ1)-1)*6+2))/6"
I am trying to pull CustomDocumentProperties from a Word Document (that I select using Application.GetOpenFilename) to an Excel Sheet.
I can get the to run code using the number of the item:
Set ExcelRange = Range("DataFields")
For r = 1 To ExcelRange.Rows.Count Step 1
ExcelWorkbook.Sheets("Sheet1").Cells(r, 3) = WordDoc.CustomDocumentProperties(r).Value
Next r
If I hard code the name of the custom property, it also works:
Set ExcelRange = Range("DataFields")
For r = 1 To ExcelRange.Rows.Count Step 1
ExcelWorkbook.Sheets("Sheet1").Cells(r, 3) = WordDoc.CustomDocumentProperties("Subject Name").Value
Next r
Obviously in this case, it returns the "Subject Name" 7 times).
I don't want to return all the values - only specific ones based on the values of a named range (in this case DataFields).
DataFields references A1:A7. I would like to take those cells(that contain the names of the custom properties) and paste the value of the corresponding custom properties in C1:C7
However, I can seem to get the code to return values in C1:C7 based on the values in A1:A7.
Based on my (limited) knowledge, I thought that the following would return the desired results, but it's not working for me:
Set ExcelRange = Range("DataFields")
For r = 1 To ExcelRange.Rows.Count Step 1
ExcelWorkbook.Sheets("Sheet1").Cells(r, 3) = WordDoc.CustomDocumentProperties(ExcelRange(r, 1))
Next r
Any help would be appreciated.
Thanks!
I'm quite new to Matlab and I'm struggling trying to figure out how to properly preprocess my data in order to make some calculations with it.
I have an Excel table with financial log returns of many companies such that every row is a day and every column is a company:
I imported everything correctly into Matlab like this:
Now I have to create what's caled "rolling windows". To do this I use the following code:
function [ROLLING_WINDOWS] = setup_returns(RETURNS)
bandwidth = 262;
[rows, columns] = size(RETURNS);
limit_rows = rows - bandwidth;
for i = 1:limit_rows
ROLLING_WINDOWS(i).SYS = RETURNS(i:bandwidth+i-1,1);
end
end
Well if I run this code for the first column of returns everything works fine... but my aim is to produce the same thing for every column of log returns. So basically I have to add a second for loop... but what I don't get is which syntax I need to use in order to make that ".SYS" dynamic and based on my array of string cells containing company names so that...
ROLLING_WINDOWS(i)."S&P 500" = RETURNS(i:bandwidth+i-1,1);
ROLLING_WINDOWS(i)."AIG" = RETURNS(i:bandwidth+i-1,2);
and so on...
Thanks for your help guys!
EDIT: working function
function [ROLLING_WINDOWS] = setup_returns(COMPANIES, RETURNS)
bandwidth = 262;
[rows, columns] = size(RETURNS);
limit_rows = rows - bandwidth;
for i = 1:limit_rows
offset = bandwidth + i - 1;
for j = 1:columns
ROLLING_WINDOWS(i).(COMPANIES{j}) = RETURNS(i:offset, j);
end
end
end
Ok everything is perfect... just one question... matlab intellissense tells me "ROLLING_WINDOWS appears to change size on every loop iteration bla bla bla consider preallocating"... how can I perform this?
You're almost there. Use dynamic field names by building strings for fields. Your fields are in a cell array called COMPANIES and so:
function [ROLLING_WINDOWS] = setup_returns(COMPANIES, RETURNS)
bandwidth = 262;
[rows, columns] = size(RETURNS);
limit_rows = rows - bandwidth;
%// Preallocate to remove warnings
ROLLING_WINDOWS = repmat(struct(), limit_rows, 1);
for i = 1:limit_rows
offset = bandwidth + i - 1;
for j = 1:columns
%// Dynamic field name referencing
ROLLING_WINDOWS(i).(COMPANIES{j}) = RETURNS(i:offset, j);
end
end
end
Here's a great article by Loren Shure from MathWorks if you want to learn more: http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/ ... but basically, if you have a string and you want to use this string to create a field, you would do:
str = '...';
s.(str) = ...;
s is your structure and str is the string you want to name your field.
I need to export data to excel sheet with a specific name using Activex. Here is what I did: First i export the data to excel in first for loop and then modify the names of the sheet using second for loop. Can it be done even in one single for loop? I think there should be another better idea.
Note: The size of the data varies.
try
filename = fullfile(pwd,'example.xlsx');
for i=1:5
xlswrite(filename,[1 2;3 4]*i,i);
end
for i = 1:5
myExcel = actxserver('Excel.Application');
excelWorkBook = myExcel.Workbooks.Open(filename,0,false);
excelWorkBook.Worksheets.Item(i).Name = ['new_sheet_' num2str(i)];
excelWorkBook.Save;
excelWorkBook.Close;
myExcel.Quit;
end
catch
% Disp Error message.....
end
The xlswrite function also accepts a string as sheet parameter. So just replace your call by this one:
xlswrite(filename,[1 2;3 4]*i,['new_sheet_' num2str(i)]);
Fratyx's answer is the simplest to implement, but a bit of knowledge of the Excel Object Model goes a long way, so here's how to implement your solution with ActiveX in one loop:
e = actxserver('excel.application');
w = e.Application.Workbooks.Add();
numnewsheets = 4;
for i = 1:numnewsheets
w.Worksheets.Add([],w.Sheets.Item(w.Sheets.Count),1);
w.Worksheets.Item(i).Name = ['new_sheet_' num2str(i)];
w.Worksheets.Item(i).Cells.Range('A1:B2').Value = [1 2;3 4];
end
w.SaveAs(filename)
w.Close
e.Quit
e.delete
To calculate the range required for the size of your data, you will have to open file "xlswrite" and copy out the sub-function "calcrange".