Adding 1 to ColB while looking at values of ColA - excel

I have been trying to looping through a range to add 1 in ColB while looking at ColA values and I want to add 1 from high to low values where 0 will be empty.
Your help will be appreciated.
My try.
Dim lastRow As Integer
lastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
If Sheet1.Range("A2" & lastRow).Value > 0 Then
Range("B2" & lastRow).Value = 1
ElseIf Sheet1.Range("A2").Value = 0 Then
Range("B2" & lastRow).Value = ""
End If
2nd try
Dim lastRow As Integer
lastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
Dim c As Range
For Each c In Range("A2:A50")
If c.Value > 0 Then
Sheet1.Range("B2" & lastRow).Value = 1
End If
Next c
like this

Write to Column If the Value in Another Column is Not Equal to 0
Note that both solutions do the same.
For the array version, it is assumed that the range has at least two cells (A2:A3). If A2:A2, it will fail.
Option Explicit
Sub SlowRange()
Dim LastRow As Long
LastRow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
Dim rg As Range: Set rg = Sheet1.Range("A2:A" & LastRow)
Dim c As Range
For Each c In rg.Cells
If c.Value <> 0 Then
c.Offset(, 1).Value = 1
'Else
' c.Offset(, 1).Value = Empty
End If
Next c
End Sub
Sub FastArray()
Dim LastRow As Long
LastRow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
Dim rg As Range: Set rg = Sheet1.Range("A2:A" & LastRow)
Dim Data As Variant: Data = rg.Value
Dim r As Long
For r = 1 To UBound(Data, 1)
If Data(r, 1) <> 0 Then
Data(r, 1) = 1
Else
Data(r, 1) = Empty
End If
Next r
rg.Offset(, 1).Value = Data
End Sub

Related

sorting with vba

please help i want to sort the name column such that each name starts after every blank cell.
I want it look something like this..pls help it's a pretty long column
Option Explicit
Sub SetNamePosition()
Dim arr As Variant
Dim i As Long: i = 1 ' for Loop
Dim j As Long: j = 1 ' for Array
Dim lastRow As Long: lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Dim rngColB As Range: Set rngColB = Range("B2:B" & lastRow)
Dim rngNames As Range: Set rngNames = Range("C1") ' Temporary range
' Get column B names only
rngColB.SpecialCells(xlCellTypeConstants, 2).Copy
rngNames.PasteSpecial (xlPasteValues)
Application.CutCopyMode = False
Set rngNames = Range(rngNames, rngNames.End(xlDown))
' Load rngNames to array
arr = Application.Transpose(rngNames)
' Clear rng of column B and rngNames
rngColB.Clear
rngNames.Clear
' Insert names
For i = 2 To lastRow
' set name
Cells(i, 1).Offset(0, 1).Value = arr(j)
' find next cell
i = Cells(i, 1).End(xlDown).Row + 1
j = j + 1
Next i
End Sub
I's probably better to remove the empty ranges before making the array, but here's one way to distribute the names:
Loading the range ito an array, then go through the numbers and look for empty ranges.
This assumes that we are working with column "A" and "B" (1 and 2), starting at the top.
Sub test()
Dim arr As Variant
Dim lastRow As Long, i As Long, j As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
arr = Application.Transpose(Range("B2:B" & lastRow))
Range("B2:B" & lastRow).Clear
j = 1
For i = 2 To lastRow
Cells(i, 2) = arr(j)
j = j + 1
If j >= UBound(arr) Then Exit For
While arr(j) = "" And j < UBound(arr)
j = j + 1
Wend
While Not Cells(i, 1).Value = ""
i = i + 1
Wend
Next i
End Sub
Any leftover names will be removed

To Calculate Average Value of Multiple Range

