Terminal Value from a to C using VBA Loop - excel

I have an excel sheet name as Terminal Volumes In this sheet, there are three variables (Terminal LF (log Fisher Cost) and CT Cost) that are using as input. I am using the following code for working on these.
enter code here
Sub InitiateValues()
ReDim TrmPnts(11) '<<<<CHANGE HERE Total Point + SE -JJ
Set ShDat = Worksheets("Data")
Set ShDatPth = Worksheets("DataPath")
Set tvol = Worksheets("Terminal Volumes")
'ShDatPth.Range("B2:FB157").Value = 0
With TrmPnts(1)
.Cpt = "T1"
.CptColr = "&H000088"
.Row = ShDat.Cells(2, 1)
.Col = ShDat.Cells(3, 1)
.LFamt = tvol.Cells(2, 2)
.CTamt = tvol.Cells(2, 3)
End With
With TrmPnts(3)
.Cpt = "T2"
.CptColr = "&H000088"
.Row = ShDat.Cells(6, 1)
.Col = ShDat.Cells(7, 1)
.LFamt = tvol.Cells(3, 2)
.CTamt = tvol.Cells(3, 3)
End With
With TrmPnts(4)
.Cpt = "T3"
.CptColr = "&H000088"
.Row = ShDat.Cells(8, 1)
.Col = ShDat.Cells(9, 1)
.LFamt = tvol.Cells(4, 2)
.CTamt = tvol.Cells(4, 3)
End With
With TrmPnts(5)
.Cpt = "T4"
.CptColr = "&H000088"
.Row = ShDat.Cells(10, 1)
.Col = ShDat.Cells(11, 1)
.LFamt = tvol.Cells(5, 2)
.CTamt = tvol.Cells(5, 3)
End With
With TrmPnts(6)
.Cpt = "T5"
.CptColr = "&H000088"
.Row = ShDat.Cells(12, 1)
.Col = ShDat.Cells(13, 1)
.LFamt = tvol.Cells(6, 2)
.CTamt = tvol.Cells(6, 3)
End With
With TrmPnts(7)
.Cpt = "T6"
.CptColr = "&H000088"
.Row = ShDat.Cells(14, 1)
.Col = ShDat.Cells(15, 1)
.LFamt = tvol.Cells(7, 2)
.CTamt = tvol.Cells(7, 3)
End With
With TrmPnts(8)
.Cpt = "T7"
.CptColr = "&H000088"
.Row = ShDat.Cells(16, 1)
.Col = ShDat.Cells(17, 1)
.LFamt = tvol.Cells(8, 2)
.CTamt = tvol.Cells(8, 3)
End With
With TrmPnts(9)
.Cpt = "T8"
.CptColr = "&H000088"
.Row = ShDat.Cells(18, 1)
.Col = ShDat.Cells(19, 1)
.LFamt = tvol.Cells(9, 2)
.CTamt = tvol.Cells(9, 3)
End With
With TrmPnts(10)
.Cpt = "T9"
.CptColr = "&H000088"
.Row = ShDat.Cells(20, 1)
.Col = ShDat.Cells(21, 1)
.LFamt = tvol.Cells(10, 2)
.CTamt = tvol.Cells(10, 3)
End With
With TrmPnts(11)
.Cpt = "T10"
.CptColr = "&H000088"
.Row = ShDat.Cells(22, 1)
.Col = ShDat.Cells(23, 1)
.LFamt = tvol.Cells(11, 2)
.CTamt = tvol.Cells(11, 3)
End With '<<<<<<CHANGE HERE
With TrmPnts(2)
.Cpt = "SE"
.CptColr = "&H004400"
.Row = ShDat.Cells(4, 1)
.Col = ShDat.Cells(5, 1)
End With
ReDim Mtx(156, 157) ' Cell Size X,Y -JJ
For i = 1 To 156 ' Cell Size X,Y -JJ 1 To 226
For j = 1 To 157 ' Cell Size X,Y -JJ
Mtx(i, j) = ShDat.Cells(1 + i, 1 + j)
ShDatPth.Cells(1 + i, 1 + j) = 0
Next
Next
EdtMod = True
End Sub
This code working fine to input the value to T10 (Only for 10 Rows). I want to use it for T1500 (1500 Rows) for the Column A to Column C- using loop.
The structure of the worksheet is as under.

