The code for the question "Sorting a range of values correctly in VBA" is below:
'Legend
Worksheets(1).Range("U1").Value = "Legend"
Worksheets(1).Range("U1:V1").Merge
'Classified Cases in Ranges
Worksheets(1).Range("U2:V2").Merge
Worksheets(1).Range("U2").Value = "Classified Cases in Ranges"
'Format the Legend
Worksheets(1).Range("U3:U40").NumberFormat = "#"
Worksheets(1).Range("V3:V40").NumberFormat = "#"
'Legend values
Worksheets(1).Range("U3").Value = 0
Worksheets(1).Range("U4").Value = 1
Worksheets(1).Range("U5").Value = 2
Worksheets(1).Range("U6").Value = 3
Worksheets(1).Range("U7").Value = 4
Worksheets(1).Range("U8").Value = 5
Worksheets(1).Range("U9").Value = 6
Worksheets(1).Range("U10").Value = 7
Worksheets(1).Range("U11").Value = 8
Worksheets(1).Range("U12").Value = 9
Worksheets(1).Range("U13").Value = 10
Worksheets(1).Range("U14").Value = 11
Worksheets(1).Range("U15").Value = 12
Worksheets(1).Range("U16").Value = 13
Worksheets(1).Range("U17").Value = 14
Worksheets(1).Range("U18").Value = 15
Worksheets(1).Range("U19").Value = 16
Worksheets(1).Range("U20").Value = 17
Worksheets(1).Range("U21").Value = 18
Worksheets(1).Range("U22").Value = 19
Worksheets(1).Range("U23").Value = 20
Worksheets(1).Range("U24").Value = 21
Worksheets(1).Range("U25").Value = 22
Worksheets(1).Range("U26").Value = 23
Worksheets(1).Range("U27").Value = 24
Worksheets(1).Range("U28").Value = 25
Worksheets(1).Range("U29").Value = 26
Worksheets(1).Range("U30").Value = 27
Worksheets(1).Range("U31").Value = 28
Worksheets(1).Range("U32").Value = 29
Worksheets(1).Range("U33").Value = 30
Worksheets(1).Range("U34").Value = 31
Worksheets(1).Range("U35").Value = 32
Worksheets(1).Range("U36").Value = 33
Worksheets(1).Range("U37").Value = 34
Worksheets(1).Range("U38").Value = 35
Worksheets(1).Range("U39").Value = 36
Worksheets(1).Range("U40").Value = "(blank)"
'Classified Cases in Ranges values
Worksheets(1).Range("V3").Value = "0-9"
Worksheets(1).Range("V4").Value = "10-19"
Worksheets(1).Range("V5").Value = "20-29"
Worksheets(1).Range("V6").Value = "30-39"
Worksheets(1).Range("V7").Value = "40-49"
Worksheets(1).Range("V8").Value = "50-59"
Worksheets(1).Range("V9").Value = "60-69"
Worksheets(1).Range("V10").Value = "70-79"
Worksheets(1).Range("V11").Value = "80-89"
Worksheets(1).Range("V12").Value = "90-99"
Worksheets(1).Range("V13").Value = "100-109"
Worksheets(1).Range("V14").Value = "110-119"
Worksheets(1).Range("V15").Value = "120-129"
Worksheets(1).Range("V16").Value = "130-139"
Worksheets(1).Range("V17").Value = "140-149"
Worksheets(1).Range("V18").Value = "150-159"
Worksheets(1).Range("V19").Value = "160-169"
Worksheets(1).Range("V20").Value = "170-179"
Worksheets(1).Range("V21").Value = "180-189"
Worksheets(1).Range("V22").Value = "190-199"
Worksheets(1).Range("V23").Value = "200-209"
Worksheets(1).Range("V24").Value = "210-219"
Worksheets(1).Range("V25").Value = "220-229"
Worksheets(1).Range("V26").Value = "230-239"
Worksheets(1).Range("V27").Value = "240-249"
Worksheets(1).Range("V28").Value = "250-259"
Worksheets(1).Range("V29").Value = "260-269"
Worksheets(1).Range("V30").Value = "270-279"
Worksheets(1).Range("V31").Value = "280-289"
Worksheets(1).Range("V32").Value = "290-299"
Worksheets(1).Range("V33").Value = "300-309"
Worksheets(1).Range("V34").Value = "310-319"
Worksheets(1).Range("V35").Value = "320-329"
Worksheets(1).Range("V36").Value = "330-339"
Worksheets(1).Range("V37").Value = "340-349"
Worksheets(1).Range("V38").Value = "350-359"
Worksheets(1).Range("V39").Value = ">=360"
Worksheets(1).Range("V40").Value = "(blank)"
'***************************************************
'***************************************************
'***************************************************
'Move to US MACRO worksheet
Worksheets(2).Activate
'Pivot Table 1
'Pivot Table 1
'Pivot Table 1
LastRow = Cells(Rows.Count, 2).End(xlUp).Row
'Use vlookup() function and perform conditional formatting
For x = 4 To (LastRow - 1)
Cells(x, 1).FormulaR1C1 = "=VLOOKUP(RC[1],'US Master Macro'!R[-1]C[20]:R[36]C[21],2,FALSE)"
If Cells(x, 2) >= 8 Then
Cells(x, 1).Interior.ColorIndex = 3
Cells(x, 1).Font.ThemeColor = xlThemeColorDark1
Cells(x, 1).Font.Bold = True
Cells(x, 2).Interior.ColorIndex = 3
Cells(x, 2).Font.ThemeColor = xlThemeColorDark1
Cells(x, 2).Font.Bold = True
Cells(x, 3).Interior.ColorIndex = 3
Cells(x, 3).Font.ThemeColor = xlThemeColorDark1
Cells(x, 3).Font.Bold = True
Else
Cells(x, 1).Interior.ColorIndex = 2
End If
Next x
"I have the following data in a pivot table. To the left, is described a range and to the right the number of cells within that range. I would like to learn a code that correctly arranges the range in the left and color codes anything after "80-89". I have used the sort function but it doesn't seem to work. Please help."
Days Count of PR ID
"10-19" 656
"0-9" 480
"20-29" 190
"30-39" 115
"40-49" 65
"50-59" 47
"70-79" 28
"60-69" 23
"80-89" 12
"110-119" 3
"120-129" 2
"130-139" 1
"100-109" 1
"180-189" 1
Related
I have a list of 1000+ names in a single column in excel where the names repeat occasionally. I am trying to count how many times each name occurs. This is what I have currently and it populates the desired sheet but it seems to mess up when counting the number of times the names show up. Anything helps!
m = 2
n = 1
person = Worksheets("Sheet1").Cells(m, 6).Value
Worksheets("Sorted_Data").Cells(n, 2).Value = person
Worksheets("Sorted_Data").Cells(n, 3).Value = 1
n = n + 1
m = m + 1
For i = 0 To Total_Tickets
person = Worksheets("Sheet1").Cells(m, 6).Value
y = 1
d = 0
Do While d <= i
comp = Worksheets("Sorted_Data").Cells(y, 2).Value
x = StrComp(person, comp, vbTextCompare)
If x = 0 Then
Worksheets("Sorted_Data").Cells(n - 1, 3).Value = Worksheets("Sorted_Data").Cells(n - 1, 3).Value + 1
m = m + 1
d = 10000
ElseIf x = 1 Or x = -1 Then
If comp = "" Then
Worksheets("Sorted_Data").Cells(n, 2).Value = person
Worksheets("Sorted_Data").Cells(n, 3).Value = 1
n = n + 1
m = m + 1
d = 10000
End If
y = y + 1
d = d + 1
End If
Loop
Next i
You're managing a lot of counters there, and that makes the logic more difficult to follow.
You could consider something like this instead:
Sub Tester()
Dim wsData As Worksheet, wsList As Worksheet, arr, m, i As Long, nm
Set wsData = ThisWorkbook.Sheets("Sheet1")
Set wsList = ThisWorkbook.Sheets("Sorted_Data")
'grab all the names in an array
arr = wsData.Range("A2:A" & wsData.Cells(Rows.Count, "A").End(xlUp).Row).Value
For i = 1 To UBound(arr, 1) 'loop over the array
nm = arr(i, 1) 'grab the name
m = Application.Match(nm, wsList.Columns("A"), 0) 'existing name on the summary sheet?
If IsError(m) Then
'name was not found: add it to the summary sheet
With wsList.Cells(Rows.Count, "A").End(xlUp).Offset(1)
.Value = nm
m = .Row
End With
End If
With wsList.Cells(m, "B")
.Value = .Value + 1 'update the count
End With
Next i
End Sub
I am trying to construct and If statement that turns a tab Red if two of three cells are colored, or Turns green if only on is colored. I was hoping that there would be an easier way to right it than three if statements like this.
Dim dateRng As String, num As Integer, j As Integer, irng As Range, frng As Range
dateRng = Sheets("Input Raw Data").Range("B" & counter + 2).Value
num = Sheets("Tool Setup").Range("C18").Value
NumPts = num * 3
For s = 1 To Sheets.Count
With Sheets(s)
For j = 1 To num
If .Name = j Then
.Range("A1:C1").Merge
.Range("A1") = dateRng
.Name = Sheets("Point Names").Range("B" & (3 * j - 1))
End If
Next j
End With
Next s
For s = 1 to Sheets.Count
With Sheets(s)
For y = 1 To NumPts
If .Name = Sheets("Reporting").Range("B" & (12 * y - 5)) Then
For k = 6 To -1
Set irng = Sheets("Reporting").Range("A" & (12 * y - k))
Set irng = Sheets("Reporting").Range(irng, irng.End(xlToRight).End(xlToRight))
irng.Copy (.Range("A2"))
Next k
.Columns("A:A").ColumnWidth = 12
.Columns("B:B").EntireColumn.AutoFit
If .Range("B7").Interior.ColorIndex > 0 Then
a = 1
End If
If .Range("B8").Interior.ColorIndex > 0 Then
a = a + 1
End If
If .Range("B9").Interior.ColorIndex > 0 Then
a = a + 1
End If
If a >= 2 Then
.Tab.ColorIndex = 3
ElseIf a <= 1 Then
.Tab.ColorIndex = 4
End If
End If
y = y + 2
Next y
End With
Next s
Something like this may help you. It still has multiple if statements. But the statements are simple and don't have to deal with how the combinations of different cells being colored.
Also, I used colorindex > 0 as the condition for having color filling.
a = 0
If .Range("B7").Interior.ColorIndex > 0 Then
a = 1
End If
If .Range("B8").Interior.ColorIndex > 0 Then
a = a + 1
End If
If .Range("B9").Interior.ColorIndex > 0 Then
a = a + 1
End If
If a = 2 Then
.Range("B10").Interior.ColorIndex = 3
ElseIf a = 1 Then
.Range("B10").Interior.ColorIndex = 43
End If
I am coding a series of loops to search one table row by row for data, and if it meets the correct specs within that row it will pull a certain value from said row and place it in a cell I want in a separate table on another chart. I am getting the error saying that there is a "Next" without a "For", but there is (unless I am entirely missing something). It also has the Sub line at the top highlighted (I am trying to run this as a macro). Let me know where I am going wrong here, thanks!
Option Explicit
Sub GraphLoop()
Dim i, g, row, color, roundcount, round, materialcount, material As Long
Dim chartdatacol, chartdatarow As Long
Worksheets("Chart Data").Range("C6:DR10000").Value = ""
roundcount = 1
materialcount = 1
color = 1
round = 1
material = 1
chartdatacol = 3
chartdatarow = 6
i = 4
For chartdatacol = 3 To Worksheets("Running Avg Log").Cells(4, cols.Count).End(xlUp).row
Do Until i = Worksheets("Running Avg Log").Cells(Rows.Count, "A").End(xlUp).row
g = 1
If Worksheets("Running Avg Log").Cells(i, 1).Value = Worksheets("Chart Data").Cells(chartdatarow, 2).Value _
And Worksheets("Running Avg Log").Cells(i, 2).Value = round _
And Worksheets("Running Avg Log").Cells(i, 3).Value = color _
And Worksheets("Running Avg Log").Cells(i, 4).Value = material Then
row = chartdatarow
Worksheets("Chart Data").Cells(row, chartdatacol).Value = _
Worksheets("Running Avg Log").Cells(i, 6 + Worksheets("Analysis").Range("C5").Value).Value
chartdatarow = chartdatarow + 1
i = 4
Else
i = i + 1
End If
Loop
color = color + 1
' loops through ten colors
If color > 10 Then
color = 1
End If
roundcount = roundcount + 1
materialcount = materialcount + 1
' every ten columns, material changes, every 30 columns, it repeats
If materialcount = 11 Or 21 Then
material = material + 1
End If
If materialcount > 30 Then
materialcount = 1
material = 1
' each round's 30 combos is 30 columns
If roundcount = 31 Then
round = round + 1
End If
Next chartdatacol
End Sub
You left out an End If here:
If materialcount > 30 Then
materialcount = 1
material = 1
' each round's 30 combos is 30 columns
BTW, another correction to make:
If materialcount = 11 Or 21 Then
should be:
If materialcount = 11 Or materialcount = 21 Then
You had an If without End If
See modified code below , I marked with ********* the place you were missing the End If :
Option Explicit
Dim i, g, row, color, roundcount, round, materialcount, material As Long
Dim chartdatacol, chartdatarow As Long
Sub GraphLoop()
Worksheets("Chart Data").Range("C6:DR10000").Value = ""
roundcount = 1
materialcount = 1
color = 1
round = 1
material = 1
chartdatacol = 3
chartdatarow = 6
i = 4
For chartdatacol = 3 To Worksheets("Running Avg Log").Cells(4, Columns.Count).End(xlUp).row
Do Until i = Worksheets("Running Avg Log").Cells(Rows.Count, "A").End(xlUp).row
g = 1
If Worksheets("Running Avg Log").Cells(i, 1).Value = Worksheets("Chart Data").Cells(chartdatarow, 2).Value _
And Worksheets("Running Avg Log").Cells(i, 2).Value = round _
And Worksheets("Running Avg Log").Cells(i, 3).Value = color _
And Worksheets("Running Avg Log").Cells(i, 4).Value = material Then
row = chartdatarow
Worksheets("Chart Data").Cells(row, chartdatacol).Value = _
Worksheets("Running Avg Log").Cells(i, 6 + Worksheets("Analysis").Range("C5").Value).Value
chartdatarow = chartdatarow + 1
i = 4
Else
i = i + 1
End If
Loop
color = color + 1
' loops through ten colors
If color > 10 Then
color = 1
End If
roundcount = roundcount + 1
materialcount = materialcount + 1
' every ten columns, material changes, every 30 columns, it repeats
If materialcount = 11 Or 21 Then
material = material + 1
End If
If materialcount > 30 Then
materialcount = 1
material = 1
' each round's 30 combos is 30 columns
If roundcount = 31 Then
round = round + 1
' **** YOU WERE MISSING the End If here ***
End If
End If
Next chartdatacol
End Sub
I am trying to duplicate the results found in the following site: http://peltiertech.com/?s=variable+column
The only difference is that I would like to use VBA code only to accomplish the end result. I would like to avoid referencing any fields on a worksheet. The end result creates columns with variable widths. I have confirmed that the process described on the website works when using data on a worksheet. I just can’t make the transition to getting the same result with only using code.
Here is what I have so far:
Sub RangeTest()
Dim MyArray1(1 To 14) As Variant
Dim MyArray2(1 To 14) As Variant
Dim MyArray3(1 To 14) As Variant
Dim MyArray4(1 To 14) As Variant
Dim MyArray5(1 To 14) As Variant
MyArray1(1) = 0
MyArray1(2) = 0
MyArray1(3) = 12.5
MyArray1(4) = 25
MyArray1(5) = 25
MyArray1(6) = 50
MyArray1(7) = 75
MyArray1(8) = 75
MyArray1(9) = 112.5
MyArray1(10) = 150
MyArray1(11) = 150
MyArray1(12) = 200
MyArray1(13) = 250
MyArray1(14) = 250
MyArray2(1) = 0
MyArray2(2) = 100
MyArray2(3) = 100
MyArray2(4) = 100
MyArray2(5) = 0
MyArray2(6) = 0
MyArray2(7) = 0
MyArray2(8) = 0
MyArray2(9) = 0
MyArray2(10) = 0
MyArray2(11) = 0
MyArray2(12) = 0
MyArray2(13) = 0
MyArray2(14) = 0
MyArray3(1) = 0
MyArray3(2) = 0
MyArray3(3) = 0
MyArray3(4) = 0
MyArray3(5) = 75
MyArray3(6) = 75
MyArray3(7) = 75
MyArray3(8) = 0
MyArray3(9) = 0
MyArray3(10) = 0
MyArray3(11) = 0
MyArray3(12) = 0
MyArray3(13) = 0
MyArray3(14) = 0
MyArray4(1) = 0
MyArray4(2) = 0
MyArray4(3) = 0
MyArray4(4) = 0
MyArray4(5) = 0
MyArray4(6) = 0
MyArray4(7) = 0
MyArray4(8) = 50
MyArray4(9) = 50
MyArray4(10) = 50
MyArray4(11) = 0
MyArray4(12) = 0
MyArray4(13) = 0
MyArray4(14) = 0
MyArray5(1) = 0
MyArray5(2) = 0
MyArray5(3) = 0
MyArray5(4) = 0
MyArray5(5) = 0
MyArray5(6) = 0
MyArray5(7) = 0
MyArray5(8) = 0
MyArray5(9) = 0
MyArray5(10) = 0
MyArray5(11) = 25
MyArray5(12) = 25
MyArray5(13) = 25
MyArray5(14) = 0
ActiveSheet.ChartObjects.Add(Left:=10, Width:=900, Top:=265, Height:=245).Name = "Testing1"
ActiveSheet.ChartObjects("Testing1").Chart.ChartType = xlAreaStacked
ActiveSheet.ChartObjects("Testing1").Chart.Axes(xlCategory).CategoryType = xlTimeScale
With ActiveSheet.ChartObjects("Testing1").Chart
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = MyArray1
.SeriesCollection(1).Values = MyArray2
.SeriesCollection(1).Name = "Alpha"
.SeriesCollection.NewSeries
.SeriesCollection(2).XValues = MyArray1
.SeriesCollection(2).Values = MyArray3
.SeriesCollection(2).Name = "Beta"
.SeriesCollection.NewSeries
.SeriesCollection(3).XValues = MyArray1
.SeriesCollection(3).Values = MyArray4
.SeriesCollection(3).Name = "Gamma"
.SeriesCollection.NewSeries
.SeriesCollection(4).XValues = MyArray1
.SeriesCollection(4).Values = MyArray5
.SeriesCollection(4).Name = "Delta"
End With
End Sub
This creates the chart but does not make the conversion to variable column width.
You didn't say what went wrong.
I didn't change much of your code, just rearranged it to make it more readable and maybe more efficient.
Sub RangeTest()
Dim MyChart As ChartObject
Dim MyArray1(1 To 14) As Variant
Dim MyArray2(1 To 14) As Variant
Dim MyArray3(1 To 14) As Variant
Dim MyArray4(1 To 14) As Variant
Dim MyArray5(1 To 14) As Variant
MyArray1(1) = 0
MyArray1(2) = 0
MyArray1(3) = 12.5
MyArray1(4) = 25
MyArray1(5) = 25
MyArray1(6) = 50
MyArray1(7) = 75
MyArray1(8) = 75
MyArray1(9) = 112.5
MyArray1(10) = 150
MyArray1(11) = 150
MyArray1(12) = 200
MyArray1(13) = 250
MyArray1(14) = 250
MyArray2(1) = 0
MyArray2(2) = 100
MyArray2(3) = 100
MyArray2(4) = 100
MyArray2(5) = 0
MyArray2(6) = 0
MyArray2(7) = 0
MyArray2(8) = 0
MyArray2(9) = 0
MyArray2(10) = 0
MyArray2(11) = 0
MyArray2(12) = 0
MyArray2(13) = 0
MyArray2(14) = 0
MyArray3(1) = 0
MyArray3(2) = 0
MyArray3(3) = 0
MyArray3(4) = 0
MyArray3(5) = 75
MyArray3(6) = 75
MyArray3(7) = 75
MyArray3(8) = 0
MyArray3(9) = 0
MyArray3(10) = 0
MyArray3(11) = 0
MyArray3(12) = 0
MyArray3(13) = 0
MyArray3(14) = 0
MyArray4(1) = 0
MyArray4(2) = 0
MyArray4(3) = 0
MyArray4(4) = 0
MyArray4(5) = 0
MyArray4(6) = 0
MyArray4(7) = 0
MyArray4(8) = 50
MyArray4(9) = 50
MyArray4(10) = 50
MyArray4(11) = 0
MyArray4(12) = 0
MyArray4(13) = 0
MyArray4(14) = 0
MyArray5(1) = 0
MyArray5(2) = 0
MyArray5(3) = 0
MyArray5(4) = 0
MyArray5(5) = 0
MyArray5(6) = 0
MyArray5(7) = 0
MyArray5(8) = 0
MyArray5(9) = 0
MyArray5(10) = 0
MyArray5(11) = 25
MyArray5(12) = 25
MyArray5(13) = 25
MyArray5(14) = 0
Set MyChart = ActiveSheet.ChartObjects.Add(Left:=10, Width:=900, Top:=265, Height:=245)
With MyChart
.Name = "Testing1"
With .Chart
With .SeriesCollection.NewSeries
.XValues = MyArray1
.Values = MyArray2
.Name = "Alpha"
End With
With .SeriesCollection.NewSeries
.XValues = MyArray1
.Values = MyArray3
.Name = "Beta"
End With
With .SeriesCollection.NewSeries
.XValues = MyArray1
.Values = MyArray4
.Name = "Gamma"
End With
With .SeriesCollection.NewSeries
.XValues = MyArray1
.Values = MyArray5
.Name = "Delta"
End With
.ChartType = xlAreaStacked
With .Axes(xlCategory)
.CategoryType = xlTimeScale
.MajorUnitScale = xlDays
.MajorUnit = 50
End With
End With
End With
End Sub
I am new in and is trying to give an automated ID in which i used the following code:
y = 0
If txtdsgnation = "Plumbing" Then
x = "P"
Do Until Cells(i, 3) = ""
If Cells(i, 3) = "Plumbing" Then
y = y + 1
End If
i = i + 1
Loop
End If
If txtdsgnation = "Electricity" Then
x = "E"
Do Until Cells(i, 3) = ""
If Cells(i, 3) = "Electricity" Then
y = y + 1
End If
i = i + 1
Loop
End If
If txtdsgnation = "Fittings" Then
x = "F"
Do Until Cells(i, 3) = ""
If Cells(i, 3) = "Fittings" Then
y = y + 1
End If
i = i + 1
Loop
End If
If txtdsgnation = "Lift maintenance" Then
x = "L"
Do Until Cells(i, 3) = ""
If Cells(i, 3) = "Lift maintenance" Then
y = y + 1
End If
i = i + 1
Loop
End If
z = 100 + y
txtID = x + "-" + z
The thing i am trying to do is that if i add a new employee, for example a plumber and I already have 2 plumbers txtID will automatically become "P-102"
Thanks in advance
In the loop, the y is always getting initialized with 0. So, y has no chance to become more than 1. Please try to put the y out of the loop.