I need help to create one VBA for below issue -
column C should be updated based on column A and B values.
in below example if column A=Unit1 and column B=IND then Person 1 and so on.. it should loop till end.
Thank you in adv. please refer picture for sample data
enter image description here
Here you go. Just change the sheet name since I don't know what you're working with.
Sub AddPerson()
Dim i As Long, ws As Worksheet, lRow As Long
Set ws = Sheets("Sheet1") 'Change to your sheet name
lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
With ws
.Range("C2:C" & lRow + 1).ClearContents
For i = 2 To lRow
If .Range("B" & i) = "IND" Then
.Range("C" & i) = "Person1"
ElseIf .Range("A" & i) = "Unit5" And .Range("B" & i) = "OTR" Then
.Range("C" & i) = "Person3"
Else
.Range("C" & i) = "Person2"
End If
Next i
End With
End Sub
I'm updating a report everyday and adding data from yesterdays runs. I would like to insert a code in the macro to add the date into column A next to the newly added data without changing the previous dates already in column A.
Sub datedd()
Dim lastRow As Long
lastRow = Range("B" & Rows.Count).End(xlUp).Row
With Range("A2:A" & lastRow)
.Value = Now -1
.NumberFormat = "mm/dd/yy"
End With
End Sub
But this changes all the dates in column A
not sure why pasting in the code breaks apart like this, sorry im new here!
Sub datedd()
Dim lastRow As Long
Dim firstRow As Long
lastRow = Range("B" & Rows.Count).End(xlUp).Row
firstRow = Range("A" & Rows.Count).End(xlUp).Row + 1
With Range("A" & CStr(firstRow) & ":A" & CStr(lastRow))
.Value = Now -1
.NumberFormat = "mm/dd/yy"
End With
End Sub
So my code is just supposed to autofill Column "H" and "I" with "0" and "1", respectively. It works just as it is supposed to, however for some reason when it gets to a certain section, it turns into dates (e.g. "1/0/1900", "1/1/1900". Not sure why it only does it to certain cells and not others. Here is my code:
Sub company()
With Sheets("Combined")
.Columns("A:XFD").NumberFormat = "Number"
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("H2:H" & LastRow).Value = 0
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("I2:I" & LastRow).Value = 1
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
.Range("A2:A" & LastRow).Value2 = "P"
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("C2:C" & LastRow).Value2 = 7
Sheets("Combined").Select
End With
End Sub
You can apply number formatting to your columns to ensure correct appearance of your numbers e.g.
Columns("H:I").NumberFormat = "0"
I have a sheet with two columns. Column (E) contains ID and names from the data source and column (K) contains ID,which are extracted from comment section.
Column E contains sometime ID, starting with B2C, and sometime names and Id starting with 5. column K contains always ID starting with B2C. the length of the ID B2C is usually 11 to 13 Digit Long. the length of ID starting with 5 is 8 Digit Long.
I would like to have a VBA that checks both the columns, if there is an id starting with 5 or some Name in column E, then it should look into column K, if an ID starting with B2C is present, then it should copy to Column L, else copy the same value(from column E) to column L.
I researched through find and replace. I saw examples where exact Name of find was given and replaced with given Name. I am able to form an algorithm, but struck how to start with code in my case. the code below, has an runn time error
object varaible or with block variable not set .
Sub compare()
Dim i As Long
Dim ws As Worksheet
ws = Sheets("Sheet1")
For i = 1 To Rows.Count
If ws.Cells(i, 11).Value = "" Then
ws.Cells(i, 12).Value = ws.Cells(i, 5).Value
Else
ws.Cells(i, 12).Value = ws.Cells(i, 11).Value
End If
Next i
End Sub
I have an Image below, which Shows the end result.
Any lead would be appreciated.
The issue causing the error message is that you are missing a Set statement for your worksheet object. You must use Set when assigning an object to a variable, that is anything with its own methods. A simple data type without methods (String, Integer, Long, Boolean, ...) doesn't need the Set statement, and can just be directly assigned like i = 0.
Your code should be updated to:
Dim i As Long
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
' RED FLAG! Rows.Count is going to cause you to loop through the entire column,
' see the below example for how to use the UsedRange property.
For i = 1 To Rows.Count
If ws.Cells(i, 11).Value = "" Then
ws.Cells(i, 12).Value = ws.Cells(i, 5).Value
Else
ws.Cells(i, 12).Value = ws.Cells(i, 11).Value
End If
Next I
An alternative which avoids even using a worksheet variable would be to use a With block:
Dim r As Long
With ThisWorkbook.Sheets("Sheet1")
For r = 2 To .UsedRange.Rows.Count
.Range("L" & r).Value = .Range("E" & r).Value
If .Range("K" & r).Value = "" Then .Range("L" & r).Value = .Range("K" & r).Value
Next r
End With
Edit:
There are various ways to find the last used row, each with their drawbacks. A drawback of both UsedRange and xlCellTypeLastCell is they are only reset when you save/close/re-open the workbook. A better solution can be found in this answer.
Sub compare()
Dim r As Long, lastrow As Long, ws As WorkSheet
Set ws = ThisWorkbook.Sheets("Sheet1")
lastrow = LastRowNum(ws)
With ws
For r = 2 To lastrow
.Range("L" & r).Value = .Range("E" & r).Value
If .Range("K" & r).Value = "" Then .Range("L" & r).Value = .Range("K" & r).Value
Next r
End With
End Sub
' Function from linked question
Public Function LastRowNum(Sheet As Worksheet) As Long
LastRowNum = 1
If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
LastRowNum = Sheet.Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End If
End Function
This is my solution:
Option Explicit
Sub Compare()
Dim i As Long
Dim lngLastRow As Long
Dim ws As Worksheet
lngLastRow = Range("A1").SpecialCells(xlCellTypeLastCell).Row
Set ws = Worksheets(1)
With ws
.Columns(12).Clear
.Cells(1, 12) = "Extract from Comment"
For i = 1 To lngLastRow
If .Cells(i, 11).Value = "" Then
.Cells(i, 12).Value = ws.Cells(i, 5).Value
Else
.Cells(i, 12).Value = ws.Cells(i, 11).Value
End If
Next i
End With
End Sub
It clears column(12) and writes Extract from comment in the first cell of the row, to make sure that everything is clean.
lngLastRow is the last row of the sheet.
My below code only gives output for 2 rows, rest it is not getting applied don't know why? formula needs to applied from K20 till last row of adjacent column(J) . Can someone help me in correcting it. Thanks!
Sub SortS()
Range("K19").Select
ActiveCell.FormulaR1C1 = "Sort"
With Sheets("Sheet1")
rowlast = .Range("K" & .Rows.Count).End(xlUp).Row
With .Range("K20:K" & rowlast)
.Formula = "=IF(COUNTIF(RC[-6]:RC[-2],""S"")>0,1,0)"
.Value = .Value
End With
End With
End Sub
You're not looking at column J for the last row - try changing this
rowlast = .Range("K" & .Rows.Count).End(xlUp).Row
to this
rowlast = .Range("J" & .Rows.Count).End(xlUp).Row