Referencing an Active-x control with variable in a loop - excel

I'm trying to create a variable for Check boxes in my worksheet so that I can refence a larger number of those in loop.
It looks like this:
The worksheet
The purpose is to make the schedule (Blue background) able to highlight the names you select with the checkboxes (On the left in the green list).
To do this I would like the loop to go through all of those checkboxes and see if they're true for the corresponding name on the same row. I've gotten this far:
Sub Test_ReplaceWithArray()
'State var
Dim Names(54) As String
Dim ChkBx As String
Dim Personal As Range
Dim n, m, i, j, ChkNr, numOfEmployees, numOfWeeks As Integer
'Set var
Set Personal = Range("A3:A55")
numOfEmployees = Application.WorksheetFunction.CountA(Personal)
numOfWeeks = Worksheets("Schemaläggning").numOfWeeksBox.Value
n = 1
m = 3
i = 3
j = 3
ChkNr = 1
ChkBx = ("CheckBox" & ChkNr)
'Fill array
Do Until n > numOfEmployees
Cells(i, 1).Select
If IsEmpty(ActiveCell) = False _
And ChkBx = True Then
Names(n) = ActiveCell.Value
i = i + 1
n = n + 1
ChkNr = ChkNr + 1
ElseIf IsEmpty(ActiveCell) = True Then
i = i + 1
n = n + 1
ChkNr = ChkNr + 1
End If
Loop
'Make Bold or Grey if in array
Do Until m > numOfWeeks + 2
Cells(m, j).Select
If j <= 7 And IsInArray(ActiveCell.Value, Names) = True Then
Selection.Font.Bold = True
j = j + 1
ElseIf j <= 7 And IsInArray(ActiveCell.Value, Names) = False Then
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.349986266670736
End With
j = j + 1
ElseIf j = 8 Then
j = 3
m = m + 1
End If
Loop
End Sub
The point is to have the name on the row (In the green list) put into an array and then look at each name in the schedule. If the name in the cell of the schedule is contained within the array the name will be boldened but if it is'nt it will be turned grey. I have allready made a comparison function to see if the names are contained in the array that works (tested it before continuing with the checkbox references).
But I get runtime error 13 at:
If IsEmpty(ActiveCell) = False _
And ChkBx = True Then
I definatly suspect that I don't know how to reference the control properly but I don't know what to do. Any help is appreciated.

Dim cCont As Control
For Each cCont In Me.Controls
If TypeName(cCont) = "CheckBox" Then
cCont.Value = False
End If
Next cCont
This is a sample of what I use to loop through all controls in a userform. Hopefully you can adjust it to your needs.
for worksheets objects:
Sub LoopListBoxes()
Dim OleObj As OLEObject
For Each OleObj In ActiveSheet.OLEObjects
If OleObj.progID = "Forms.ListBox.1" Then
MsgBox OleObj.Object.ListCount
End If
Next OleObj
End Sub
CheckBox = Forms.CheckBox.1
ComboBox = Forms.ComboBox.1
CommandButton = Forms.CommandButton.1
Frame = Forms.Frame.1
Image = Forms.Image.1
Label = Forms.Label.1
ListBox = Forms.ListBox.1
MultiPage = Forms.MultiPage.1
OptionButton = Forms.OptionButton.1
ScrollBar = Forms.ScrollBar.1
SpinButton = Forms.SpinButton.1
TabStrip = Forms.TabStrip.1
TextBox = Forms.TextBox.1
ToggleButton = Forms.ToggleButton.1
borrowed from:
http://www.ozgrid.com/forum/showthread.php?t=61068

Related

superscript letters in vba string variable