Just make a For...next loop and use the variable of the loop to calculate the correct fields like the following (I hope I interpreted the logic correctly, please check):
Sub InitiateValues()
ReDim trmpnts(11) '<<<<CHANGE HERE Total Point + SE -JJ
Set ShDat = Worksheets("Data")
Set ShDatPth = Worksheets("DataPath")
Set tvol = Worksheets("Terminal Volumes")
'ShDatPth.Range("B2:FB157").Value = 0
Dim I As Long
For I = 1 To UBound(trmpnts)
With trmpnts(I)
.Cpt = "T" & I
.CptColr = "&H000088"
.Row = ShDat.Cells(I * 2, 1)
.Col = ShDat.Cells(I * 2 + 1, 1)
.LFamt = tvol.Cells(I, 2)
.CTamt = tvol.Cells(I, 3)
End With
Next I
....
End Sub
That should do the trick...

Related

Data cleaning and identification of incomplete orders

Sub FormatAndIncompleteOrders()
Dim a, Q&, i&, b(1 To 2), R, j%
Application.ScreenUpdating = False
Rem -----------------------------------\
a = Range("'Original Data'!A3").CurrentRegion: Q = UBound(a)
ReDim R(1 To Q, 1 To 4): b(1) = R: b(2) = R
ReDim R(1 To 2) As Long
Rem -----------------------------------\
For i = 2 To Q
Select Case True
Case a(i, 2) <> "" And a(i, 3) <> "" And a(i, 4) <> ""
R(1) = 1 + R(1): b(1) = fillArray(b(1), R(1), a, i)
Case a(i, 2) <> ""
R(2) = 1 + R(2): b(2) = fillArray(b(2), R(2), a, i)
End Select
Next
Rem -----------------------------------\
With Sheets("New Orders")
.Select
.Range("A3").CurrentRegion.Offset(1).Delete xlShiftUp
.Range("A4").Resize(R(1), 4) = b(1)
End With
Rem -----------------------------------\
With Sheets("Incomplete Orders")
.Range("A1").CurrentRegion.Offset(1).Delete xlShiftUp
.Range("A2").Resize(R(2), 4) = b(2)
End With
End Sub
*I am trying to use the code below to format and clean data it keeps giving me an error message "Sub or function not defined"

Add the first item to my listbox if empty vba

