I've got a listbox of 11 columns. When I try to add data to one of the columns, I get an error.
ListBox1.Column(10, j) = shtG.Cells(k, 13)
I don't understand why this happens, the listbox on the userform has a ColumnCount of 11.
The error I'm getting:
"Run-time error 380: Unable to set Column property. Invalid property value."
The value of the selected cell is "Group 16".
More info:
Code:
'adding this doesn't help
ListBox1.Clear
ListBox1.ColumnCount = 20
While shtG.Cells(k, 1) <> ""
If 'some long working condition Then
frmTP.ListBox1.AddItem (shtG.Cells(k, kolID))
frmTP.ListBox1.Column(1, j) = shtG.Cells(k, kolVnm) & strSpace & shtG.Cells(k, kolTV) & strSpace & shtG.Cells(k, kolAnm)
frmTP.ListBox1.Column(2, j) = shtG.Cells(k, 5)
frmTP.ListBox1.Column(3, j) = shtG.Cells(k, 6)
frmTP.ListBox1.Column(4, j) = shtG.Cells(k, 7)
frmTP.ListBox1.Column(5, j) = shtG.Cells(k, 8)
frmTP.ListBox1.Column(6, j) = shtG.Cells(k, 9)
frmTP.ListBox1.Column(7, j) = shtG.Cells(k, 10)
frmTP.ListBox1.Column(8, j) = shtG.Cells(k, 11)
frmTP.ListBox1.Column(9, j) = shtG.Cells(k, 12)
frmTP.ListBox1.Column(10, j) = shtG.Cells(k, 13)
j = j + 1
End If
k = k + 1
Wend
This is the sort of thing I mean (you could improve performance by loading the sheet data into an array to begin and processing that, and not resizing the array so often, but it would distract from the key idea here!):
Dim vData()
j = 0
While shtG.Cells(k, 1) <> ""
If 'some long working condition Then
ReDim Preserve vData(0 To 10, 0 To j)
vData(0, j) = shtG.Cells(k, kolID).Value
vData(1, j) = shtG.Cells(k, kolVnm) & strSpace & shtG.Cells(k, kolTV) & strSpace & shtG.Cells(k, kolAnm)
vData(2, j) = shtG.Cells(k, 5)
vData(3, j) = shtG.Cells(k, 6)
vData(4, j) = shtG.Cells(k, 7)
vData(5, j) = shtG.Cells(k, 8)
vData(6, j) = shtG.Cells(k, 9)
vData(7, j) = shtG.Cells(k, 10)
vData(8, j) = shtG.Cells(k, 11)
vData(9, j) = shtG.Cells(k, 12)
vData(10, j) = shtG.Cells(k, 13)
j = j + 1
End If
Wend
frmTP.ListBox1.Column = vData
Related
Looking for some help using both COUNTIF and INSTR to determine the number of separate occurrences of a string in a set of data. I have the COUNTIF statements working for the cell values, but I am now trying to dig into the cells, and identify how many instances of a particular string occur across the entire column.
The code I have is as follows:
j = 2
Cells(2, 11) = "Active, non-corresp add"
Cells(3, 11) = "No start date of res"
Cells(4, 11) = "Invalid address"
Cells(5, 11) = "Active ID, no country"
Cells(6, 11) = "Invalid address format"
Cells(7, 11) = "Invalid characters in address"
While Cells(j, 11) <> vbNullString
s = WorksheetFunction.CountIf(Worksheets("Addresses Master").Range("N:N"), Cells(j, 11))
If s <> 0 Then
Cells(j, 12) = s
End If
t = WorksheetFunction.CountIfs(Worksheets("Addresses Master").Range("N:N"), Cells(j, 11), Worksheets("Addresses Master").Range("I:I"), 1)
If t <> 0 Then
Cells(j, 13) = t
End If
u = WorksheetFunction.CountIfs(Worksheets("Addresses Master").Range("N:N"), Cells(j, 11), Worksheets("Addresses Master").Range("I:I"), 0)
If u <> 0 Then
Cells(j, 14) = u
End If
k = 15
If Cells(j, 11) = "Review address" Then
p = 0
Else
p = 1
End If
While k <= 19
v = WorksheetFunction.CountIfs(Worksheets("Addresses Master").Range("N:N"), Cells(j, 1), Worksheets("Addresses Master").Range("I:I"), p, Worksheets("Addresses Master").Range("C:C"), Cells(1, k))
If v <> 0 Then
Cells(j, k) = v
v = 0
End If
k = k + 1
Wend
j = j + 1
s = 0
t = 0
u = 0
Wend
In the cells being searched, there could be a combination of the 6 strings that I am looking for (Cells 2 - 7).
Edit: clarified title
Resolved the issue through the use of wildcards.
Instead of trying to use something like InStr, the use of an asterisk before and after the cell location being searched for allows for the Countif function to search inside the cell.
i.e. "*" & cells(j,11) & "*"
The code then becomes:
j = 2
Cells(2, 11) = "Active, non-corresp add"
Cells(3, 11) = "No start date of res"
Cells(4, 11) = "Invalid address"
Cells(5, 11) = "Active ID, no country"
Cells(6, 11) = "Invalid address format"
Cells(7, 11) = "Invalid characters in address"
While Cells(j, 11) <> vbNullString
s = WorksheetFunction.CountIf(Worksheets("Addresses Master").Range("N:N"), "*" & Cells(j, 11) & "*")
If s <> 0 Then
Cells(j, 12) = s
End If
k = 13
p = 1
While k <= 19
v = WorksheetFunction.CountIfs(Worksheets("Addresses Master").Range("N:N"), "*" & Cells(j, 11) & "*", Worksheets("Addresses Master").Range("I:I"), p, Worksheets("Addresses Master").Range("C:C"), Cells(1, k))
If v <> 0 Then
Cells(j, k) = v
v = 0
End If
k = k + 1
Wend
j = j + 1
s = 0
t = 0
u = 0
Wend
I have this bit of my code which takes like 90 % of the runtime.
There are about 8000 rows and information are stored in column A. This bit of code is splitting this information in the other columns.
It takes approximately 15 mins to run ( :O ).
Any suggestions on how to improve the performance ?
For i = 2 To Row_Number ' Loop for each row
If InStr(Cells(i, 1), "//") = 0 Then ' This means that if // appears somewhere in the text we delete all the rows (including this one) (see Else :) and stop the loop
j = 1
Do Until Mid(Cells(i, 1), j, 1) = ";"
j = j + 1
Loop
LongVIN = Mid(Cells(i, 1), 1, j - 1)
k = j
j = j + 1
Do Until Mid(Cells(i, 1), j, 1) = ";"
j = j + 1
Loop
Cells(i, 3) = Mid(Cells(i, 1), k + 1, j - k - 1) ' Model
k = j
j = j + 1
Do Until Mid(Cells(i, 1), j, 1) = ";"
j = j + 1
Loop
Cells(i, 4) = Mid(Cells(i, 1), k + 1, j - k - 1) ' Dealer
k = j
j = j + 1
Do Until Mid(Cells(i, 1), j, 1) = ";"
j = j + 1
Loop
k = j
j = j + 1
Do Until Mid(Cells(i, 1), j, 1) = ";"
j = j + 1
Loop
Cells(i, 6) = Mid(Cells(i, 1), k + 1, j - k - 1) ' Region
k = j
j = j + 1
Do Until Mid(Cells(i, 1), j, 1) = ";"
j = j + 1
Loop
Cells(i, 7) = CDate(Mid(Cells(i, 1), k + 1, j - k - 1)) ' Retail Date
k = j
Cells(i, 5) = Mid(Cells(i, 1), k + 1, Len(Cells(i, 1)) - k) '(Len - (k+1) +1) Dealer Name
Cells(i, 1) = Mid(LongVIN, 1, 10)
Cells(i, 2) = Mid(LongVIN, 11, 7)
Else:
Range("A" & i & ":A" & Row_Number).Delete 'ClearContents
Exit For
End If
Next i
You should see a significant boost in performance by storing the data in an array, operating on the array, and storing the data back into the spreadsheet.
Something like:
Dim data As Variant
Dim result As Variant
data = Range(Cells(2, 1), Cells(Row_Number, 1))
Redim result (1 To Row_Number, 1 To 7) As Variant
Now instead of reading from Cells(i, 1), you read from data(i, 1) and instead of writing to Cells(i, n) you write to result(i, n).
And at the end of your code:
Range(Cells(2, 1), Cells(Row_Number, 7)) = result
Dim chessboard(7, 7) As Integer
For i = 0 To chessboard.GetUpperBound(0)
For j = 0 To chessboard.GetUpperBound(0)
If (i + j) mod 2 = Then
Console.out.Write(chessboard(i, j) = "B")
Else
Console.out.WriteLine(chessboard(i,j)="W")
End If
Next
Next
Console.in.ReadLine()
Because you are new at this...
Dim chessboard(7, 7) As String
For i As Integer = 0 To chessboard.GetUpperBound(0)
For j As Integer = 0 To chessboard.GetUpperBound(1)
If ((i + j) And 1) = 0 Then
chessboard(i, j) = "B"
Console.Write(chessboard(i, j))
Else
chessboard(i, j) = "W"
Console.Write(chessboard(i, j))
End If
Next
Console.WriteLine("")
Next
I'm trying VLookup until the cells in Column A are empty and it is not working and is returning Run-time error '1004' Unable to get the VLookup property of the WorksheetFunction class. Any help? Or is there a better loop that I can use.
Sub FindOldValue()
Dim oldvalue As String
Dim result As String
i = 2
j = 1
K = 2
l = 3
Do
oldvalue = Worksheets("Products").Cells(i, j) & Worksheets("Products").Cells(i, K) & "delete"
result = Application.WorksheetFunction.VLookup(oldvalue, Worksheets("Raw Delta").Range("A:H"), 7, 0)
Worksheets("Products").Cells(i, l) = result
i = i + 1
j = j + 1
K = K + 1
Loop Until Worksheets("Products").Cells(i, 1) = ""
End Sub
Have you tried using only Application.Vlookup instead of Application.Worksheetfunction.vlookup in the line
result = Application.WorksheetFunction.VLookup(oldvalue, Worksheets("Raw Delta").Range("A:H"), 7, 0)
?
How about changing the loop to something like this?
Do
oldvalue = Worksheets("Products").Cells(i, j) & Worksheets("Products").Cells(i, K) & "delete"
resultrow = Application.Match(oldvalue, Worksheets("Raw Delta").Columns(1), 0)
If Not IsError(resultrow) Then
Worksheets("Products").Cells(i, l) = Worksheets("Raw Delta").Cells(resultrow,7).Value
End If
i = i + 1
j = j + 1
K = K + 1
Loop Until Worksheets("Products").Cells(i, 1) = ""
The below is a snippet of the code I'm using. I'm having a problem with how I need to name j. I need it to be 3,4,5,6 for the first tab_name and then 7,8,9,10 for the next and 11,12,13,14 for the one after that etc.
Can I improve the way I've attempted below?
tab_names = Array("11EB", "11WB", "12EB", "12WB", "13EB", "13WB", "14EB")
Location = Array(3, 7, 11)
For Each indiv_tab In tab_names
For Each j In Location
For i = 9 To 24
Sheets("Front Page").Cells(2, 2) = Cells(i, 1)
Cells(i, j) = Sheets(indiv_tab).Cells(2993, 9)
Cells(i, j + 1) = Sheets(indiv_tab).Cells(2993, 22)
Cells(i, j + 2) = Sheets(indiv_tab).Cells(2993, 35)
Cells(i, j + 3) = Sheets(indiv_tab).Cells(2993, 48)
Next i
Next j
Next
EDIT
I'm now using the below code, however, I need it go Next tab_name and Next j at the same time. Is there anyway to do this?
tab_names = Array("11EB", "11WB", "12EB", "12WB", "13EB", "13WB", "14EB", "14WB", "15NB", "15SB", "16NB", "16SB", "17EB", "17WB", "18EB", "18WB", "19NB", "19SB", "20NB", "20SB", "21NB", "21SB", "22NB", "22SB", "23NB", "23SB", "24NB", "24SB", "25NB", "25SB", "26NB", "26SB", "27EB", "27WB", "28EB", "28WB", "29EB", "29WB", "30EB", "30WB", "31NB", "31SB", "32NB", "32SB", "33EB", "33WB", "34EB", "34WB", "35NB", "35SB", "36NB", "36SB", "37EB", "37WB", "38NB", "38SB", "39NB", "39SB", "40EB", "40WB", "41EB", "41WB", "A12NB", "A12SB", "M11NB", "M11SB", "M25NB", "M25SB", "A120EB", "A120WB", "A120AEB", "A120AWB")
For i = 9 To 24
For Each indiv_tab In tab_names
For j = 3 To 291 Step 4
Sheets("Front Page").Cells(2, 2) = Cells(i, 1)
Cells(i, j) = Sheets(indiv_tab).Cells(2993, 9)
Cells(i, j + 1) = Sheets(indiv_tab).Cells(2993, 22)
Cells(i, j + 2) = Sheets(indiv_tab).Cells(2993, 35)
Cells(i, j + 3) = Sheets(indiv_tab).Cells(2993, 48)
Next j
Next
Next i
Thanks for any help.
Do you mean you want j to iterate through 3, 4, 5, 6 on the first tab, then 7, 8, 9, 10 on the second etc...?
If so, the below should work. Start with location as I've specified (declare as a new variable if you use it elsewhere), then manually adjust it each time.
tab_names = Array("11EB", "11WB", "12EB", "12WB", "13EB", "13WB", "14EB")
Location = Array(3, 4, 5, 6) '##changed this
For Each indiv_tab In tab_names
For Each j In Location
For i = 9 To 10
Sheets("Front Page").Cells(2, 2) = Cells(i, 1)
Cells(i, j) = Sheets(indiv_tab).Cells(2993, 9)
Cells(i, j + 1) = Sheets(indiv_tab).Cells(2993, 22)
Cells(i, j + 2) = Sheets(indiv_tab).Cells(2993, 35)
Cells(i, j + 3) = Sheets(indiv_tab).Cells(2993, 48)
Next i
Next j
'adjust values on each loop
For i = 0 To UBound(Location, 1)
Location(i) = Location(i) + 4
Next i
Next