I am looking for how to super/subscript a letter/digit in a VBA string variable. I am working in excel with charts that have axes, titles and chart titles that require s-scripting. Additionally, there is a formula to go in a textbox:
Cpt = Cp0 * e^(-ket) where all the p's, t's and 0 are subscripts. The entire expression, (-ket) is superscripted with embedded subscripting for the e (the e between the k & t). Finally, all the specially formatted string variables will be copied to PowerPoint variables via clipboard/gettext.
Any help / guidance is greatly appreciated.
Pat K.
It is workaround Idea only and the code may not be useful for your purpose depending on source and destination of the data and may be treated as demo only. i have only used excel cells and Text Boxes on a sheet as destination and used PowerPoint Text Boxes as target.
The simple approach is that while picking up String from formatted cells/Text Boxes from excel to a variable, Font Subscript, Superscript information is also to be picked up in a parallel variable (here in a 2D Array). The same font information may be used while writing in PowerPoint. The demo idea have to be Modified/Converted to suit your need.
Demo Screen shot
The demo code
Sub Sscript()
Dim CellStr() As Variant
Dim Rng As Range, Cell As Range
Dim shp As Shape
Dim VarNo As Long, i As Long, j As Long, Txt As String, FntInfo As String
Set Rng = Range("C3:C7") 'Range used for collecting input data and font information for the variable
VarNo = 0
'loop used for Trial may be directly assigned to CellStr by increasing Varno by one for each cell
For Each Cell In Rng.Cells
VarNo = VarNo + 1
ReDim Preserve CellStr(1 To 2, 1 To VarNo)
Txt = Cell.Value
CellStr(1, VarNo) = Txt
FntInfo = ""
For i = 1 To Len(Txt)
If Cell.Characters(i, 1).Font.Subscript = True Then
FntInfo = FntInfo & "A"
ElseIf Cell.Characters(i, 1).Font.Superscript = True Then
FntInfo = FntInfo & "B"
Else
FntInfo = FntInfo & "C"
End If
Next i
CellStr(2, VarNo) = FntInfo
Next Cell
'again loop used for Trial may be directly assigned to CellStr from Textboxes in the sheet
For Each shp In ActiveSheet.Shapes
If shp.Type = msoTextBox Then
VarNo = VarNo + 1
ReDim Preserve CellStr(1 To 2, 1 To VarNo)
Txt = shp.TextFrame2.TextRange.Text
CellStr(1, VarNo) = Txt
FntInfo = ""
For i = 1 To Len(Txt)
If shp.TextFrame2.TextRange.Characters(i, 1).Font.Subscript = msoTrue Then
FntInfo = FntInfo & "A"
ElseIf shp.TextFrame2.TextRange.Characters(i, 1).Font.Superscript = msoTrue Then
FntInfo = FntInfo & "B"
Else
FntInfo = FntInfo & "C"
End If
Next i
CellStr(2, VarNo) = FntInfo
End If
Next
'Start of Trial code in excel to be deleted
For i = 1 To UBound(CellStr, 2)
ActiveSheet.Cells(i, 10).Value = CellStr(1, i)
ActiveSheet.Cells(i, 11).Value = CellStr(2, i)
FntInfo = CellStr(2, i)
For j = 1 To Len(FntInfo)
ActiveSheet.Cells(i, 10).Characters(j, 1).Font.Subscript = False
ActiveSheet.Cells(i, 10).Characters(j, 1).Font.Superscript = False
If Mid(FntInfo, j, 1) = "A" Then ActiveSheet.Cells(i, 10).Characters(j, 1).Font.Subscript = True
If Mid(FntInfo, j, 1) = "B" Then ActiveSheet.Cells(i, 10).Characters(j, 1).Font.Superscript = True
Next j
Next
'End of Trial code in excel to be deleted
'Powerpoint placement of data in powerpoint
Dim Pp As PowerPoint.Application
Dim Prs As Presentation
Dim Sld As Slide
Dim Pshp As Shape
Set Pp = CreateObject("Powerpoint.application")
Pp.Visible = True
Set Prs = Pp.Presentations.Open("C:\users\user\desktop\test.pptx")
Set Sld = Prs.Slides(1)
For i = 1 To UBound(CellStr, 2)
Set Pshp = Sld.Shapes(i)
Pshp.TextFrame.TextRange.Text = CellStr(1, i)
FntInfo = CellStr(2, i)
For j = 1 To Len(FntInfo)
Pshp.TextFrame.TextRange.Characters(j, 1).Font.Subscript = False
Pshp.TextFrame.TextRange.Characters(j, 1).Font.Superscript = False
If Mid(FntInfo, j, 1) = "A" Then Pshp.TextFrame.TextRange.Characters(j, 1).Font.Subscript = True
If Mid(FntInfo, j, 1) = "B" Then Pshp.TextFrame.TextRange.Characters(j, 1).Font.Superscript = True
Next j
Next
End Sub
It is suggested to Add reference of Microsoft PowerPoint Object Library and thanks for asking a good question/challenge to achieve something seemingly not possible but logically possible.
Edit: another more simplistic approach (the 1st half of the String variable contains actual string and 2nd half of the variable contains Font Info) with generalized functions is also added below
Sub Sscript2()
Dim Txt As String, Var1 As String, Var2 As String
Dim Addr As String
Var1 = GetVarFont("C6") ' 1st half of the var contains actual string and 2nd half contain font Info
Var2 = GetVarFont("C7") ' 1st half of the var contains actual string and 2nd half contain font Info
'Powerpoint placement of data in powerpoint
Dim Pp As PowerPoint.Application
Dim Prs As Presentation
Dim Sld As Slide
Dim Pshp As Object
Set Pp = CreateObject("Powerpoint.application")
Pp.Visible = True
Set Prs = Pp.Presentations.Open("C:\users\user\desktop\test.pptx")
Set Sld = Prs.Slides(1)
WriteShp Sld.Shapes(8).TextFrame.TextRange, Var1
WriteShp Sld.Shapes(9).TextFrame.TextRange, Var2
End Sub
Sub WriteShp(Ptxt As TextRange, VarX As String)
Dim i As Long
Ptxt.Text = Left(VarX, Len(VarX) / 2)
For i = 1 To Len(VarX) / 2
Ptxt.Characters(i, 1).Font.Subscript = False
Ptxt.Characters(i, 1).Font.Superscript = False
If Mid(VarX, Len(VarX) / 2 + i, 1) = "A" Then Ptxt.Characters(i, 1).Font.Subscript = True
If Mid(VarX, Len(VarX) / 2 + i, 1) = "B" Then Ptxt.Characters(i, 1).Font.Superscript = True
Next
End Sub
Function GetVarFont(Addr As String) As String
Dim Txt As String, i As Long
Txt = Range(Addr).Value
GetVarFont = Txt
For i = 1 To Len(Txt)
If Range(Addr).Characters(i, 1).Font.Subscript = True Then
GetVarFont = GetVarFont & "A"
ElseIf Range(Addr).Characters(i, 1).Font.Superscript = True Then
GetVarFont = GetVarFont & "B"
Else
GetVarFont = GetVarFont & "C"
End If
Next i
End Function