Hi I have a userform with a 'search' listbox and a 'results' listbox. If I first import data into the results listbox ' lstresults ' from another array my code works and I can import individual items from ' lstsearch ' and it shows a new item in 'lstresults'.
When the lstresults is empty though before attempting to add data I can see the data has been added in my watch to a list variable in results but it is not visible in the lstresults. I have fiddled with various elements of the code but I must be missing something. Any help appreciated:
Private Sub cmdAddPlant_Click()
' Need to take data from selected line item from search box
' Add item to results list
lstResults.Visible = True
Dim ctlsource As Control
Dim plnt As Integer
Set ctlsource = Me!lstSearch
For plnt = 0 To ctlsource.ListCount - 2
If ctlsource.Selected(plnt) = True Then
With Me.lstResults.List
If lstResults.ListCount > 0 Then _
Me.lstResults.AddItem
Me.lstResults.List(lstResults.ListCount - 1, 0) = lstSearch.List(plnt, 0)
Me.lstResults.List(lstResults.ListCount - 1, 1) = lstSearch.List(plnt, 1)
Me.lstResults.List(lstResults.ListCount - 1, 3) = lstSearch.List(plnt, 2)
Me.lstResults.List(lstResults.ListCount - 1, 2) = lstSearch.List(plnt, 3)
Else
' I added this to try and deal with the first item but no dice
lstResults.AddItem
lstResults.List(0, 0) = lstSearch.List(plnt, 0)
lstResults.List(0, 1) = lstSearch.List(plnt, 1)
lstResults.List(0, 3) = lstSearch.List(plnt, 2)
lstResults.List(0, 2) = lstSearch.List(plnt, 3)
End If
End With
lstSearch.RemoveItem (plnt) ' Avoids accidently selecting plant twice
End If
Next plnt
With lstResults
Dim sTemp As String
Dim sTemp2 As String
Dim LbList As Variant
'Store the list in an array for sorting
LbList = Me.lstResults.List
If UBound(LbList) > 1 Then
'Bubble sort the array on the first value
For i = LBound(LbList, 1) To UBound(LbList, 1) - 1
For j = i + 1 To UBound(LbList, 1)
If LbList(i, 0) > LbList(j, 0) Then
'Swap the first value
sTemp = LbList(i, 0)
LbList(i, 0) = LbList(j, 0)
LbList(j, 0) = sTemp
'Swap the other values
sTemp2 = LbList(i, 1)
LbList(i, 1) = LbList(j, 1)
LbList(j, 1) = sTemp2
sTemp3 = LbList(i, 2)
LbList(i, 2) = LbList(j, 2)
LbList(j, 2) = sTemp3
sTemp4 = LbList(i, 3)
LbList(i, 3) = LbList(j, 3)
LbList(j, 3) = sTemp4
End If
Next j
Next i
'Remove the contents of the listbox
lstResults.Clear
'Repopulate with the sorted list
lstResults.List = LbList
End If
End With
Set LbList = Nothing
End Sub
Thanks - pic of screen to help you visualize....
Userform
This should work regardless of whether or not the list is empty
Dim i As Long
With lstResults
.AddItem lstSearch.List(plnt, 0)
i = .ListCount - 1
.List(i, 1) = lstSearch.List(plnt, 1)
.List(i, 2) = lstSearch.List(plnt, 2)
.List(i, 3) = lstSearch.List(plnt, 3)
End With

Format Condition in Conditional Formatting

