fill a range until a certain value is reached - excel

Is there a way in vba to fill a range until a certain value is reached?
For example, I want to fill column A with dates starting from the value in cell B1 (Let’s say 1/1/2014) till it reaches the value in cell C1 (Let’s say 1/15/2014) stepping one day.
Is this possible please?
Thank u.

Here is my proposition, just put the dates in B1 and C1 cells and run "Dates" macro.
Sub Dates()
Dim startdate As Date
Dim enddate As Date
Dim row As Double
startdate = Range("B1").Value
enddate = Range("C1").Value
row = 0
Range("A1").Select
Do Until DateAdd("d", 1, startdate) = enddate + 1
ActiveCell.Offset(row, 0).Value = DateAdd("d", 1, startdate)
startdate = startdate + 1
row = row + 1
Loop
End Sub

How about:
Sub DateFiller()
For i = 2 To Rows.Count
Cells(i, 1).Value = Cells(i - 1, 1).Value + 1
If Cells(i, 1).Value = Cells(1, 2).Value Then Exit Sub
Next i
End Sub
EDIT#1:
this version matches the request:
Sub DateFiller()
Cells(1, 1).Value = Cells(1, 2).Value
For i = 2 To Rows.Count
Cells(i, 1).Value = Cells(i - 1, 1).Value + 1
If Cells(i, 1).Value = Cells(1, 3).Value Then Exit Sub
Next i
End Sub

Related

How to insert data rows with Select Case