Change forecolor in Listview depending on value excel vba

Hope you can help me... I have a displayed data on my listview. The column Headers are:
ROW ID CUSTOMER PICKUP DELIVERY LOAD PLACE BAGS AMOUNT STATUS -total of 10 columns
I want the forecolor in my column STATUS depends on the value. the value is either PAID or UNPAID, if PAID the color should be green and if UNPAID, the color should be red.
I have this code, but it not working for me, somebody will help me? Thank you in advance.
Private Sub UserForm_Activate()
Dim C As Long
Dim i As Long
Dim R As Long
ListView1.View = lvwReport
ListView1.HideSelection = False
ListView1.FullRowSelect = True
ListView1.HotTracking = True
ListView1.HoverSelection = False
ListView1.ColumnHeaders.Add Text:="Row", Width:=40
For C = 1 To 12
ListView1.ColumnHeaders.Add Text:=Cells(1, C).Text
ComboBox1.AddItem Cells(1, C).Text
Next C
**' |In this part of my code is not working|**
Dim Item As ListItem
Dim counter As Long
For counter = 1 To listView1.ListItems.Count
Set Item = listView1.ListItems.Item(counter)
If Item.SubItems(10) = "Paid" Then
listView1.ListItems.Item(counter).ListSubItems(10).ForeColor = vbGreen
End If
If Item.SubItems(10) = "Unpaid" Then
listView1.ListItems.Item(counter).ListSubItems(10).ForeColor = VBRed
Next counter
End Sub
listsubitime must be 9, since index numbers start at 0.
In my test, it work well.
Private Sub UserForm_Activate()
Dim C As Long
Dim i As Long
Dim R As Long
Dim li As ListItem
ListView1.View = lvwReport
ListView1.HideSelection = False
ListView1.FullRowSelect = True
ListView1.HotTracking = True
ListView1.HoverSelection = False
ListView1.ColumnHeaders.Add Text:="Row", Width:=40
For C = 1 To 12
ListView1.ColumnHeaders.Add Text:=Cells(1, C).Text
ComboBox1.AddItem Cells(1, C).Text
Next C
Dim vDB
vDB = Range("a1").CurrentRegion
For i = 2 To UBound(vDB, 1)
Set li = ListView1.ListItems.Add
For j = 1 To UBound(vDB, 2)
With li
.Text = i
.ListSubItems.Add , , vDB(i, j)
End With
Next j
Next i
'**' |In this part of my code is not working|**
Dim Item As ListItem
Dim counter As Long
For counter = 1 To ListView1.ListItems.Count
Set Item = ListView1.ListItems.Item(counter)
If Item.SubItems(9) = "Paid" Then
ListView1.ListItems.Item(counter).ListSubItems(9).ForeColor = vbGreen
End If
If Item.SubItems(9) = "Unpaid" Then
ListView1.ListItems.Item(counter).ListSubItems(9).ForeColor = vbRed
End If
Next counter
End Sub