I'm trying to calculate the Average value of multiple ranges as shown in attached Fig.
Conditions -
It should match the cell value of column "L" and "M" with a range of column "A" and Make a range (e.g 322810 to 324900) to calculate the average of column B values which are against the specific range (e.g 322810 to 324900).
I've been able to write the following code but it obviously not working.
Dim lastrow As Long
Dim i As Long, j As Long
With Worksheets("Source")
lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To lastrow + 1 'loop whole range (column C)
If .Cells(i, "L") = .Range("A").Value Then 'If column L cell value match with any cell of Range "A"
For j = i To lastrow 'Loop "group" range.
If .Cells(j, "M") = .Range("A").Value Then ' (end of small group range) then apply formula
.Cells(i, "N").Formula = "=AVERAGE(B" & i & ":B" & j & ")" 'AVG
Exit For
End If
Next j
End If
Next I
End With
All kind of help will be appreciated (Formula or VBA Code)
Yes, BigBen is right. This is the way. The Formula in my example is
=AVERAGEIFS($B$3:$B$16,$A$3:$A$16,">="&L4,$A$3:$A$16,"<="&M4)
Try,
Sub test()
Dim Lastrow As Long
Dim i As Long, j As Long
Dim r As Long
Dim mPoint As Long
Dim Ws As Worksheet
Dim vDB, vR()
Dim rngStart As Range, rngEnd As Range
Dim rngDB As Range
Set Ws = Worksheets("Source")
With Ws
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
vDB = .Range("L3", .Range("m" & .Rows.Count).End(xlUp))
r = UBound(vDB, 1)
ReDim vR(1 To r, 1 To 1)
For i = 1 To r
For k = 1 To Lastrow
If .Range("a1").Cells(k) = vDB(i, 1) Then
Set rngStart = .Range("a1").Cells(k)
mPoint = rngStart.Row
Exit For
End If
Next k
If rngStart Is Nothing Then
Else
For k = mPoint To Lastrow
If .Range("a1").Cells(k) = vDB(i, 2) Then
Set rngEnd = .Range("a1").Cells(k)
Exit For
End If
Next k
End If
If rngStart Is Nothing Or rngEnd Is Nothing Then
Else
Set rngDB = .Range(rngStart, rngEnd).Offset(, 1)
Debug.Print rngDB.Address
vR(i, 1) = WorksheetFunction.Average(rngDB)
End If
Set rngStart = Nothing
Set rngEnd = Nothing
Next i
.Range("n3").Resize(r) = vR
End With
End Sub

Count unique values and return results in another column

I have values in column B (green, blue, white....) and I want to count them and the result must appear in column A in the following format (green01, green02, green03...., blue01, blue02, blue03, blue04...., white01, white 02...).
The result must look like in this photo
I have searched the net for a macro, but I didn't find one to fit my needs.
THX
No VBA needed, in A1:
=B1&TEXT(COUNTIF(B$1:B1,B1),"00")
Try the next code, please:
Sub testCountSortColors()
Dim sh As Worksheet, lastRow As Long, i As Long, c As Long
Set sh = ActiveSheet
lastRow = sh.Range("B" & Rows.count).End(xlUp).Row
sh.Range("B1:B" & lastRow).Sort key1:=sh.Range("B1"), order1:=xlAscending, Header:=xlYes
For i = 2 To lastRow
If sh.Range("B" & i).value <> sh.Range("B" & i - 1).value Then
c = 1
Else
c = c + 1
End If
sh.Range("A" & i).value = sh.Range("B" & i).value & Format(c, "00")
sh.Range("A" & i).Font.Color = sh.Range("B" & i).Font.Color
Next
End Sub
I thought you maybe have column headers...
A Unique Count
Adjust the values in the constants section.
Option Explicit
Sub countUnique()
Const SourceColumn As Variant = 2 ' e.g. 2 or "B"
Const TargetColumn As Variant = 1 ' e.g. 1 or "A"
Const FirstRow As Long = 1
Dim rng As Range
Dim dict As Object
Dim Key As Variant
Dim Source As Variant, Target As Variant
Dim i As Long, UB As Long
Dim CurrString As String
Set rng = Columns(SourceColumn).Find(What:="*", _
LookIn:=xlFormulas, SearchDirection:=xlPrevious)
If rng Is Nothing Then GoTo exitProcedure
If rng.Row < FirstRow Then GoTo exitProcedure
Source = Range(Cells(FirstRow, SourceColumn), rng)
Set rng = Nothing
UB = UBound(Source)
Set dict = CreateObject("Scripting.Dictionary")
For i = 1 To UB
If Source(i, 1) <> "" Then
dict(Source(i, 1)) = dict(Source(i, 1)) + 1
End If
Next i
ReDim Target(1 To UB, 1 To 1)
For i = UB To 1 Step -1
CurrString = Source(i, 1)
If CurrString <> "" Then
Target(i, 1) = CurrString & Format(dict(CurrString), "00")
dict(CurrString) = dict(CurrString) - 1
End If
Next i
With Cells(FirstRow, TargetColumn)
.Resize(Rows.Count - FirstRow + 1).ClearContents
.Resize(UB) = Target
End With
MsgBox "Operation finished successfully."
exitProcedure:
End Sub

Finding duplicates in all columns with varying row numbers