My VBA script errors when I try to insert data from Sheet1 into Sheet2. Script code delivers only "Case 2-3" ROW numbers, first "Case" does not input into Sheet 2. Wondering what else should be included in VBA script to finalize processes?
My VBA Script:
Sub CopyFromSheet1()
Dim i As Long
For i = 1 To Sheet1.Cells(Sheet1.Rows.Count, 6).End(xlUp).Row ' Last Cell of Column F
Select Case CStr(Sheet1.Cells(i, 3).Value) ' Looks at the Value in Column C
Case "Due From"
Sheet2.Cells(22, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL1"
Sheet2.Cells(23, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL2"
Sheet2.Cells(24, 3).Value = Sheet1.Cells(i, 6).Value
End Select
Next i
End Sub
What about this: Do you everything your doing, but just for Column C. And then Do it all again for column D?
Sub CopyFromSheet1()
Dim i As Long
Dim col as Long
for col = 3 to 4
For i = 1 To Sheet1.Cells(Sheet1.Rows.Count, 6).End(xlUp).Row ' Last Cell of Column F
Select Case CStr(Sheet1.Cells(i, col).Value)
Case "Due From"
Sheet2.Cells(22, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL1"
Sheet2.Cells(23, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL2"
Sheet2.Cells(24, 3).Value = Sheet1.Cells(i, 6).Value
End Select
Next i
next col
End Sub

Using Year(Date) in IF statement but no output

I am trying to grab data from three columns in my workbook AK,AL and AM respectively. After getting the data I am doing 3 different comparisons which are stated in the code below.
Firstly, I am comparing the date in Column AL and Column AM. I am checking if Column AL is of year 2018 and Column AM is not of year 2018. If its true then It will insert text in Column L called "Routine". This is done cell by cell using a for loop as seen in the code.
Next, there is a check if Column AM is of year 2018 and Column AK is color coded to Yellow color. If it is true then text will be inserted in Column L called "New".
Lastly, there is a check if Column AM is of year 2018 and Column AK is not colored in Yellow. If it is true then text will be inserted in Column 'L' called "Major"
Else, The cell will be left blank without any data inserted.
PROBLEM: The code runs fine and there are no issues or errors. But I am not able to get the output I want. The code does not insert any text in the Column L
Dim j As Long
Dim lastrow As Long
Dim ws1 As Worksheet
Dim wbk As Workbook
Dim wb As Worksheet
Dim date1 As Date, date2 As Date
Set wbk = Application.Workbooks("MaxiTrak RV Service Report - Blank.xlsm")
Set ws1 = wbk.Worksheets("ML_PSV_SERVICE")
lastrow = ws1.range("AL" & Rows.Count).End(xlUp).Row
For j = 2 To lastrow
date1 = ws1.Cells(j, 38).Value
date2 = ws1.Cells(j, 39).Value
If Year(date1) = Year(Date) - 1 And Year(date2) <> Year(Date) - 1 Then
Cells(j, 12).Value = "Routine"
If Year(date2) = Year(Date) - 1 And Cells(j, 37).Interior.ColorIndex = 6 Then
Cells(j, 12).Value = "New"
If Year(date2) = Year(Date) - 1 And Cells(j, 37).Interior.ColorIndex <> 6 Then
Cells(j, 12).Value = "Major"
Else
Cells(j, 12).Value = ""
End If
End If
End If
Next j
Sample Output expected
Try code below, it's pretty simple and self-explanatory:
Sub Compare()
Dim lastRow As Long, i As Long
lastRow = Range("AK" & Rows.Count).End(xlUp).Row
For i = 1 To lastRow
If Year(Range("AM" & i)) <> 2018 Then
If Year(Range("AL" & i)) = 2018 Then Range("L" & i) = "Routine"
' column AM has year equal to 2018
ElseIf Range("AK" & i).Interior.ColorIndex = 6 Then
Range("L" & i) = "New"
Else
Range("L" & i) = "Major"
End If
Next
End Sub
In the following code that you supplied you have:
If Year(date2) = Year(Date) - 1 And Cells(j, 37).Interior.ColorIndex = 6 Then
Cells(j, 12).Value = "New"
If Year(date2) = Year(Date) - 1 And Cells(j, 37).Interior.ColorIndex <> 6 Then
Cells(j, 12).Value = "Major"
Else
Cells(j, 12).Value = ""
End If
End If
Where you get into the first if statement because cells(j,37).interior.colorindex = 6, but then, you do a check where cells(j,37).interior.colorindex <> 6.
The conflict here is that it will always set cells(j,12).value = "". Either your first cell reference is off or your second reference is off. Another possibility is that you need to change one of the colorindex values.
From your description, I believe you have too many if statements. You also have an if statement nested inside another if statement.
Dim j, lastrow As Long
Dim ws1, wb As Worksheet
Dim wbk As Workbook
Dim dateAL, dateAM As Date
Dim colorID As Variant
Set wbk = Application.Workbooks("MaxiTrak RV Service Report - Blank.xlsm")
Set ws1 = wbk.Worksheets("ML_PSV_SERVICE")
lastrow = ws1.Range("AL" & Rows.Count).End(xlUp).Row
currentyear = Year(Date)
For j = 2 To lastrow
dateAL = Year(ws1.Cells(j, 38).Value) ' column AL
dateAM = Year(ws1.Cells(j, 39).Value) ' column AM
colorID = ws1.Cells(j, 37).Interior.ColorIndex
If dateAL = currentyear - 1 And dateAM <> currentyear - 1 Then
Cells(j, 12).Value = "Routine"
Else
If dateAM = currentyear - 1 And colorID = 6 Then
Cells(j, 12).Value = "New"
Else
Cells(j, 12).Value = "Major"
End If
End If
Next j
Try the above code, but please review the code FIRST.

I am looking for a VBA code for the below scenario:

I am looking for a VBA code for the below scenario:
There are four columns (A, B, C, D) in an excel sheet and the code should populate the D column.
The logic should be
IF C2 = A2, then populate D2 with value in B2
else if C2 = A3, then populate D2 with B3 and so on till D2 gets right value.
The columns are long lists with 400 entries.
Try this:
Sub MySub()
Dim lastRow As Long, i As Long
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
For i = 2 To lastRow
If Cells(i, 1).Value = Cells(i, 3).Value Then
Cells(i, 4).Value = Cells(i, 2).Value
Else If Cells(i + 1, 1).Value = Cells(i, 3).Value Then
Cells(i, 4).Value = Cells(i + 1, 2).Value
End If
Next
End Sub
Sub test()
Dim x As Integer
Dim erow As Integer
'erow takes in the value of the last row number.
erow = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For x = 1 To erow
For i = x To erow
If Cells(x, 3).Value = Cells(i, 1).Value Then
Cells(i, 2).Select
Application.CutCopyMode = False
Selection.Copy
Cells(x, 4).Select
ActiveSheet.Paste
Application.CutCopyMode = False
i = erow
End If
Next i
Next x
End Sub
Is this what you need. It does this for every D column not only for 2nd D column.

Highlight rows based pf column criteria VBA

Im trying to write a VBA script to compare two = rows and have the spreadsheet highlight the duplicate rows only if certain criteria is met, such as (Value of row, column a = Value of row-1, column) AND Value of row, column b > Value of row-1, column b) Then entirerow of the greater value in column b.font.color = vbRed.
Here is a section of the table I'm running...
Table Selection
Here is the code I am using...
Sub RemoveDuplicates()
Dim i As Long, R As Long
'Dim DeviceName As Range, SerialNumber As Range, LastContact As Range
Application.ScreenUpdating = False
R = Cells(Rows.Count, 1).End(xlUp).Row
'Set DeviceName = Columns(2)
'Set SerialNumber = Columns(3)
'Set LastContact = Columns(7)
For i = R To 2 Step -1
'If Cells(i, "F").Value > Cells(i - 1, "F").Value Then
'Code above doesn't work
If Cells(i, 3).Value = Cells(i - 1, 3).Value And Cells(i, 2).Value = Cells(i - 1, 2).Value Then
'If Cells(i, 3).Value = Cells(i - 1, 3).Value And Cells(i, 2).Value = Cells(i - 1, 2).Value And Cells(i, 5).Value > Cells(i - 1, 5).Value Then
'Code above doesn't work
Cells(i, 1).EntireRow.Font.Color = vbRed
End If
Next i
Application.ScreenUpdating = True
End Sub
I can get the duplicates to highlight, but when I try to introduce the greater than check, the system gets janky.
try a conditional formatting rule.
With worksheets("sheet1").usedrange.offset(1, 0).entirerow
.FormatConditions.Delete
With .FormatConditions.Add(Type:=xlExpression, Formula1:="=and($a2=$a1, $b2=$b1, $f2>$f1)")
.font.Color = vbRed
End With
End With

read Cellvalue through a range, and write to a different range

I am trying to make a VBA scrip that check all cells between B2 and B60 for the text "Ja" that's yes in Norwegian.
How can I make this a little bit simpler that making a "if" command for each cell?
I want it to, if the cell contains "ja"(yes) then write to colum D and the same number.
ie. B1,2,3,4,5 cotains "ja", I need it to take the previous cell value in D1.2,3,4,5 and add another digit to it +1.
If nothing is found in B(ie.false) it needs to write "NEI" in the current cell, and if "NEI" (no) is found in that cell it adds +1 to colum E
Sub Macro2()
Dim celltxt As String
Dim a As Variant
If IsEmpty(Range("B2").Value) = True Then
Cells(2, 2).Value = "NEI"
End If
celltxt = ActiveSheet.Range("B2").Text
If InStr(1, celltxt, "ja") Then
a = Cells(2, 1).Value
'write to cell
Cells(2, 4).Value = Cells(2, 4) + 1
Else
'antall Cw'er vedkommende IKKE har deltatt på
Cells(2, 5).Value = Cells(2, 5) + 1
End If
If IsEmpty(Range("B3").Value) = True Then
Cells(3, 2).Value = "NEI"
End If
celltxt = ActiveSheet.Range("B3").Text
If InStr(1, celltxt, "ja") Then
a = Cells(3, 1).Value
'write to cell
Cells(3, 4).Value = Cells(3, 4) + 1
Else
'antall Cw'er vedkommende IKKE har deltatt på
Cells(3, 5).Value = Cells(3, 5) + 1
End If
End Sub
Sub slettingALL()
Range("D2:E55").Select
Selection.ClearContents
End Sub
Sub slettingdeltakelse()
Range("B2:B60").Select
Selection.ClearContents
End Sub
The following code uses a For Each loop and an IF THEN ELSE statement to check for the value "JA" in the range B2:B60.
If it finds "JA", it looks two columns to the right from the current i location, and adds "+1" to the value above it. If it finds nothing, it writes "NEI" to the current i location, and then moves three columns to the right and adds +1 to the value above it.
Sub Macro2()
For Each i In Range(Cells(2, 2), Cells(60, 2))
If i.Value = "JA" Then
i.Offset(0, 2).Value = i.Offset(-1, 2).Value + 1
Else
i.Value = "NEI"
i.Offset(0, 3).Value = i.Offset(-1, 3).Value + 1
End If
Next i
End Sub
Please let me know if this code does not work for your purpose.

Resources