What to do with error 1004 in loop through column? - excel

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

Related

Copy and Pasting a row based on a condition

Copy and Pasting an entire row based off of two conditions
For a school project, I am trying to find all the rows that satisfy both (of two) conditions then paste the entire row at the end of my data set. I am trying to do this with variables as the spreadsheet may change in tests that my professor will perform. I keep getting a "Subscript out of range" error. My reading and assigning to P and T, for loop, if statements, and count functions all work.
numrow = Rows(Rows.Count).End(xlUp).row
numcolumn = Columns(Columns.Count).End(xlUp).Column
P = Range(Cells(3, 1), Cells(numrow, 1)).Value
T = Range(Cells(3, 2), Cells(numrow, 2)).Value
For i = LBound(P, 1) To UBound(P, 1)
If P(i, 1) = 5 And T(i, 1) = 100 Then
countrow = countrow + 1 'check: return is 25
'Range(i, numcolumn).copy Sheets("Sheet1").End(xlUp).Offset(1, 0)
Worksheets("Sheet1").Range(Cells(i, numcolumn)).Value.copy
lastrow = ActiveSheet.Cells(1, 1).CurrentRegion.Rows.Count + 1
Range(Cells(lastrow, 1)).PasteSpecial xlPasteFormulasAndNumberFormats
Application.CutCopyMode = False
'I would also like to change all the cells that were just pasted in the first column to the value of 2.5 here, but I have no idea where to start with that
End If
Next i
As you can see I tried it two ways
1.)
Range(i, numcolumn).copy Sheets("Sheet1").End(xlUp).Offset(1, 0)
(which is commented for now)
2.)
Worksheets("Sheet1").Range(Cells(i, numcolumn)).Value.copy
both get highlighted when I try to debug and have the "subscript out of range" error

Excel - Record cell values, transpose them into columns and copy dynamically changing cells

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

How to add missing times in column in Excel for interpolation

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

How to get the value of another cell if the only addition to the referenced cell is "-LA" or "-LED'

So I have a full sheet of item numbers that need to be updated for quantities on a daily basis. There are many items that contain two accessories as different item numbers such as what is shown below:
1114
1114-LA
15894/3
1114-LED
What I need to happen is for the value(quantity) of the two numbers with the -LA and -LED to equal the original item number's (ex. 1114) quantity. Some items have no other option and some only have one or the other, and some have both.
Any Ideas on how I could do this? The spreadsheet is several thousand numbers long.
here is what it looks like:
Item number Quantity
1114 5
1114-LA 0
1114-LED 0
225896/3 2
225896-LED 0
114895 8
114895-LED 91
114895-LA 0
115938/2 91
115938/2-LED 0
edited after OP's data pattern change
assuming, as per your example, that:
unknown accessories per item number
accessories follow immediately after item number
quantities are in column adjacent item numbers one
you can try this:
Sub main()
Dim iRow As Long
iRow = 1
Do While iRow < Cells(Rows.Count, 1).End(xlUp).Row '<--| change '1' to your actual columnn with item numbers index
If InStr(Cells(iRow, 1).Value, "-") = 0 Then
Do While InStr(Cells(iRow + 1, 1).Value, "-") > 0
Cells(iRow + 1, 2).Value = Cells(iRow, 2).Value
iRow = iRow + 1
Loop
Else
iRow = iRow + 1
End If
Loop
End Sub
while an alternative Autofilter() + "Formula" approach is the following:
Sub main2()
With Range("B1", Cells(Rows.Count, 1).End(xlUp)) '<--| assuming data are in range "A1:Bx" with row 1 headers
.AutoFilter field:=1, Criteria1:="*-*"
With .Resize(.Rows.Count - 1, 1).Offset(1, 1)
.SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=R[-1]C"
.Parent.AutoFilterMode = False
.Value = .Value
End With
End With
End Sub
where row 1 must be a "header" row

Filling Array once worked, does not anymore (subscript out of range)

The code I have once worked to add/delete groups of rows (requirements). I needed to modify the code so that if the 1st row of the group met certain criteria (i.e, the requirement was not one we wanted to consider), (1) we would not count it and (2), we would hide the group (current and subsequent 2 rows). This all works fine.
The problem is that now that I incorporated these changes, I get an error in another section of the code and for the life of me I cannot figure out why. I have stepped through this and am extremely frustrated. I am reaching out for help, and am hoping someone can see my error(!)
We calculate the start and finish row numbers within a grouping, and store these calculations in Arrays called "Start" and "Finish." I use the ReDim statement to initialize my arrays, because I thought that could be part of the problem, but no.
Any insight as to why my "subscripts are out of range" would be appreciated. I have traced through the logic, investigated this error, and read about the syntax/usage of VBA arrays. I don't know what else to do. Thanks in advance. Here are the relevant lines:
Sub Button1_Click()
Cells.Select
Selection.ClearOutline
If Cells.EntireRow.Hidden Then Cells.EntireRow.Hidden = False
Dim Start() As Integer
Dim Finish() As Integer
Dim p As Integer, q As Integer
ReDim Start(0, 50)
ReDim Finish(0, 50)
The following is embedded in logic that loops through all the rows in the spreadsheet:
i = 1
For Row = 4 To Cells(1, 6).Value - 1
If Begin Then
If Cells(Row, 3).Interior.ColorIndex = 44 Then
Start(i) = Row + 1
j = Cells(Row, 2).Value
Begin = False
End If
Else
If Cells(Row, 2).Value = j + 1 Or Cells(Row, 2).Interior.ColorIndex = 37 Then
Finish(i) = Row - 1
Begin = True
i = i + 1
Row = Row - 1
End If
End If
Next
The block I changed is as follows (code I added is last block where I attempt to hide rows). It precedes the previous. I am wondering how my change could have affect the above(?!)
If Cells(Row, 5).Value = "Requirement" Then
Range(Cells(Row, 4), Cells(Row, 4)).Interior.ColorIndex = 40
Rows(Row).Font.Bold = True
Rows(Row).Font.Italic = False
Rows(Row).Font.ColorIndex = 1 'Black
If Cells(Row - 3, 4).Value = "" Then 'this is requirement #1
Cells(Row, 4).Value = 1
Else
Cells(Row, 4).Value = Cells(Row - 3, 4).Value + 1
End If
p = Row
q = p + 2
Rows(p & ":" & q).Select
If Cells(p, 19).Value = "4" Then
Selection.EntireRow.Hidden = True
Else
Selection.EntireRow.Hidden = False
End If
Redim Start(0,50) makes the array dimensions 0 to 0, 0 to 50 i.e. a 2d array.
This means that when you call the array you need to provide parameters for both dimensions I.E: Start(0,i) or Finish(0,i). Calling Start(i) will result in the error you mentioned.
The easiest way to get rid of the error is to change your Redim lines to
ReDim Start(50)
ReDim Finish(50)
which is what I assume you meant to do in the first place.
Note: Based upon the format you used, you may have been meaning to do Start(0 to 50) and Finish(0 to 50) initially. The comma in the dimensioning indicates another dimension instead of a separation between lower and upper bounds.

Resources