How to loop through checkboxes on dynamically created userform?

I'm trying to utilize a dynamically created userform and based on what boxes are check, gray out certain cells.
As background, this is for an injection molding facility. QA sets up the cavity numbers that are running. This dynamic userform creates checkboxes based on the cavity numbers that are input on the worksheet.
Option Explicit
Private Sub UserForm_Initialize()
Dim col As Long
Dim row As Long
Dim lcol As Long
Dim i As Long
Dim j As Long
Dim chkBox As MsForms.CheckBox
Dim l As MsForms.Frame
Dim t As MsForms.Label
Set l = Me.Controls.Add("Forms.Frame.1", "cavz", True)
l.Caption = "BLOCKED CAVITIES"
l.Height = 195
Set t = l.Controls.Add("Forms.Label.1", "mark")
t.Caption = "Mark all cavities that are currently blocked:"
t.Width = 175
t.Top = 10
col = 2 'Set your column index here
row = 8
lcol = 17
j = 1
For i = col To lcol
Set chkBox = l.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
If Worksheets("QA").Cells(row, i).Value <> "" Then
chkBox.Caption = Worksheets("QA").Cells(row, i).Value
ElseIf Worksheets("QA").Cells(row, i).Value = "" Then
GoTo 10
End If
If i <= 9 Then
'MsgBox "i = " & i
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
ElseIf i > 9 Then
j = j + 1
'MsgBox "j = " & j
chkBox.Left = 100
chkBox.Top = 5 + ((j - 1) * 20)
End If
10
Next i
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub CommandButton1_Click()
Dim x As Control
Dim cavz As MsForms.Frame
For Each x In cavz.Controls
If x.Value = True Then
If x.Value = Range("B8") Then
Range("B8:B14").Select
Selection.Interior.ColorIndex = 16
End If
End If
Next x
End Sub
I started out doing one thing, and somehow this is what my code turned into. It's not the neatest code, but I would still consider myself a novice at this. Plus, this is the first time I couldn't find the answer on my own and thus my first time asking for help. So, any help would be greatly appreciated!
Thanks!
UPDATE
This is great! Thank you Don. However, I do have a follow up question. Perhaps I've been banging my head against this for too long, but I cannot think of a more efficient way to do this. Here is what I'm now doing:
For i = 2 To 17
Set ctl = Controls.Item("CheckBox_" & i)
If ctl = True Then
If i = 2 Then
Range(Cells(j, c), Cells(m, c)).Select
Selection.Interior.ColorIndex = 16
Range(Cells(k, c), Cells(m, c)).Select
With Selection
.Merge
Cells(k, c) = "BLOCKED"
.Orientation = 90
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlCenter
End With
End If
Next i
etc
I'm in the process of writing if i = 1 through 17, but I feel like there's a more efficient way and I just can't crack it.
Loop through them by name the same way you created them.
Dim ctl As Control
For i = 2 To 17
Set ctl = Controls.Item("CheckBox_" & i)
Next 'i
Also, I would create module level constants for the initial values of col, lcol, etc. and reuse those in both routines.

