I'm trying to use "for loop" to get some specific values, code runs perfect but values do not print to destination.
subcode, where the problem is, looks like this :
lastRow = Range("H" & Rows.Count).End(xlUp).Row
For i = 5 To lastRow
radius = Range("C" & i).Value
spacing = Range("L" & i).Value
Select Case radius
Case 0 To 450
spacing = 6
Case 451 To 750
spacing = 9
Case 751 To 2000
spacing = 18
End Select
Next i
In your code you are storing a value from Range("L" & i).Value to the variable spacing. Then, in your For loop you are assigning a new value to the variable, but you are not setting the range (cell) to that value.
The below, albeit shoddy, will work for you.
lastRow = Range("H" & Rows.Count).End(xlUp).Row
For i = 5 To lastRow
radius = Range("C" & i).Value
Select Case radius
Case 0 To 450
Range("L" & i).Value = 6
Case 451 To 750
Range("L" & i).Value = 9
Case 751 To 2000
Range("L" & i).Value = 18
End Select
Next i
Related
The AE, AG, AH, whenever the D or AD parallel cells are empty, return e.g. date of 00/01/1900 or time as 00:00. Can you please clarify how to return blank if the same parallel cell in D or AD is blank? Thanks
Sub valuedifference()
Dim Total As Double
Dim TimeX As Date
Dim TimeY As Date
Dim LastRow As Long
Dim i As Long
With ThisWorkbook.Sheets("Test1")
LastRow = .Cells(.Rows.Count, "d").End(xlUp).Row
For i = 2 To LastRow
TimeX = CDate(.Range("d" & i).Value)
TimeY = CDate(.Range("ad" & i).Value)
Total = TimeValue(TimeY) - TimeValue(TimeX)
.Range("ae" & i).Value = Total
.Range("ag" & i).Value = Abs(Total * 24)
.Range("ah" & i).Value = Abs(Total * 1440)
Next i
End With
End Sub
I think this a formatting issue- If TimeY is "" and TimeX is "", then
Total = TimeValue(TimeY) - TimeValue(TimeX) is 0.
0 in time format is 0:0:00 and 0 in date format is 00/01/1900
One solution to this is to include an if statement that checks that there are not blank cells
For i = 2 To LastRow
If .Range("D" & i).Value <> "" And .Range("AD" & i).Value <> "" Then
TimeX = CDate(.Range("d" & i).Value)
TimeY = CDate(.Range("ad" & i).Value)
Total = DateDiff("n", TimeY, TimeX)
.Range("AE" & i).Value = Total
.Range("AG" & i).Value = Format(Abs(Total), "#.##")
.Range("AH" & i).Value = Format(Abs(Total), "#.##")
End If
Next i
First, I'm new to programming VBA to excel so if this program makes no sense...here's to the learning curve.
I'm trying to concatenate a range from "BO1:BQ" & DLimportLastRow, where DLimportLastRow is the last row of a variable import that was just run before this code. I've done as much internet research as i can, but most of the concatenate codes i found make no sense to me so i can't modify them to meet my needs. The end goal is to concatenate BO1:BQ" & DLimportLastRow with each cell separated by a "," and fitting into one cell which is "AE" & LastRow, where LastRow is the next blank cell in "AE".
DLimportLastRow = WorkEnd.Cells(WorkEnd.Rows.Count, "BO").End(xlUp).Offset(1).Row
DLimportLastRow = "BQ" & DLimportLastRow
i = 1
Do Until IsEmpty(Range("BO" & i).Value) = True Or Range("C" & i).Value = "" Or Range("C" & i).Value = Null Or Range("C" & i).Value = 0
x = Workbooks(WName).Worksheets("ReportGroupingInfo").Range("BO" & i).Value
For Each cell In Range("BO1", DLimportLastRow)
y = x & cell.Value & ","
i = i + 1
Next
Loop
Range("AE" & LastRow).Value = y
Got it:
Dim y as string
i = 1
Do Until IsEmpty(Range("BO" & i).Value) = True Or Range("C" & i).Value = "" Or Range("C" & i).Value = Null Or Range("C" & i).Value = 0
For Each cell In Range("BO1", DLimportLastRow)
y = y & cell.Value & ","
i = i + 1
Next
Loop
Range("AE" & LastRow).Value = y
So Every time I run my code I get the Types Mismatch error and I can't find what to change into what in order to run it properly. Even though this might still not entirely Solve the greater problem which is how to formulate my question into the vba code, but first things first. How to get this line of code without errors:
Range("I" & r & ":" & "I" & B).Value = Range("I" & r & ":" & "I" & B).Value + 1
Where r = the current row the code is checking ( 5 to 44)
and B is the last row the code can check ( 44)
All I want this line to do is to add one to the already existing value of the cell (which is 0 if nothing is done in that row or a formula if conditions are met, this formula will make a value from 1 to 40)
You cannot do this the way you are doing -> you cannot add a number to a range of cells at once. You must add the number to one cell at a time.
Try this:
Dim i As Integer
For i = r To B
Range("I" & i).Value = Range("I" & i).Value + 1
Next
To handle formulas:
Dim i As Integer
For i = r To B
Dim numberToAdd As Integer
numberToAdd = 1
If Range("I" & i).HasFormula Then
Range("I" & i).Value = Range("I" & i).Formula & "+" & Trim(Str(numberToAdd))
Else
Range("I" & i).Value = Range("I" & i).Value + 1
End If
Next
I have some VBA code to re-calculate subtotals on a worksheet change event. The subtotals are then assigned to the appropriate cells. The code below works, but the cell values aren't getting updated. When I put it in debugger and step through the code, FreightTotal and OtherTotal are calculated correctly and rw references the correct row. The value in the two variables isn't getting assigned to the cells. I have no idea what is going on, it should work.
Sheet1.Range("H" & rw).Value = FreightTotal
Sheet1.Range("J" & rw).Value = OtherTotal
Here is the if full loop:
If Target.Column = 8 Then
For rw = 11 To 36
If Range("A" & rw).Value = "Total" Then
i = rw - 1
FreightTotal = 0
OtherTotal = 0
Do While Range("BA" & i).Value = Range("BA" & rw).Value
FreightTotal = FreightTotal + Range("H" & i).Value
OtherTotal = OtherTotal + Range("J" & i).Value
i = i - 1
Loop
Sheet1.Range("H" & rw).Value = FreightTotal
Sheet1.Range("J" & rw).Value = OtherTotal
End If
Next
End If
I know a little bit about VBA but I can't seem to be able to work my way around this programming question.
I have a sheet where I want to program how many days it will take before the end of the tasking. Each Status are equal to a number of days, for exemple if a file is at the Pending stage it will take 180 total to be completed. But what i want is at each stage to write the number of days it will take. For example
Status is written in range E3:E160
If cell in range= Pending then
Offset 4 columns over and write 20, and offset 5 columns over and write 35 and offset 6 columns over and write 50, and offset 7 columns over and write 25, and offaet 8 columns over and write 15 and offset 9 columns over and write 15 and finally, offset 10 columns over and write 20
However if cell in range = "Planning" then offset 5 columns over and write 35, and offset 6 columns over and write 50 and so on until offset 10 columns over and write 20
The goal is tha for each status, the number of offset is based on the status.
Hope this help
I'm assuming ther will be a loop or something but I really can't figure it out.
Also it needs to be able to capture any new rows inserted within the range or outside the range.
Thanks to anyone who will be able to help me
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim LastRow As Long
Dim i As Long
LastRow = Range("E" & Rows.Count).End(xlUp).Row
For i = 3 To LastRow
If Range("E" & i).Value = "Pending" Then
Range("I" & i).Value = "20" And Range("J" & i).Value = 35 And Range("K" & i).Value = 50 And Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Planning" Then
Range("J" & i).Value = 35 And Range("K" & i).Value = 50 And Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Screening" Then
Range("K" & i).Value = 50 And Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Exam" Then
Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Interview" Then
Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "References" Then
Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Closing" Then
Range("O" & i).Value = 20
End If
Next i
End Sub
If Range("E" & i).Value = "Pending" Then
Range("I" & i).Value = 20
Range("J" & i).Value = 35
Range("K" & i).Value = 50
Range("L" & i).Value = 25
Range("M" & i).Value = 15
Range("N" & i).Value = 15
Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Planning" Then
Range("J" & i).Value = 35
Range("K" & i).Value = 50
Range("L" & i).Value = 25
Range("M" & i).Value = 15
Range("N" & i).Value = 15
Range("O" & i).Value = 20`
You need to get rid of all the And statements in your Then clause. This is an example. You can change the rest. You might want to look into the Case Select method as well.