Working on a project, I'm having a little issue on copying down values using a loop. i've tried to describe the issue down here as clearly as i can.
-What should be done, is that for each of the i's (1 to 83) and for the whole period (ie 1/1/2014-15/1/2014) values are copied below each other on Calculation sheet.
-What is happening so far, is that it copies down values but overwrites all days except for the first, for all i's except for the last.
Hence, the result after the macro is finished is: 1/1/2014 of i1, 1/1/2014 of i2 [...] till 1/1/2014-15/1/2014 for i83.
What should be the result is 1/1/2014-15/1/2014 for i1, 1/1/2014-15/1/2014 for i2 [...] till 1/1/2014-15/1/2014 for i83.
Sounds mysterious and unclear, but if here's the code which makes it hopefully more clear.
Sheets("Summary").Select
For i = 1 To 83
Sheets("Summary").Select
Sheets("Summary").Range("A10").Value = Sheets("Summary").Cells(i + 10, 1).Value
Sheets("Calculation").Select
Sheets("Calculation").Range(Cells(i + 17, 1), Cells(i + 31, 59)).Value = Sheets("Calculation").Range("a2:bg16").Value
Next i
Range("a1").Select
End Sub
I'm looking forward to your suggestions, if anything is not clear please let me know
Valentino
Question? Do you have observations of 83 item (in 83 columns) for each of the 15 dates (in rows?) or 83 rows of observations of the dates in 15 columns?
Worksheets("Summary").Range("A10:CF24").Copy Destination:=Worksheets("Calculation").Range("A10").Offset(17)
Related
So I have the following excel tabs:
Code 1, Code 2, Code 3, LI, 2015, 2016, 2017, 2018, 2019, output for 2015 etc.
For 2015, I have a table in '2015' tab with 10 rows a list of 3 code and their respective % values. e.g.
ref name yr code 1 % code 2 % code 3 %
12345 NAME 2015 AB 50% CD 37% EF 13%
78901 NAME 2015 AX 54% OD 30% NG 6%
26572 NAME 2015 AE 60% CD 27% PF 13%
I need the code 1 'AB' and % '50%' to be put into cells B5 and B6 in the tab Code 1. Same for codes 2 and 3 'CS' and '37%' in B5 and B6 in tab Code 2 etc. These then produce an pattern in the LI tab in cells F5:F183 which then needs copying for each reference and pasting into the output for 2015 tab for each reference. Then this loops for each reference and repeats pasting the output.
So far I have this for the copying and pasting part:
Sub Copy_and_paste2()
Dim rng2 As Range, cell2 As Range
Dim i As Integer
i = 3
Set rng2 = Worksheets("2015").Range("D10:D21")
For Each cell2 In rng2
Worksheets("Code 1").Range("B5").Value = cell2.Value
Worksheets("2015 output").Range("A" & i & ":AW" & i).Value = Worksheets("LI").Range("F5:F183").Value
i = i + 1
Next cell2
End Sub
At this point I was just trying to make it work for the first code with no % then I can try add the later ones but this one doesn't work either. Any advice?
Your question is a little hard to read so I've written an answer based on what I think is the most obvious issue with your code. Apologies if I've misread your query or missed the point entirely..
You appear to be trying to copy the contents of
Worksheets("LI").Range("F5:F183") (179 cells)
to
Worksheets("2015 output").Range("A" & i & ":AW" & i) (49 cells)
Ignoring the fact that you can't copy 179 entries into 49 (it will only copy the first 49) - I think you are probably seeing the first entry repeated across all 49 cells?
This would be because you're not transposing the range (in this case, switching from a column to a row).
Try something like this to Transpose:
Worksheets("2015 output").Range("A" & i & ":AW" & i).Value = _
WorksheetFunction.Transpose(Worksheets("LI").Range("F5:F57").Value)
Keep in mind though, I've changed the source range to just the first 49 cells. You don't have to but I'm highlighting it in case you weren't aware this is happening.
I have a list of numbers (that will be changed weekly) and I also have a list of ranges (that also change weekly). I need to check whether each number falls between each range.
Eg. My list of numbers on the left and my list of ranges on the right.
4 1 3
10 67 99
54 120 122
155
So what I need is to return a value if 4 falls between 1-3, then check if it falls between 67-99 and so on. Then return a value if 10 falls between 1-3 or 67-99 etc.
I have tried array and vba but I'm noob and I cant find much in the way of examples for this issue. I have had success with the following nested if;
=IF(OR(AND(G2>$L$2,G2<$M$2),AND(G2>$L$3,G2<$M$3),AND(G2>$L$4,G2<$M$4),G2,"")
=IF(OR(AND(G3>$L$2,G3<$M$2),AND(G3>$L$3,G3<$M$3),AND(G3>$L$4,G3<$M$4),G3,"")
However, once my number of ranges gets above a certain number it says i have too many characters.
Any help would be appreciated.
Regards,
Will.
So what I need is to return a value if 4 falls between 1-3, then check if it falls between 67-99 and so on. Then return a value if 10 falls between 1-3 or 67-99 etc.
If you want to match each value in col G with the list in L and M then, rearrange the G column values in a row as shown below so that you can get the entire view in one go.
Put this formula =(AND($N$1>L2,$N$1<M2)) in N2 and drag it down. Similarly put the formula =(AND($O$1>L2,$O$1<M2)) in O2 and pull it down and so on...
Thanks Siddarth. This works but the number of rows will be changing weekly and your method would be too tedious to do weekly.
Ended up working it out on my own. Pretty simple but took me forever!
Sub Subtract_Start()
Set rng1 = Range(Range("G2"), Range("G2").End(xlDown))
Set rng2 = Range(Range("L2"), Range("L2").End(xlDown))
For i = 2 To rng1.Rows.count
For j = 2 To rng2.Rows.count
If Cells(i, "G").Value > Cells(j, "L").Value Then
If Cells(i, "G").Value < Cells(j, "M").Value Then
Cells(i, "G").Copy Cells(i, "J")
End If
End If
Next j
Next i
End Sub
I am really struggling with the simple task to write a Code to fill missing data in a measurement file. I've never coded before so it is quite difficult.
Problem description:
I have uploaded a picture with an example of the problem.
The source format is a .csv file with two columns, a timestamp (hh:mm:ss) and a value for each timestamp.
I created the row 'time value' in excel, which is displaying the timestamp as a number. (minute 1 = 1/1440 to minute 59 = 1439/1440).
In column D, I put the difference between two timesteps, which should be around 0.007 for a 1-minute step.
However, the problem is that some minutes are missing in the data throughout the column, sometimes only one minute and sometimes multiple.
Required Code
So what I need would be a Matlab Code, for instance, that would go through the timestamps or time values and identify missing minutes and write the values of its previous minute.
For example. Minute 6 missing? --> Write row for minute 6 and give it the value of minute 5.
I hope my problem is clear, otherwise, I am happy to explain more details.
--
Cheers
Alison :)
PS: The multiple files each have hundreds of thousands of data. That's why I would need an automated code :)
PICTURE:
I think you can do stuff like this :
Sub test()
lastrow = Worksheets("Inactive").Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To lastrow
If Format(TimeValue(CDate(Cells(i, 1).Value)) - TimeValue(CDate(Cells(i - 1, 1).Value)), "hh:mm:ss") > "00:01:00" Then
Rows(i).Insert
Cells(i, 1).Value = Format(TimeValue(CDate(Cells(i - 1, 1).Value) + "00:01:00"), "hh:mm:ss")
Cells(i, 2).Value = Cells(i - 1, 2).Value
End If
Next
End Sub
before
Output AFTER
I've devellopped a quick little code to help me sort a large amount of data on an excel tool, it works perfectly except for one tiny detail ruining all of my work so far.
My table is 500+ columns wide and my algorithm would like to copy and paste two rows at a time (I use fused cells and do not wish to use an alternative to that).
My algorithm then tries to do :
Rows(i & ":" & i + 1).Cut
Rows(j & ":" & j + 1).Select
Selection.Insert shift:=xlDown
i and j being the row index (I try to put row j and j+1 just below row i and i+1
Which tells me that excel cannot do with the current ressources.
I tried to reduce the amount of cells with something like
range(cells(i,bc),cells(i+1,ec)).cut
range(cells(j,bc),cells(j+1,ec)).select
Selection.Insert shift:=xlDown
bc being the first column of my selection and ec the last
But still I have the same problem (selection is between 500 and 510 colums so around 1k cells)
So here is the question :
Is there a way to bypass that error and force the cut/paste? (I would like to avoid changing x columns at a time, slowing a process already quite slow)
Or a way to change the index of the row or something alike that I do not know about ?
Thank you for your time and future answers.
PEagle
This is not the answer, just too long to put as a comment.
I just ran the code below (with 700 columns of data per row), and it Cut >> Paste just fine without errors.
Option Explicit
Sub CopyFullTwoRows()
Dim i As Long, j As Long
' just for simulation
i = 2
j = 10
Rows(i & ":" & i + 1).Cut
Rows(j & ":" & j + 1).Insert Shift:=xlDown
End Sub
Sorry i am not yet at comment privilegies, so here is the answer to your comments/answers:
My file is about 6300KB.
For other programs, generaly just the outlook and firefox but computers at work are not very powerful.
Regarding the flatening of my merged cells and then sorting, the problem is then that I need to do sorting but only on even lines.
As it is a file from work I cannot send it on the internet, but to give you an idea, I have 2 lines working together, columns B to F are merged (B8+B9, B10+B11 etc, then C8+C9 etc.) and then I have two rows of data for each of these lines.
Unmerging would cause blank cells to pop and then sorting would mess the whole table.
I am sorry, my question is now obsolete.
I reorked the file, noticing that the file, for a reason I do not know, included all lines until the end of the sheet (1+ million). I have removed the unnecessary lines (recreated the file) and it now works fine.
sorry for your time loss, thank you for helping me.
See you soon.
All, I asked a question "Excel VBA: Sort, then Copy and Paste" and received two excellent answers. However, because I failed to provide sufficient user requirements, they won't work: I asked for a fix to the existing solution I created, instead of specifying the actual business need and seeing if anyone has a better way.
(sigh) Here goes:
My boss asked me to create a ss to log issues. He wants a compound ID that concatenates the "Assigned Date" with a number that indicates what number issue it is for that day only. A new day, the count must restart at 1. E.g.:
Assigned Issue Concatenated
Date & Count = ID
5/11/2011 & 1 = 5112011-1
5/11/2011 & 2 = 5112011-2
5/11/2011 & 3 = 5112011-3
5/12/2011 & 1 = 5122011-1
I solved this with a hidden column C that calculates =IF(D2<>D1,1,C1+1), thus calculating the Issue Count by incrementing the previous issue count if the assigned date in column D is the same as the previous date, and starting over at 1 when the date changes. Another column concatenates the assigned date and the issue count, and I have my issue ID.
Quick, easy, elegant, in, out, and done. Right? But when I delivered the ss, he pointed out that if you (that is, he) sorts any part of the spreadsheet, the issue ID goes out of sequence. Of course---each formula isn't referencing the previous date in sequence if the rows are sorted out of Assigned Date order.
My immediate thought, which prompted my previous question, was to first re-sort the Assigned Date order correctly, then copy and paste the value of the calculated Issue Count to lock it in, and thus preserve the concatenated ID.
The only other way I can see to do this (in VBA, natch) is to:
evaluate all the dates in the Assigned Date column
evaluate all the numbers in the Issue Count column
calculate the latest sequential Issue Count for an a new item assigned on a given Assigned Date
Assign that sequential Issue Count to the new item
It'd be nice to then place the cursor into the next cell that the user would ordinarily go to, which would be the one right adjacent to the just-entered Assigned Date; however, that isn't necessary
That would avoid the need to re-sort the physical ss. However, besides a hazy guess that this would involve VLOOKUP, I got nothing. I couldn't find anything through searching.
Can anyone help? Or suggest a place to go? Thanks!!!
Sounds like you just want to automate a Paste Special action. The following replaces the formulas in a1:a100 with their calculated values:
Set src = ActiveSheet.Range("a1:a100")
src.Copy
src.Select
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
I think the formula =IF(D2<>D1,1,C1+1) could be improved as this relies on dates being in order. The following will preserve the count for any order that is sorted
Assume
ColA ColB ColC
Row1 Assigned_Date Issue Count Concatenate
Row2 05/11/2011 =COUNTIF($A$1:A2,A2) =TEXT(A2,"ddmmyyyy")&"-"&B2
Row3 05/11/2011 =COUNTIF($A$1:A3,A3) =TEXT(A3,"ddmmyyyy")&"-"&B3
Row4 05/12/2011 =COUNTIF($A$1:A4,A4) =TEXT(A4,"ddmmyyyy")&"-"&B4
Row5 05/11/2011 =COUNTIF($A$1:A5,A5) =TEXT(A5,"ddmmyyyy")&"-"&B5
Essentially enter B2 and C2 formulae and drag down. You might need to swap ddmmyyyy to mmddyyyy as we use dates first rather than months :)
Also, note the locking of the first part of the range only using $ - $A$1:Ax
This works perfectly for your current question but does not work if the Issue Count is assigned in time order per date.
How about using a procedure? Just click a button to add the next entry.
I've assumed that the entries will be given today's date and that the sheet layout is:
Rows: 1 = Title / 2 = left blank / 3 = Headings of the data block
Columns: A = Date / B = Issue Count / C = Combined ID / D etc = other data
Sub AddEntry()
Dim iDayRef As Long, iNumRows As Long, n As Long
With Range("A3")
iNumRows = .CurrentRegion.Rows.Count
For n = 2 To iNumRows
If .Cells(n, 1).Value = Date Then
If .Cells(n, 2).Value > iDayRef Then iDayRef = .Cells(n, 2).Value
End If
Next
.Cells(iNumRows + 1, 1).Value = Date
.Cells(iNumRows + 1, 2).Value = iDayRef + 1
.Cells(iNumRows + 1, 3).Value = Format(Date, "mm/dd/yyyy") & " - " & iDayRef + 1
.Cells(iNumRows + 1, 4).Select
End With
End Sub
And do you really need three columns for Date, Count, and Combined ID? If you went with a
yyyy/mm/dd - xx
ID format, one column could replace all three, and you could easily sort on it.