I have an Excel spreadsheet with data in two columns as Time and Temperature. The Time column has values such as follows, where there are some skipped seconds:
2017-10-17 14:18:15
2017-10-17 14:18:17
2017-10-17 14:18:18
2017-10-17 14:18:19
2017-10-17 14:18:21
2017-10-17 14:18:22
Is it possible to have Excel add in those missing times with a corresponding blank row (so then I can do interpolation on those blank spots?)
I am not sure how your data look like but this is what you can do if you don't want to use VBA.
Use this formula from cell B2 and drag down:
=IF(B1="","",IF(N(B1),IF(ROW(A2)-ROW($A$1)-1<SECOND(MAX($A$2:$A$7)-MIN($A$2:$A$7))+1,B1+TIME(0,0,1),""),A2))
What this does is to determine how many seconds between max and min and then use the rows as a reference (you can change this part based on your data structure). Try and see if this works for you or not.
Try this! It assumes your times are in column A, with a header in cell A1
Sub AddRows()
'Finds out what row to go up to
Dim lRow As Long
lRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lRow + 1
'If the difference between the cell below is more than a second, then add a
row above and skip the next i
If Cells(i + 1, 1) - Cells(i, 1) > 0.000011575 Then
Cells(i + 1, 1).Select
Selection.EntireRow.Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
lRow = lRow + 1
Cells(i + 1, 1).Select
Cells(i + 1, 1).Value = Cells(i, 1).Value + 0.000011575
i = i + 1
Else
End If
Next i
End Sub
Related
I have a large set of data that needs to be broken up based on group type in column 4. The goal of the loop is to insert three blank rows and copy-paste the sheet's header on top of the new group. This should continue until the bottom row row. However, it seems like my lastrow3 isn't updating within the loop to account for the new rows inserted. Is there anything I'm missing in re-designating the last row within this For-Next Loop? Thanks!
Dim lastrow3 As Integer, Dim b As Integer
Range("A1").End(xlDown).Select
lastrow3 = ActiveCell.Row
For b = 2 To lastrow3
If Cells(b, 4) <> Cells(b + 1, 4) Then
Rows(b + 1).Resize(3).EntireRow.Insert
Rows("1").Copy
Rows(b + 3).PasteSpecial xlPasteFormats
Rows(b + 3).PasteSpecial xlPasteValues
lastrow3 = lastrow3 + 3
b = b + 4
End If
Next
UPDATED. #garbb makes the right call in the comments. Your For loop is flawed in that you're setting it to end at a specific integer, however this number could change. Basically, you want the loop to continue until it reaches the cell that was was originally marked, taking into account the inserted rows. This can be accomplished by using a Do-While loop which evaluates the row number of that cell if it's set to a variable. As a variable, it will update as you insert rows which addresses your issue.
I tested this out and it should work. Make sure the b=b+1 is in the proper spot. I can't tell without seeing what you did.
Dim theEndRange As Range
Set theEndRange = Range("A1").End(xlDown)
Dim b As Long
b = 2
'set the looping rule here
'theEndRange will update as rows are inserted.
Do While b <= theEndRange.Row
If Cells(b, 4) <> Cells(b + 1, 4) Then
Rows(b + 1).Resize(3).EntireRow.Insert
Rows("1").Copy
Rows(b + 3).PasteSpecial xlPasteFormats
Rows(b + 3).PasteSpecial xlPasteValues
b = b + 4
Else
'without this your code will loop indefinitely!!
'if this is causing an issue, it may need to place outside if statement
b = b + 1
End If
'or here
'b = b+1
Loop
I have existing script that does a major chunk of what I need. The script (from here: https://www.extendoffice.com/documents/excel/4054-excel-duplicate-rows-based-on-cell-value.html) basically inserts and then copies rows of data X number of times, where X is one of the fields in the table. It works well and the referenced page shows examples of the start and end points.
But when I run the script in Excel I go from ~2,000 lines in my table to ~40,000 lines. I need to modify all the duplicated rows (incremental dates) and so I am now attemting to also include new data into the table while the script runs that will allow me to change data in the duplicated rows... for example I can use the duplicate number 1, 2, 3, 4 and some simple formulas to change dates relative to a start point.
I expect that I will need some additional code inserted into the routine that will add data into a nominated column and do the auto incrementing from 1.
Having zero actual VBA skillz, ive no idea how to tackle the second part of my problem with the code I already have. Any help would be totally awesome !!
Sub CopyData()
'Updateby Extendoffice 20160922
Dim xRow As Long
Dim VInSertNum As Variant
xRow = 1
Application.ScreenUpdating = False
Do While (Cells(xRow, "A") <> "")
VInSertNum = Cells(xRow, "D")
If ((VInSertNum > 1) And IsNumeric(VInSertNum)) Then
Range(Cells(xRow, "A"), Cells(xRow, "D")).Copy
Range(Cells(xRow + 1, "A"), Cells(xRow + VInSertNum - 1, "D")).Select
Selection.Insert Shift:=xlDown
xRow = xRow + VInSertNum - 1
End If
xRow = xRow + 1
Loop
Application.ScreenUpdating = False
End Sub
Try this code below, I used the same sample data on the link you provided. However on this code I created 2 worksheets, one for the raw data to be processed and one for the duplicate output including the increment of dates and duplicate number.
Sub duplicateData()
Dim rSH As Worksheet
Set rSH = ThisWorkbook.Sheets("RAW") 'Your raw data
Dim oSH As Worksheet
Set oSH = ThisWorkbook.Sheets("OUTPUT") 'Output data on another sheet
x = 2
For a = 2 To rSH.Range("A" & Rows.Count).End(xlUp).Row
For b = 1 To rSH.Cells(a, 4).Value '4 is the column of duplicate times
If b = 1 Then
For c = 1 To 4 'Number of your column
oSH.Cells(x, c).Value = rSH.Cells(a, c).Value
Next c
oSH.Cells(x, 5) = 1 'First instance, 5 is the column number of duplicate counter
Else
For c = 1 To 4 'Number of your column
oSH.Cells(x, c).Value = rSH.Cells(a, c).Value
Next c
oSH.Cells(x, 3).Value = CDate(oSH.Cells(x - 1, 3).Value) + 1 '3 is the column number of date to increment
oSH.Cells(x, 5).Value = CInt(oSH.Cells(x - 1, 5).Value) + 1 '5 is the column number of duplicate counter
End If
x = x + 1 'Increment Output row number
Next b
Next a
End Sub
English isn't my first language so I hope you will understand me
Beacuse I dont know anything about programming I am turning for help to you: Excel pros;)
I have a workbook with two sheets ( datafeed and record )
On "datafeed", column B, cells B2 to B400 I am capturing live prices from internet.
Currently I use the following :
Sub my_onTime()
Application.OnTime Now + TimeValue("00:00:01"), "my_Procedure"
End Sub
Sub my_Procedure()
With Sheets("record")
rw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Range(.Cells(rw, 1), .Cells(rw, 2)).Value = Sheets("datafeed").Range("a2:b2").Value
End With
ThisWorkbook.RefreshAll
my_onTime
End Sub
which currently records prices from "datafeed" to rows in "record".
Another problem is that records only one cell from "datafeed"; cell B2 . I dont know how to set it, so it would record all prices in range from cells B2:B400
My wish is that Excel would record price changes to"record" Sheet into columns not rows.
"Graphic" display what I want:
Sheet1 - "datafeed" ... B2 = 155............Sheet 2 - "record" cells : F2 = 155.....G2 = 150.....H2 - 145.....I2 - 140,....and so on
Sheet1 - "datafeed" ... B3 = 66.............Sheet 2 - "record" cells : F3 = 66.....G3 = 67....H3 - 66.....I3 - 65,....and so on
Sheet1 - "datafeed" ... B4 = 1015.............Sheet 2 - "record" cells : F4 = 1015.....G4 = 1025....H4 - 1035....I4 - 1045,....and so on
Also last recorded price must be put in first column Sheet 2 - "record"; cells: , F2,F3,F4.......(Right now last recorded value is put in last row )
Any help will be greatly appreciated.
Thank you!
p.s
I have added 3 pics for clarification
1. sheet datafeed
2. sheet record
3. what i wish for sheet record
replace this part of your procedure
With Sheets("record")
rw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Range(.Cells(rw, 1), .Cells(rw, 2)).Value = Sheets("datafeed").Range("a2:b2").Value
End With
with this
With Sheets("record")
rw = .Cells(.Rows.Count, 5).End(xlUp).Row + 1
.Cells(rw, 5).Value = Sheets("datafeed").Cells(2, 1).Value 'write name from column A
iColumn = 6 'set you want to start in column F with prices
For iRow = Sheets("datafeed").Cells(Sheets("datafeed").Rows.Count, 1).End(xlUp).Row To 2 Step -1
.Cells(rw, iColumn).Value = Sheets("datafeed").Cells(iRow, 2).Value
iColumn = iColumn + 1
Next iRow
End With
if you want the new line added to first line instead of the last
With Sheets("record")
iColumn = 6 'set you want to start in column F with prices
iRecordRow = 3 'set to what row number to put the current price
.Rows(iRecordRow & ":" & iRecordRow).Insert Shift:=xlDown
For iRow = Sheets("datafeed").Cells(Sheets("datafeed").Rows.Count, 1).End(xlUp).Row To 2 Step -1
.Cells(iRecordRow, iColumn).Value = Sheets("datafeed").Cells(iRow, 2).Value
iColumn = iColumn + 1
Next iRow
.Cells(iRecordRow, 5).Value = Sheets("datafeed").Cells(2, 1).Value 'write name from column A
End With
if you want it added to different row than 3 just change number in iRecordRow = 3
I'm trying to use this code for showing the overflow to another value in the column I in sheet 20170224-SUAGDB. But I'm getting an Error 1004 about the line If Works....Value Then. Can somebody help me with the error?
Dim i As Long
For i = 2 To Rows.Count
If Worksheets("20170224-SUAGDB").Cells(i, 9).Value <> Worksheets("20170224-SUAGDB").Cells((i + 1), 9).Value Then
Cells(i, 9).Font.Color = vbRed
End If
Next i
Since you haven't .select'ed any specific Range() in your Worksheet, your Rows.Count value is equal to 1048576.
You can check it's value using MsgBox Rows.Count.
So, when your loop goes from 1 to 1048576, in the last iteration when you try to compare .Cells(i, 9).Value with .Cells((i + 1), 9).Value, the second term "explodes" your total number of rows!
Your code looks for the row number 1048577 - which don't exists - so it throws a 1004 error.
One possible solutions is changing :
This: For i = 2 To Rows.Count
To this: For i = 2 To Rows.Count - 1
This solution will loop through all - 1 rows even if they don't contain any data.
Or maybe even:
This: For i = 2 To Rows.Count
To this: For i = 2 To Range("I:I").End(xlDown).Row - 1
This solution get the last non-blank row value of the selected column.
ps. If you don't add - 1 in For i = 2 To Range("I:I").End(xlDown).Row - 1, and your worksheet contains some data at the row number 1048576, your code will throw the same error 1004.
Some of your objects are not fully qualified. For instance, get the last row number in column "I" (9) in `Worksheets("20170224-SUAGDB").
Try the code below:
Dim i As Long
With Worksheets("20170224-SUAGDB")
For i = 2 To .Cells(.Rows.Count, 9).End(xlUp).Row '<-- get last row in column "I" (column 9)
If .Cells(i, 9).Value <> .Cells((i + 1), 9).Value Then
.Cells(i, 9).Font.Color = vbRed
End If
Next i
End With
I'm trying to sort out a manifest that needs to be converted to a different format.
Just wondering, is there a way for to find a group of blank cells and delete them, using VBA?
The files look like this:
name
address
address
region
-blank line-
-blank line-
-blank line-
Name
Address
Address
Region
-blank line etc
What I would like is something to delete out the grouped blank cells. The issue I have is sometimes one of the address columns is blank, (one blank line) so I can't just use Go to > Special...
What I would love is if I could work out how to have a VBA to run down the column, pick up a group of 3 or more blank cells on top of each other, and delete those entire rows.
Is that possible?
Cheers, Nigel.
You can always do something like this:
Sub DeleteBlanks()
Dim TotalRows As Long
Dim i As Long
Dim j As Long
TotalRows = Rows(Rows.Count).End(xlUp).Row
For i = TotalRows To 2 Step -1
'Check if there is more than one blank cell
If Cells(i, 1).Value = "" And Cells(i - 1, 1).Value = "" Then
'Count the number of cells that are blank
Do While Cells(i - j, 1).Value = ""
If i - j <= 1 Then
Exit Do
End If
j = j + 1
Loop
'Delete group of blank of cells
If i - j <= 1 Then
Rows(i & ":" & i - j).Delete
Else
Rows(i & ":" & i - j + 1).Delete
End If
j = 0
End If
Next
End Sub