I am new to VBA and was trying to write a macro to check duplicates among a column. I have values in columns from A to Z with varying last row number, some may be 5 while some may be 10. Is there any way to check if duplicate value exist among a column and then print "duplicate" on the first row (I dont have any values in the first row for all the columns). I need this for varying last row and last column number.
You can try:
Option Explicit
Public Sub Get_Unique_Count_Paste_Array()
Dim Ob As Object
Dim rng As Range
Dim i As Long
Dim str As String
Dim LR As Long
Dim Item As Variant
With Worksheets("Sheet1")
For i = 1 To 26
Set Ob = CreateObject("scripting.dictionary")
LR = .Cells(.Rows.Count, i).End(xlUp).Row
For Each rng In .Range(Cells(2, i), Cells(LR, i))
str = Trim(rng.Value)
If Len(str) > 0 Then
Ob(str) = Ob(str) + 1
End If
Next rng
For Each Item In Ob.keys
If .Cells(1, i).Value = "" Then
.Cells(1, i).Value = Item
ElseIf .Cells(1, i).Value <> "" Then
.Cells(1, i).Value = .Cells(1, i).Value & ", " & Item
End If
Next Item
Next i
End With
End Sub
Edited Version:
Option Explicit
Public Sub Get_Unique_Count_Paste_Array()
Dim Ob As Object
Dim rng As Range
Dim i As Long
Dim str As String
Dim LR As Long
Dim Item As Variant
With Worksheets("Sheet1")
For i = 1 To 26
Set Ob = CreateObject("scripting.dictionary")
LR = .Cells(.Rows.Count, i).End(xlUp).Row
For Each rng In .Range(Cells(2, i), Cells(LR, i))
str = Trim(rng.Value)
If Len(str) > 0 Then
Ob(str) = Ob(str) + 1
End If
Next rng
For Each Item In Ob.keys
If .Cells(1, i).Value = "" And Ob(Item) > 1 Then
.Cells(1, i).Value = "Duplicate"
Exit For
End If
Next Item
Next i
End With
End Sub
A slight alteration of #error 1004's idea
Private d As Scripting.Dictionary
Private s As String
Function Get_Dupe_Summary(rngInput As Excel.Range) as string
Dim c As Excel.Range
Set d = New Scripting.Dictionary
For Each c In rngInput.Cells
If d.Exists(c.Value) Then
Get_Dupe_Summary = Get_Dupe_Summary & _
IIf(Len(Get_Dupe_Summary) > 0, ",", "") & _
"Dupe : " & c & " on row " & c.Row
Else
d.Add c.Value, 1
End If
Next c
End Function

Split a string of text in a cell and have it correlate against multiple columns

I have a string of text that I want to split up. But at the same time when the string is split, I need it to pull the following data that is associated with it. I have tried transposing, and I have tried the split function. But it does not do what I need it to do. Any ideas or suggestions that I can try. Here is an example of what I am trying to accomplish:
This is what I currently have and changed and tried to modify from the first piece of code. Still cant figure it out:
Sub Test()
Dim rng As Range, Lstrw As Long, c As Range, d As Range
Dim SpltRng As Range
Dim i As Integer
Dim j As Integer
Dim Orig As Variant
Dim txt As String
Lstrw = Cells(Rows.Count, "A").End(xlUp).Row
Set rng = Range("A2:A" & Lstrw)
For Each c In rng.Cells
Set SpltRng = c.Offset(, 1)
txt = SpltRng.Value
Orig = Split(txt, ",")
Lstrw = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Range("B2:B" & Lstrw)
For Each d In rng.Cells
Set SpltRng = d.Offset(, 1) + 1
For i = 0 To LBound(Orig)
Cells(Rows.Count, "L").End(xlUp).Offset(1) = c
Cells(Rows.Count, "L").End(xlUp).Offset(, 1) = Orig(i)
For j = 0 To LBound(Orig)
Cells(Rows.Count, "L").End(xlUp).Offset(1) = d
Cells(Rows.Count, "L").End(xlUp).Offset(, 1) = Orig(j)
Next j
Next i
Next d
Next c
End Sub
You are way over thinking it, you only need to add one line to the code rovided by #Davesexcel:
Sub ChickatAH()
Dim rng As Range, Lstrw As Long, c As Range
Dim SpltRng As Range
Dim i As Integer
Dim Orig As Variant
Dim txt As String
Lstrw = Cells(Rows.Count, "A").End(xlUp).Row
Set rng = Range("A2:A" & Lstrw)
For Each c In rng.Cells
Set SpltRng = c.Offset(, 1)
txt = SpltRng.Value
Orig = Split(txt, " ")
For i = 0 To UBound(Orig)
Cells(Rows.Count, "L").End(xlUp).Offset(1) = c
Cells(Rows.Count, "L").End(xlUp).Offset(, 1) = Orig(i)
'New Line
Cells(Rows.Count, "L").End(xlUp).Offset(, 2).Resize(, 3).Value = c.Offset(, 2).Resize(, 3).Value
Next i
Next c
End Sub

Resources