in vba how to define array and compare them?

i have 2 sheets , i want to find the same rows in 2 sheets , so i put the first row in array , and by a for next i define the first array ...then i define another array from second sheet , then i compare them .... why it doesn't work?
Sub compare()
Dim n(4) As Variant
Dim o(4) As Variant
Dim i As Integer
For i = 3 To 20 'satrha
For j = 2 To 4 'por kardan
n(j) = Sheets("guys").Cells(i, j)
Next 'por kardan
k = 3
Do 'hhhh
For Z = 2 To 4 'por dovomi
o(Z) = Sheets("p").Cells(k, Z)
Next 'por dovomi
If n(j) = o(Z) Then
Sheets("guys").Cells(i, 1) = Sheets("p").Cells(k, 2)
flag = True
Else
flag = False
k = k + 1
End If
Loop Until flag = False 'hhhhh
Next 'satrha
End Sub
Guessing from your existing code, my following code will copy the value from sheet "p" column B into sheet "guys" column A when a match is found.
Sub compare()
Dim i As Integer
Dim j As Integer
Dim l As Integer
l = Sheets("p").Range("B65535").End(xlUp).Row
Debug.Print l
For i = 3 To 20
For j = 3 To l
If Sheets("guys").Cells(i, 2).Value = Sheets("p").Cells(j, 2).Value And _
Sheets("guys").Cells(i, 3).Value = Sheets("p").Cells(j, 3).Value And _
Sheets("guys").Cells(i, 4).Value = Sheets("p").Cells(j, 4).Value Then
Sheets("guys").Cells(i, 1).Value = Sheets("p").Cells(j, 2).Value
Exit For
End If
Next
Next
End Sub
Noted that I explicitly said Value in my code. That will copy the computed value (e.g. result of a formula) instead of the "original" content.

Search and Sum based on selection

