I have a piece of code that worked for part but took ages to run (about 1700 rows). When I updated it to perform the whole function it now doesn't do anything- not sure where Ive gone wrong, and is there a version of code that would be quicker?
I'm still very new so do my code by searching what I want to do then bending it to suit.
I would like to check in column I for all dates that are less than the date in cell Z1. If any cells in the corresponding row say "Issued" I would like it to change to "Overdue".
Sub updateoverdue()
Application.ScreenUpdating = True
Dim j As Long, i As Long, lastRow1 As Long, lastRow2 As Long
Dim sh_1, sh_3 As Worksheet
Set sh_1 = Sheet6
Set sh_3 = Sheet6
lastRow1 = sh_1.UsedRange.Rows.Count
For j = 2 To lastRow1
Sheet6.Range("z1") = sh_1.Cells(j, 9).Value
lastRow2 = sh_3.UsedRange.Rows.Count
For i = 2 To lastRow2
If sh_3.Cells(i, 9).Value < Sheet6.Range("z1") And sh_3.Cells(i, 10).Value = "Issued" Then
sh_3.Cells(i, 10).Value = "Overdue"
End If
Next i
Next j
Application.ScreenUpdating = True
End Sub
I got it working on just Column J but then it failed when I added in the Issued part. Plus I cant get it to change more than one column (J to W).
Something like this might work for you:
Sub tgr()
Dim ws As Worksheet
Set ws = Sheet6
Dim TargetDate As Date
TargetDate = ws.Range("Z1").Value2
Dim DateList As Range
Set DateList = ws.Range("I2", ws.Cells(ws.Rows.Count, "I").End(xlUp))
If DateList.Row < 2 Then Exit Sub 'No dates
Dim DateCell As Range
For Each DateCell In DateList.Cells
If DateCell.Value2 > TargetDate And LCase(Trim(DateCell.Offset(, 1).Value)) = "issued" Then
DateCell.Offset(, 1).Value = "Overdue"
End If
Next DateCell
End Sub
I tested this and it worked fine:
Sub try()
Dim ws As Worksheet, lastrow As Long
Set ws = Sheet6
lastrow = ws.Cells(Rows.Count, 9).End(xlUp).Row
Application.ScreenUpdating = False
For i = 1 To lastrow
If ws.Cells(i, 9).Value < ws.Cells(1, 26).Value Then
ws.Cells(i, 10).Value = "Overdue"
ElseIf ws.Cells(i, 9).Value > ws.Cells(1, 26).Value Then
ws.Cells(i, 10).Value = "Issued"
Else
ws.Cells(i, 10).Value = "Due Today"
End If
Next i
Application.ScreenUpdating = True
End Sub
Or you can just use an excel formula and drop it down the entire column:
=IF(I1<$Z$1, "Issued","Overdue")
Related
I was able to get this much working but not able to get it to start from Range("B12:B500").
Sub findTwoEmptyCells()
Dim lastRow As Long, i As Long
Dim firstEmptyCell As Range
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
For i = 1 To lastRow
If Cells(i, 1).Value = "" And Cells(i, 1).Offset(1, 0).Value = "" Then
Set firstEmptyCell = Cells(i, 1)
Exit For
End If
Next i
End If
firstEmptyCell.Value = "x"
End Sub
Thanks to #Dominique & #FaneDuru, i was able to get the result as per the below by combining both their ideas :
Sub findTwoEmptyCells()
Dim lastRow As Long, i As Long
Dim firstEmptyCell As Range
lastRow = Cells(Rows.Count, 12).End(xlUp).Row
For i = 12 To lastRow
If Cells(i, 2).Value = "" And Cells(i + 1, 1).Value = "" Then
Set firstEmptyCell = Cells(i, 1)
Exit For
End If
Next i
If firstEmptyCell Is Nothing Then
MsgBox ("There are no two empty cells in a row")
Exit Sub
End If
firstEmptyCell.Value = "x"
End Sub
I'm attempting to match values between two sheets and if found and the conditions are met, perform the action of changing the cell colour.
PROBLEM:
I'm getting an error with my For...Next loop, even though I thought I have a NEXT for each FOR statement. Not sure what I've done wrong.
Also, I'm not sure my counters are setup correctly to accurately scan through each sheet/column needed. Any help would be appreciated.
Sub ReadData()
Dim wb As Workbook
Dim ws As Worksheet
Dim lastrow As Long
Dim i As Long
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Ref1")
Set ws2 = wb.Sheets("TRA")
lastrow = Sheets("Ref1").Cells(Rows.Count, "A").End(xlUp).Row
lastrow2 = Sheets("TRA").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Ref1").Activate
i = 2
k = 2
For i = 2 To lastrow
For k = 2 To lastrow2
If Cells(i, 4).Value = "Active" Then
If ws.Cells(i, 18).Value = ws2.Cells(i, 1).Value And (ws2.Cells(i, 23).Value <> "Cancelled" Or ws2.Cells(i, 23).Value <> "Completed") Then
Cells(i, 20).Interior.ColorIndex = 9
End If
Next
Next
End Sub
Quick Repair
To better understand the code, it is often preferable to use letters,
instead of numbers, for columns.
The Code
Sub ReadData()
Dim wb As Workbook
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim lastrow As Long
Dim lastrow2 As Long
Dim i As Long
Dim k As Long
' Use ThisWorkbook instead of ActiveWorkbook, if the code is
' in the same workbook where these sheets are.
With ActiveWorkbook
Set ws = .Worksheets("Ref1")
Set ws2 = .Worksheets("TRA")
End With
lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
lastrow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastrow
If ws.Cells(i, "D").Value = "Active" Then
For k = 2 To lastrow2
If ws.Cells(i, "R").Value = ws2.Cells(k, "A").Value _
And ws2.Cells(k, "W").Value <> "Cancelled" _
And ws2.Cells(k, "W").Value <> "Completed" Then
ws.Cells(i, "T").Interior.ColorIndex = 9 ' Brown
Exit For
End If
Next
End If
Next
End Sub
I am looking to fill a spreadsheet with data repeating data, so 25 appointments for today, 25 appointments for tomorrow with the same name and so on for as far as possible.
Is the a simple way of filling the table where the date increases in blocks of 25?
Example of what i am trying to do
Try using this you might be able to achieve what you want ,any problems shout out
'to change the date to the next day
Public Function ExtraDay(strDate As String)
Dim tDay As Date
tDay = Format(DateAdd("d", 1, strDate), "dd/mm/yy")
ExtraDay = tDay
End Function
'gets the last used row
Function getThelastUsedRowAddress() As Integer
'Get Last Row in Worksheet UsedRange
Dim LastRow As Range, ws As Worksheet
Set ws = ActiveSheet
MsgBox ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row
getThelastUsedRowAddress = ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row
End Function
'button command on the sheet
Private Sub CommandButton1_Click()
Dim n, t As Integer
Dim ns As String
n = getThelastUsedRowAddress()
t = n + n
ns = CStr(t)
Call getThelastUsedRow(CStr(n))
Call TheLoopRange(CStr(n) + 1, ns)
End Sub
'get the last used and paste after
Sub getThelastUsedRow(address As String)
'Get Last Row in Worksheet UsedRange
Dim LastRow As Range, ws As Worksheet
Dim numcopied As Integer
Dim numonpaper As Integer
Set ws = ActiveSheet
numcopied = ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row
numonpaper = numcopied + 1
ws.UsedRange.Copy 'Destination:=Wst.Cells(1, 1)
'paste
Sheets("Sheet1").Range("A" & numonpaper).PasteSpecial xlPasteValues
End Sub
'loop the pasted range and change date to the next day from date
Sub TheLoopRange(rangestart As String, rangeend As String)
'rangestart,rangeend
Dim rCell As Range
Dim rRng As Range
Set rRng = Sheet1.Range("E" & rangestart & ":E" & rangeend)
For Each rCell In rRng.Cells
'MsgBox rCell.Value
rCell.Value = ExtraDay(rCell.Value)
Next rCell
End Sub
Lets as assume that:
We use Sheet1
Company column is column D
Date column is column I
Pease try:
Option Explicit
Sub Test()
Dim Lastrow As Long, i As Long
With ThisWorkbook.Worksheets("Sheet1")
Lastrow = .Cells(.Rows.Count, "D").End(xlUp).Row
For i = 2 To Lastrow
If i = 2 Then
.Cells(i, 9).Value = Date + 1
ElseIf i <> 2 And .Cells(i, 4).Value = 1 Then
.Cells(i, 9).Value = .Cells(i, 9).Offset(-1, 0).Value + 1
Else: .Cells(i, 9).Value = .Cells(i, 9).Offset(-1, 0).Value
End If
Next i
End With
End Sub
I'm new in the VBA. its there any way to connect with sheet 2 and sheet 1. See the picture below:
Sheet 1
Sheet 2
I tried using this VBA code but its not working.
Sub Finddata()
Dim x As Long
Sheets("Sheet2").Activate
For x = 2 To Sheets("Sheet2").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
For y = 2 To 5
On Error Resume Next
Cells(x, y).Value = Application.WorksheetFunction.VLookup(Cells(x, "A").Value, Sheets("Sheet1").Range("A2:F18"), y = 1, 0)
Next y
Next x
End Sub
How about the following, this uses the .Find method to search for the given value in Sheet1 and then return by offsetting:
Sub Finddata()
Dim x As Long, LastRow As Long
Dim FindValues As Range
Dim ws As Worksheet: ws = Sheets("Sheet1")
Dim ws2 As Worksheet: ws2 = Sheets("Sheet2")
'above declare variables
LastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Sheet2
For x = 2 To LastRow 'loop from row 2 to last on Sheet2
Set FindValues = ws.Range("A:A").Find(What:=ws2.Cells(x, 1).Value, Lookat:=xlWhole)
'use the Find method to search for the value in Sheet1
If Not FindValues Is Nothing Then 'if found
ws2.Cells(x, 2).Value = FindValues.Offset(0, 1).Value
ws2.Cells(x, 3).Value = FindValues.Offset(0, 2).Value
ws2.Cells(x, 4).Value = FindValues.Offset(0, 3).Value
ws2.Cells(x, 5).Value = FindValues.Offset(0, 4).Value
ws2.Cells(x, 6).Value = FindValues.Offset(0, 5).Value
'pass the values into Sheet2
End If
Next x
End Sub
I have a very basic question. I have a large Workbook with alot of listed information. I want to take out some of the information into a new workbook and sort it on the different worksheets. I am having some problems with making the code understand which tab I want the information to be placed into. the strName = Range(cell value) do not work and I do not really know what I am doing wrong. How can I make this works? Sorry about the very messy code.
Private Sub CommandButton1_Click()
Dim strName As String
Set sourceWq = Workbooks("SD KPIs 2014 onwards").Worksheets("VQN+Concessionn")
Set front = Workbooks("databank progging").Worksheets("Frontpage")
For l = 5 To 30
For i = 2 To 250000 'Goes through the sourceWq workbook
If front.Cells(l, 13).Value = sourceWq.Cells(i, 24).Value Then 'Finds correct supplier
strName = Range("l,13")
Sheets(strName).Select 'Selects the correct worksheet for the supplier
For j = 4 To 15 'Month
If sourceWq.Cells(i, 33).Value = Cells(7, j).Value Then
For n = 8 To 11 'The type of NCR
If sourceWq.Cells(i, 27).Value = Cells(n, 2).Value Then
Cells(n, j).Value = Cells(n, j).Value + 1
Else: End If
Next n
Else: End If
Next j
Else: End If
Next i
Next l
End Sub
I slightly rewrote your code without this loop For i = 2 To 250000 (I use Find method instead):
Private Sub CommandButton1_Click()
Dim strName As String
Dim sourceWq As Worksheet, front As Worksheet, sh As Worksheet
Dim rng As Range
Dim firstAddress As String
Dim wb1 As Workbook, wb2 As Workbook
Dim l As Long, i As Long, j As Long
Set wb1 = Workbooks("SD KPIs 2014 onwards")
Set wb2 = Workbooks("databank progging")
Set sourceWq = wb1.Worksheets("VQN+Concessionn")
Set front = wb2.Worksheets("Frontpage")
For l = 5 To 30
With sourceWq.Range("X2:X250000")
Set rng = .Find(front.Cells(l, 13).Value, LookIn:=xlValues)
End With
If Not rng Is Nothing Then
firstAddress = rng.Address
Do
strName = front.Cells(l, 13).Value
Set sh = wb2.Worksheets(strName)
With sh
For j = 4 To 15 'Month
If rng.Offset(, 9).Value = .Cells(7, j).Value Then
For n = 8 To 11 'The type of NCR
If rng.Offset(, 3).Value = .Cells(n, 2).Value Then
.Cells(n, j).Value = .Cells(n, j).Value + 1
End If
Next n
End If
Next j
End With
Set rng = .FindNext(rng)
Loop While Not rng Is Nothing And rng.Address <> firstAddress
End If
Next l
End Sub
strName = Range("l,13") should read strName = Cells(l,13)