I have a macro that has two parts. The first part is the upper portion that color codes based on keywords, the bottom portion highlights cells that are duplicates.
The first part has a format condition that makes it only work if the corresponding cell in column "D" is has a value of .6 or greater. I need the same thing to work for the second part.
The format condition is
FormatConditions.Add xlExpression, Formula1:="=AND(D1>0.6,ISNUMBER(SEARCH(""" & aKeyColors(i, 1) & """,G1)))"
Macro:
Sub oneSixColorCodingPluskey()
'
' oneSixColorCodingPluskey Macro
'
Dim wb As Workbook
Dim wsKey As Worksheet
Dim wsFees As Worksheet
Dim aKeyColors(1 To 29, 1 To 2) As Variant
Dim aOutput() As Variant
Dim sKeyShName As String
Dim i As Long, j As Long
Set wb = ActiveWorkbook
Set wsFees = wb.Sheets("Fees")
sKeyShName = "Color Coding Key"
On Error Resume Next
Set wsKey = wb.Sheets(sKeyShName)
On Error GoTo 0
If wsKey Is Nothing Then
Set wsKey = wb.Sheets.Add(After:=ActiveSheet)
wsKey.Name = sKeyShName
With wsKey.Range("A1:B1")
.Value = Array("Word", "Color")
.HorizontalAlignment = xlCenter
.Font.Bold = True
.Borders(xlEdgeBottom).LineStyle = xlContinuous
End With
Else
wsKey.Range("A2:B" & wsKey.Rows.Count).Clear
End If
aKeyColors(1, 1) = "Strategize": aKeyColors(1, 2) = 10053120
aKeyColors(2, 1) = "Coordinate": aKeyColors(2, 2) = 10053120
aKeyColors(3, 1) = "Develop": aKeyColors(3, 2) = 10053120
aKeyColors(4, 1) = "Draft": aKeyColors(4, 2) = 10053120
aKeyColors(5, 1) = "Organize": aKeyColors(5, 2) = 10053120
aKeyColors(6, 1) = "Finalize": aKeyColors(6, 2) = 10053120
aKeyColors(7, 1) = "Maintain": aKeyColors(7, 2) = 10053120
aKeyColors(8, 1) = "Prepare": aKeyColors(8, 2) = 10053120
aKeyColors(9, 1) = "Rework": aKeyColors(9, 2) = 10053120
aKeyColors(10, 1) = "Revise": aKeyColors(10, 2) = 10053120
aKeyColors(11, 1) = "Review": aKeyColors(11, 2) = 10053120
aKeyColors(11, 1) = "Analysis": aKeyColors(11, 2) = 10053120
aKeyColors(11, 1) = "Analyze": aKeyColors(11, 2) = 10053120
aKeyColors(12, 1) = "Follow Up": aKeyColors(12, 2) = 10053120
aKeyColors(12, 1) = "Follow-Up": aKeyColors(12, 2) = 10053120
aKeyColors(13, 1) = "Maintain": aKeyColors(13, 2) = 10053120
aKeyColors(14, 1) = "Address": aKeyColors(14, 2) = 10053120
aKeyColors(15, 1) = "Attend": aKeyColors(15, 2) = 10092441
aKeyColors(16, 1) = "Confer": aKeyColors(16, 2) = 10092441
aKeyColors(17, 1) = "Meet": aKeyColors(17, 2) = 16751103
aKeyColors(18, 1) = "Work With": aKeyColors(18, 2) = 16751103
aKeyColors(19, 1) = "Correspond": aKeyColors(19, 2) = 16750950
aKeyColors(20, 1) = "Email": aKeyColors(20, 2) = 16750950
aKeyColors(20, 1) = "E-mail": aKeyColors(20, 2) = 16750950
aKeyColors(21, 1) = "Phone": aKeyColors(21, 2) = 6697881
aKeyColors(22, 1) = "Telephone": aKeyColors(22, 2) = 6697881
aKeyColors(23, 1) = "Call": aKeyColors(23, 2) = 6697881
aKeyColors(24, 1) = "Committee": aKeyColors(24, 2) = 3394611
aKeyColors(25, 1) = "Various": aKeyColors(25, 2) = 32768
aKeyColors(26, 1) = "Team": aKeyColors(26, 2) = 13056
aKeyColors(27, 1) = "Print": aKeyColors(27, 2) = 10092543
aKeyColors(28, 1) = "Wip": aKeyColors(28, 2) = 65535
aKeyColors(29, 1) = "Circulate": aKeyColors(29, 2) = 39372
wsFees.Cells.FormatConditions.Delete
ReDim aOutput(1 To UBound(aKeyColors, 1), 1 To 2)
With wsFees.Columns("G")
For i = LBound(aKeyColors, 1) To UBound(aKeyColors, 1)
If WorksheetFunction.CountIf(.Cells, "*" & aKeyColors(i, 1) & "*") > 0 Then
j = j + 1
aOutput(j, 1) = aKeyColors(i, 1)
aOutput(j, 2) = aKeyColors(i, 2)
.FormatConditions.Add xlExpression, Formula1:="=AND(D1>0.6,ISNUMBER(SEARCH(""" & aKeyColors(i, 1) & """,G1)))"
.FormatConditions(.FormatConditions.Count).Interior.Color = aKeyColors(i, 2)
End If
Next i
End With
If j > 0 Then
wsKey.Range("A2").Resize(j, 1).Value = aOutput
For i = 1 To j
wsKey.Cells(i + 1, "B").Interior.Color = aOutput(i, 2)
Next i
wsKey.Columns("A").EntireColumn.AutoFit
End If
With wsFees.Columns("G")
.FormatConditions.AddUniqueValues
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).DupeUnique = xlDuplicate
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 192
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
End Sub
A formula-based CF using COUNTIFS() would likely do the job.
E.g.

Conditional Formatting Macro to Active Column

I currently have a macro that will apply conditional formatting to a cell (in this it is cell "G" but I think it would be better if I was able to have the macro apply the conditional formatting to whatever cell I have selected so that I am not limited to only the cell I have setup in the macro.
Sub ColorCoringPluskey()
'
' ColorCoringPluskey Macro
'
Dim wb As Workbook
Dim wsKey As Worksheet
Dim wsFees As Worksheet
Dim aKeyColors(1 To 20, 1 To 2) As Variant
Dim aOutput() As Variant
Dim sKeyShName As String
Dim i As Long, j As Long
Set wb = ActiveWorkbook
Set wsFees = wb.Sheets("Fees")
sKeyShName = "Color Coding Key"
On Error Resume Next
Set wsKey = wb.Sheets(sKeyShName)
On Error GoTo 0
If wsKey Is Nothing Then
Set wsKey = wb.Sheets.Add(After:=ActiveSheet)
wsKey.Name = sKeyShName
With wsKey.Range("A1:B1")
.Value = Array("Word", "Color")
.HorizontalAlignment = xlCenter
.Font.Bold = True
.Borders(xlEdgeBottom).LineStyle = xlContinuous
End With
Else
wsKey.Range("A2:B" & wsKey.Rows.Count).Clear
End If
aKeyColors(1, 1) = "Strategize": aKeyColors(1, 2) = 10053120
aKeyColors(2, 1) = "Coordinate": aKeyColors(2, 2) = 13421619
aKeyColors(3, 1) = "Committee": aKeyColors(3, 2) = 16777062
aKeyColors(4, 1) = "Attention": aKeyColors(4, 2) = 2162853
aKeyColors(5, 1) = "Work": aKeyColors(5, 2) = 5263615
aKeyColors(6, 1) = "Circulate": aKeyColors(6, 2) = 10066431
aKeyColors(7, 1) = "Numerous": aKeyColors(7, 2) = 13158
aKeyColors(8, 1) = "Follow up": aKeyColors(8, 2) = 39372
aKeyColors(9, 1) = "Attend": aKeyColors(9, 2) = 65535
aKeyColors(10, 1) = "Attention to": aKeyColors(10, 2) = 65535
aKeyColors(11, 1) = "Print": aKeyColors(11, 2) = 10092543
aKeyColors(12, 1) = "WIP": aKeyColors(12, 2) = 13056
aKeyColors(13, 1) = "Prepare": aKeyColors(13, 2) = 32768
aKeyColors(14, 1) = "Develop": aKeyColors(14, 2) = 3394611
aKeyColors(15, 1) = "Participate": aKeyColors(15, 2) = 10092441
aKeyColors(16, 1) = "Organize": aKeyColors(16, 2) = 13369548
aKeyColors(17, 1) = "Various": aKeyColors(17, 2) = 16751103
aKeyColors(18, 1) = "Maintain": aKeyColors(18, 2) = 16724787
aKeyColors(19, 1) = "Team": aKeyColors(19, 2) = 16750950
aKeyColors(20, 1) = "Address": aKeyColors(20, 2) = 6697881
wsFees.Cells.FormatConditions.Delete
ReDim aOutput(1 To UBound(aKeyColors, 1), 1 To 2)
With wsFees.Columns("G")
For i = LBound(aKeyColors, 1) To UBound(aKeyColors, 1)
If WorksheetFunction.CountIf(.Cells, "*" & aKeyColors(i, 1) & "*") > 0 Then
j = j + 1
aOutput(j, 1) = aKeyColors(i, 1)
aOutput(j, 2) = aKeyColors(i, 2)
.FormatConditions.Add xlTextString, String:=aKeyColors(i, 1), TextOperator:=xlContains
.FormatConditions(.FormatConditions.Count).Interior.Color = aKeyColors(i, 2)
End If
Next i
End With
If j > 0 Then
wsKey.Range("A2").Resize(j, 1).Value = aOutput
For i = 1 To j
wsKey.Cells(i + 1, "B").Interior.Color = aOutput(i, 2)
Next i
wsKey.Columns("A").EntireColumn.AutoFit
End If
End Sub
Thanks for any help!

Multidimensional Arrays with For Loops VBA

Trying to check column one for a value (column in the multidimensional array that is) and if it matches sort another column for the value that matches that row.
I think I am doing this wrong, but this is the first time I am messing with multidimensional arrays.
Would I need to use UBound and LBound in each for loop to tell it what colum to look through?
I am def interested in learning the best practice method for using this in the future, aside from just an answer/solution tot he current issue.
Code:
Private Sub ThisStuff()
Dim CoaAmt As Long
Dim COAArray(3, 2)
Dim ThisValue As String
Dim AnotherValue As String
AnotherValue = "Bananas"
ThisValue = "Apples"
COAArray(0, 0) = "Apples"
COAArray(1, 0) = "Oranges"
COAArray(2, 0) = "Peaches"
COAArray(3, 0) = "Pomegranets"
COAArray(0, 1) = 498
COAArray(0, 1) = 505
COAArray(1, 1) = 564
COAArray(1, 2) = 556
COAArray(2, 1) = 570
COAArray(2, 2) = 573
COAArray(3, 1) = 742
COAArray(3, 2) = 750
If AnotherValue = "Bananas" Then
For i = COAArray(0, 0) To COAArray(3, 0)
For j = COAArray(1, 0) To COAArray(3, 2)
If COAArray(i, j) = ThisValue Then CoaAmt = COAArray(i, j)
Next j
Next i
End If
MsgBox ("The value of CoaAmt is " & CoaAmt)
End Sub
Yes. The LBound and
UBound functions allow you to specify the rank. This lets your nested For .. Next loops to cycle through all array elements.
debug.print LBound(COAArray, 1) & ":" & UBound(COAArray, 1)
debug.print LBound(COAArray, 2) & ":" & UBound(COAArray, 2)
If AnotherValue = "Bananas" Then
For i = LBound(COAArray, 1) To UBound(COAArray, 1)
For j = LBound(COAArray, 2) To UBound(COAArray, 2)
If COAArray(i, j) = ThisValue Then CoaAmt = COAArray(i, j)
Next j
Next i
End If
Your array element assignment was a little messed up. It should have been closer to,
COAArray(0, 0) = "Apples"
COAArray(1, 0) = "Oranges"
COAArray(2, 0) = "Peaches"
COAArray(3, 0) = "Pomegranates"
COAArray(0, 1) = 498
COAArray(1, 1) = 505
COAArray(2, 1) = 564
COAArray(3, 1) = 556
COAArray(0, 2) = 570
COAArray(1, 2) = 573
COAArray(2, 2) = 742
COAArray(3, 2) = 750
For example, with the repaired array assignment above, COAArray(0, 0) is Apples, COAArray(0, 1) is 498 and COAArray(0, 2) is 570. The following spits out 498 and 570.
Dim i As Long, j As Long
Dim COAArray(3, 2) As Variant, CoaAmt(0 To 1) As Variant
Dim ThisValue As String, AnotherValue As String
AnotherValue = "Bananas"
ThisValue = "Apples"
COAArray(0, 0) = "Apples"
COAArray(1, 0) = "Oranges"
COAArray(2, 0) = "Peaches"
COAArray(3, 0) = "Pomegranets"
COAArray(0, 1) = 498
COAArray(1, 1) = 505
COAArray(2, 1) = 564
COAArray(3, 1) = 556
COAArray(0, 2) = 570
COAArray(1, 2) = 573
COAArray(2, 2) = 742
COAArray(3, 2) = 750
If AnotherValue = "Bananas" Then
For i = LBound(COAArray, 1) To UBound(COAArray, 1)
If COAArray(i, 0) = ThisValue Then
For j = LBound(COAArray, 2) + 1 To UBound(COAArray, 2)
CoaAmt(j - 1) = COAArray(i, j)
Next j
End If
Next i
End If
MsgBox "The value of CoaAmt is " & CoaAmt(LBound(CoaAmt)) & " " & CoaAmt(UBound(CoaAmt))
I had to change your CoaAmt var to a one-dimensioned variant array in order to collect both numbers and output them.

Resources