I tried Sum, CountIf, Dsum, SumProduct
I have a Userform with a ComboBox "History_Select_Debtor". The RowSource for the ComboBox is "Debtor_list_Debtors" - A Dynamic Named Range on WorkSheet "DebtorList". It consists of Customer Names from A2:A24 but will grow eventually.
The UserForm also Has a Textbox for Total Items Purchased Named "txtPurchased".
With each Transaction a Record is saved on Worksheet "InvoiceList" which consists of 7 Columns.
Each of these Columns have Dynamic Named Ranges
A = "Debtor" (Invoice_list_Debtor)
B = "Item" (Invoice_list_Item)
C = "Price" (Invoice_list_Price)
D = "Date" (Invoice_list_Date)
E = "Time" (Invoice_list_Time)
F = "Balance" (Invoice_list_Balance)
G = "Payed" (InvoiceList_Payed)
The Record Saved in the Item Column is Text;
"Payed Balance","Added Balance","Quarter Item","Half Item","1 Item" - "10 Items"
I need to, "Based on the combo selection (History_Select_Debtor)", Reference that Particilar Debtor with "InvoiceList", sum up the total Number of Purchases and display that Value in "txtPurchased".
I need a specific Value to be assigned to each Item e.g. "Quarter Item" = 0.25 or "5 Item = 5".
If as an example "Adrian" has 7 Transactions recorded on InvoiceList
Added Balance
Quarter Item
Half Item
Quarter Item
10 Items
4 Items
Payed Balance
The Value to be displayed in "txtPurchased" would be "15".
I've a Macro that sums up the total Purchases;
It sums up the Total Row rather than just whichever Debtor is Selected in "History_Select_Debtor"
'-------Total Transactions----------------------------------------------------------------------
Set ws = Worksheets("DebtorList")
With Me
'Starting point of lookup data
Rw = .History_Select_Debtor.ListIndex + 2
History_Select_Debtor.List = Range("Debtor_list_Debtors").Value
txtTransactions.Value = Application.CountIf(Range("Invoice_list_Debtor"), History_Select_Debtor)
End With
'-----------------------------------------------------------------------------------------------
Another Macro I've made which also won't work;
=SUM(IF(Invoice_list_Item="Quarter Item",0.25,0)+IF(Invoice_list_Item="Half Item",0.5,0)+IF(Invoice_list_Item="1 Item",1,0)+IF(Invoice_list_Item="2 Items",2,0)+IF(Invoice_list_Item="3 Items",3,0)+IF(Invoice_list_Item="4 Items",4,0)+IF(Invoice_list_Item="5 Items",5,0)+IF(Invoice_list_Item="10 Items",10,0))
The Issue with this one is that given I use the Invoice_list_Debtor as the RowSource for my ComboBox I end up with over 170 duplicate Names.
Here is the Source Code to the Page I need to code to work on;
Public ListTable As Long
Private Sub UserForm_Initialize()
History_Select_Debtor.List = Range("Debtor_list_Debtors").Value
History_Select_Debtor = ""
Label6.Visible = False
Label7.Visible = False
Label8.Visible = False
Label9.Visible = False
Label10.Visible = False
Label11.Visible = False
Label12.Visible = False
Dim ws As Worksheet
Set ws = Worksheets("InvoiceList")
ListTable = ws.Range("A65536").End(xlUp).Row
Me.ListBox1.List = Range("A2:G" & ListTable).Value
Me.ListBox1.Clear
Me.ListBox1.ColumnWidths = "50;80;70;100;80;80;80"
'-----------Listview--------------------------------------------------------------------------------------------------------------
'Dim ws As Worksheet
'Dim lngRow As Long
'Dim lvwItem As ListItem
'Dim lngEndCol As Long
'Dim lngCol As Long
'Dim lngEndRow As Long
'Dim lngItemIndex As Long
'Dim blnHeaders() As Boolean
'Dim Rw As Long
'Set ws = Worksheets("InvoiceList")
'lngEndCol = ws.Range("A1").End(xlToRight).Column
'lngEndRow = ws.Range("A1").End(xlDown).Row
'ListView1.Gridlines = True
'lngRow = 1
'With ListView1
'.View = lvwReport
'For lngCol = 1 To lngEndCol
'.ColumnHeaders.Add , , ws.Cells(lngRow, lngCol).Text, ws.Columns(lngCol).ColumnWidth + 59.6
'.BackColor = vbBlack
'Next
'For lngRow = 2 To lngEndRow
'lngCol = 1
'lngItemIndex = 0
'Set lvwItem = .ListItems.Add(, , (ws.Cells(lngRow, lngCol).Text))
'For lngCol = 2 To lngEndCol
'lngItemIndex = lngItemIndex + 1
'lvwItem.SubItems(lngItemIndex) = Format(ws.Cells(lngRow, lngCol).Text, ws.Cells(lngRow, lngCol).NumberFormat) 'Adds Value from Current Row and Column 1
'Next
'Next
'.TextBackground = lvwTransparent
'End With
'-----------Listview--------------------------------------------------------------------------------------------------------------
'-----------ChartSpace---------------------------------------------------
Dim ChtSpc As OWC11.ChartSpace
Dim cht As OWC11.ChChart
Dim Sps As OWC11.Spreadsheet
Dim owcChart As OWC11.ChartSpace
Dim Balance As String
Balance = Range("B1").Value
Set owcChart = Me.ChartSpace1
Set ChtSpc = Me.ChartSpace1
Set Sps = Me.Spreadsheet1
Set ws = ThisWorkbook.Worksheets("DebtorList") ' change to you worksheet name
Sps.Range("A1:B100") = ws.Range("A1:B100").Value ' Set worksheet range to sheet control range
Set ChtSpc.DataSource = Sps ' set sheet control as chart control source
Set cht = ChtSpc.Charts.Add ' Add blank chart
With cht ' Set data for chart
.SetData chDimCategories, 0, "A2:A25" ' change to your category range
.SeriesCollection(0).SetData chDimValues, 0, "B2:B25" ' change to your series 1 range
'.PlotArea.FlipHorizontal
'.PlotArea.FlipVertical
'.PlotArea.RotateClockwise
'.SeriesCollection.Add
'.SeriesCollection(1).SetData chDimValues, 0, "A1:A24" ' change to your series 2 range
'By changing the layout we can control how the charts are presented
'inside the Chart space.
.Interior.Color = RGB(0, 0, 0)
.Border.Color = vbWhite
.Border.Weight = Thick
'.Type = chChartTypeColumn3D
'.Type = chChartTypeAreaStacked
End With
Me.Spreadsheet1.Visible = False ' hide the sheet control
'Set up the charts and manipulate some of their properties.
With owcChart.Charts(0)
'The data reference must be of the datatype string.
'The last parameter specify if each row represent a serie or not.
'.HasTitle = True
With .PlotArea
.Interior.Color = RGB(0, 0, 0)
'.Border.Color = RGB(255, 255, 255)
'.Border.DashStyle = chLineSolid
'.Border.Weight = Thick
End With
'With .Title
'.Caption = Balance
'.Font.Name = "Verdana"
'.Font.Size = 10
'.Font.Bold = True
'.Font.Color = RGB(50, 205, 50)
'End With
With .Axes(0).Font
.Name = "Verdana"
.Size = 8
'.Bold = True
.Color = RGB(255, 255, 255)
End With
With .Axes(1).Font
.Name = "Verdana"
.Size = 8
'.Bold = True
.Color = RGB(255, 255, 255)
End With
'With .Axes(0).MinorGridlines
'.Line.Color = RGB(255, 255, 255)
'End With
'With .Axes(0).MajorGridlines
'.Line.Color = RGB(255, 255, 255)
'End With
'With .Axes(1).MinorGridlines
'.Line.Color = RGB(255, 255, 255)
'End With
'With .Axes(1).MajorGridlines
'.Line.Color = RGB(255, 255, 255)
'End With
With .SeriesCollection(0)
'.Border.Color = RGB(255, 255, 255)
.Interior.Color = vbGreen
.Caption = Balance
.Line.Color = RGB(255, 255, 255)
End With
'With .SeriesCollection(1)
'.Interior.Color = vbBlue
'.Caption = Balance
'End With
'.HasLegend = True
'With .Legend
'.Position = chLegendPositionBottom
'.Border.Color = vbWhite
'.LegendEntries(2).Visible = False
'End With
End With
'------------------------------------------------------------------------
End Sub
Private Sub cmdClose_History_Click()
Unload Me
frmMenu.Show
End Sub
Private Sub History_Select_Debtor_Change()
'--------Total Purchased-----------------------------------------------
'Worksheets("InvoiceList").Rows(1).AutoFilter Field:=1, Criteria1:="=" & Me.History_Select_Debtor
'Me.txtPurchased = Worksheets("Summary").[C2] 'the cell containing the SUBTOTAL
'-------------------------------------------------------
Label6.Visible = True
Label7.Visible = True
Label8.Visible = True
Label9.Visible = True
Label10.Visible = True
Label11.Visible = True
Label12.Visible = True
FilterList 0, Me.History_Select_Debtor.Text
Me.cmdClose_History.SetFocus
Dim ws As Worksheet
Dim Rw As Long
Set ws = Worksheets("DebtorList")
'Get row based on ComboBox ListIndex
With Me
'Starting point of lookup data
Rw = .History_Select_Debtor.ListIndex + 2
'Data to be displayed based on selection
txtBalance.Value = FormatCurrency(Expression:=ws.Cells(Rw, 2).Value, _
NumDigitsAfterDecimal:=2)
End With
'-------Total Transactions----------------------------------------------------------------------------------------------------------------------
Set ws = Worksheets("DebtorList")
With Me
'Starting point of lookup data
Rw = .History_Select_Debtor.ListIndex + 2
History_Select_Debtor.List = Range("Debtor_list_Debtors").Value
txtTransactions.Value = Application.CountIf(Range("Invoice_list_Debtor"), History_Select_Debtor)
End With
'-------Total Payed------------------------------------------------------------------------------------------------------------------------------
txtPayed.Value = FormatCurrency(Expression:=Application.SumIf(Range("Invoice_list_Debtor"), _
History_Select_Debtor.Value, Range("Invoice_list_Price")), _
NumDigitsAfterDecimal:=2)
End Sub
Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
' Prevents use of the Close button
If CloseMode = vbFormControlMenu Then
Cancel = True
End If
End Sub
Private Sub FilterList(iCtrl As Long, sText As String)
Dim iRow As Long
Dim ws As Worksheet
Dim sCrit As String
sCrit = "*" & UCase(sText) & "*"
Set ws = Worksheets("InvoiceList")
With Me.ListBox1
ListTable = ws.Range("A65536").End(xlUp).Row
.List = ws.Range("A2:G" & ListTable).Value
For iRow = .ListCount - 1 To 0 Step -1
If Not UCase(.List(iRow, iCtrl)) Like sCrit Then
.RemoveItem iRow
End If
Next iRow
'Determine number of columns
.ColumnCount = 7
'Set column widths
.ColumnWidths = "50;80;70;100;80;80;80"
'Insert the range of data supplied
For x = 2 To 3 'loop the numeric columns - 3 to 4
For i = 0 To .ListCount - 1 'loop through the rows of columns 3 to 5
.List(i, x) = Format(.List(i, x), "$#,##")
Next i
Next x
For x = 5 To 6 'loop the numeric columns - 4 to 5
For i = 0 To .ListCount - 1 'loop through the rows of columns 3 to 5
.List(i, x) = Format(.List(i, x), "$#,##")
Next i
Next x
For x = 4 To 4 'loop the numeric columns - 3 to 4
For i = 0 To .ListCount - 1 'loop through the rows of columns 3 to 5
.List(i, x) = Format(.List(i, x), "[$-409]h:mm AM/PM;#")
Next i
Next x
End With
End Sub
There is more than one issue here I believe ...
To get the total number of invoiced items for a debtor you can
(auto)filter the InvoiceList for your current Debtor
display the sum of invoiced items using the =SUBTOTAL(109,InvoiceSheet!$F:$F) worksheet function (asuming the invoice sheet is named [InvoiceSheet] ;-) )
I would even suggest to have that =SUBTOTAL on a seperate sheet (Sheet2), so it's location is constant. Don't use ControlSource() on the text field in the dialog, but set Locked = True
You can set up Autofilter on [InvoiceSheet] once and use the Sub
Private Sub History_Select_Debtor_Change()
Worksheets("InvoiceSheet").Rows(1).AutoFilter field:=1, Criteria1:="=" & Me.History_Select_Debtor
Me.txtPurchased = Worksheets("Sheet2").[A1] 'the cell containing the SUBTOTAL
End Sub
to fire the filter and get the value of the SUBTOTAL formula back into the dialog.
For the transition of quantities from text to number I would suggest to create an extra sheet [QTYCode] looking like
A B ...
+------------+-----+----
1 |Text |Value|
2 |Quarter item| 0.25|
3 |Half item | 0.5|
4 |1 item | 1|
5 |2 item | 2|
6 |3 item | 3|
...
where column A (except header row) serves as RowSource() for the QTY selection box, and for each record you create in [InvoiceSheet] you save not only the selected QTYText, but as well an extra column containing a =VLOOKUP() formula that converts text into value (and base your =SUBTOTAL() on that new column - of course)
Hope that helps
Good luck - MikeD

Resources