I need to know how to move an element in an array to the last position.
Dim lastElement As String = strChar(UBound(strChar)) 'J.
For i As Integer = 0 To characters.Count - 1
If characters(i).actor = searchName And characters(i).title = searchMovie Then
For j = UBound(strChar) To LBound(strChar) + strChar.Count - 1 Step -1
strChar(j) = strChar(j - 1)
Next
End If
Next
strChar(LBound(strChar)) = lastElement
So here I have a structure characters and an array strChar.
I looked this code up on the internet and can't figure it out. It won't move the position of the element to the last or the first, but copies an element to the top.
I'm supposed to be deleting an element, but first I have to move the element before redim preserving it.
I wish I could use an arraylist, but i can't because i'm not supposed to for school.
A trick to this is that when you're re-arranging elements in an array, you have to save an entry somewhere outside the array, or you're going to overwrite one.
If it's legal to simply swap this element with what is currently the last element, then:
dim temp = characters(i)
characters(i) = characters(characters.length-1)
characters(characters.length-1) = temp
If you have to preserve the relative order:
dim temp = characters(i)
for j = i to characters.length-2
characters(j)=characters(j+1)
next
characters(characters.length-1) = temp
So I figured the best way to do this is to copy last item in the array to the position of the item i'm trying to delete, then redim preserve the array so the bottom item is deleted. essentially, i'm making a copy of an element to the position i'm trying to delete, and getting rid of that bottom element.
Dim searchName As String = txtActorName.Text
Dim searchMovie As String = txtMovieTitle.Text
For i As Integer = 0 To characters.Count - 1
If searchName = characters(i).actor And searchMovie = characters(i).title Then
characters(i) = characters(characters.Count - 1)
End If
Next
ReDim Preserve characters(characters.Count - 2)
i'm using a structure array as you can see here.
Related
I am using the WorksheetFunction.Large and WorksheetFunction.CountIf commands to determine the closest "jaw size" using a 1-Dimensional array as the source data, shown below.
wsSheet.Range("H2").Value = WorksheetFunction.Large(myArray, WorksheetFunction.CountIf(myArray, ">" & SizePush) + 1)
The problem I am having is when I use whole numbers (1, 2, 3, 4) the resulting jaw size does not take the closest value from the array, it takes the second closest value. The array I am using is shown in image 1 (myArray), and 'SizePush' refers to the following equation: (Start Diameter - (Start Diameter - End Diameter))-0.05.
a snippet of the jaw size array
I have attached the code that I am using. If anyone can help that would be greatly appreciated because I cannot figure out why only whole numbers cause an issue.
Dim StartDiam, EndDiam, PReduction, Push1, Push2, Push3, Push4, SizePush
StartDiam = 0.5
EndDiam = 4.75
PReduction = Worksheets("Sheet1").Range("D2").Value
Push1 = Worksheets("Sheet1").Range("I2").Value
Push2 = Worksheets("Sheet1").Range("I3").Value
Push3 = Worksheets("Sheet1").Range("I4").Value
Push4 = Worksheets("Sheet1").Range("I5").Value
SizePush = Worksheets("Sheet1").Range("I6").Value
Dim myArray
Set myArray = Range("T2:T51")
Dim wsSheet As Worksheet
Set wsSheet = Worksheets("Sheet1")
If StartDiam < wsSheet.Range("B2").Value Then
If EndDiam > wsSheet.Range("C2").Value Then
'size of jaw if the push is one
If wsSheet.Range("I2").Value = Push1 Then
wsSheet.Range("H2").Value = WorksheetFunction.Large(myArray, WorksheetFunction.CountIf(myArray, ">" & SizePush) + 1)
Exit Sub
End If
I'm using secondarray as range of cells in a worksheet (Ex. "1", "2") to exclude them as autofilter list that I'm defining in the below function in "filtercriteria".
I get "type mismatch" error in the filter (secondarray) line for some reason, but I works flawlessly when I define an array using a list of items instead. For example, if I use below line to define secondarray instead.
secondarray = ("1", "2")
I've researched similar postings and wasn't lucky, can someone help with this instance?
Thanks,
Dim secondArray As Variant
secondArray = Range("L76:M76").Value
c = 0
k = 0
count = 0
rowNumb = Worksheets("List").Range(Worksheets("List").Range("L5"), Worksheets("List").Range("L5").End(xlDown)).Rows.count
For L = 1 To rowNumb
c = Worksheets("List").Range("L5").Offset(L)
If c <> k Then
'check the current activity type against the array of types we don’t want. If it isn’t in the array we add it to an array that will be used as the filter criteria
If UBound(Filter(secondArray, c)) = -1 Then
ReDim Preserve filterCriteria(0 To count)
filterCriteria(count) = c
count = count + 1
End If
k = c
End If
Next
It isn't working because filter function takes a One-dimensional array of strings to be searched for its sourcearray argument.
When you read in a range from the sheet you automatically get a 2d array as opposed to the 1D you have when assigning from a list.
Find a way to use a 1D array to pass in
For example, as your data is coming from 1 row then slice the array by row
UBound(Filter(Application.WorksheetFunction.Index(secondArray, 1, 0), c)) = -1
You may need to find the right method for you.
Another method is given here.
I have code that creates an array and enters "supplier names" or "null" (actual string null) into an array if certain conditions are met. If certain conditions are not met, the array will not be filled with any data and is thus empty (or so I believe).
The next thing I want to do is print out only the supplier names listed in that array. Hence I have to create an If statement that will only be entered when the item in the array does not have the value "null" and when the array is not empty.
I'm experiencing the following problem in the code below. The string array supplierCategoryP(r) did not meet the conditions and thus was never filled with any information. So I assume this is an empty array. Yet when I debug, the code shows that this first If is still entered:
If supplierCategoryP(r) <> "null" And Not IsEmpty(supplierCategoryP(r)) Then
...while it shouldn't, since the array is empty.
k = 1
If countNoNull > 0 Then
moveDownBy = countNoNull
For r = 1 To nP
If supplierCategoryP(r) <> "null" And Not IsEmpty(supplierCategoryP(r)) Then
Cells(9 + k + moveDownBy, 5) = supplierCategoryP(r)
k = k + 1
countNoNull = countNoNull + 1
End If
Next r
Else
For r = 1 To nP
If supplierCategoryP(r) <> "null" And Not IsEmpty(supplierCategoryP(r)) Then
Cells(9 + k, 5) = supplierCategoryP(r)
k = k + 1
countNoNull = countNoNull + 1
End If
Next r
End If
Code that creates the array:
Worksheets("PEMCO").Activate
comNO = CLng(Range("commoditiesAmount").Text)
nP = CLng(Range("supplierAmount").Text)
ReDim supplierCategoryP(1 To nP) As String
For c = 1 To comNO
commodityLoop = Cells(3, 1 + c)
If commodity = commodityLoop Then
For r = 1 To nP
cellX = Cells(3 + r, 1 + c)
If cellX = "x" Then
supplierCategoryP(r) = Cells(3 + r, 1)
Else
supplierCategoryP(r) = "null"
End If
Next r
End If
Next c
Note that the IsEmpty function doesn't work on a null string, it tests for empty numeric value. You can verify this in the Immediate pane:
?IsEmpty("")
False
since you've ReDim your array to a specific number of items, all of those items are initialized as an empty string by the ReDim statement. Later, you assign to (overwrite) some those items with either the value from the cell, or the "null" value. The other cases will still retain the vbNullString from initialization.
To check for an empty string, you'd need to test whether supplierCategoryP(r) = vbNullString (this is the built-in constant which expresses "").
Or, if you consider spaces or sequence of spaces " " to be empty, you'd use Trim:
Trim(supplierCategoryP(r)) = vbNullString
Note also, and this may seem pedantic, but it's important: an empty array is not the same as an array that's been initialized which contains "empty" values. Your array is never empty, even if it contains nothing but "empty" (vbNullString) values.
I have a 100 X 1 (n1) cell array with each cell holding indices of a bigger data set(100 X 100, n2). I made a nested loop in order to access each individual element(index) and compare the values of another data set with these indices with a if condition. If the condition succeeds, I want to delete that element from the original cell array into a new cell array. However when I set the element to [] in matlab, the value of the cell array does not change. The code is below:
for i = 1:length(n1)
for j = 1:length(n1{i, 1})
if n2(i,n1{i,1}(1,j)) > n3(i) && n2(i, n1{i,1}(1,j)) > n4(n1{i, 1}(1, j))
n1{i,1}(1,j) == [];
end
end
end
I take that n1(i,1) is always a row vector so you should use,
n1{i,1}(j) = [];
If n1(i,1) is not a column or row then removing an element from middle would be impossible.
for example:
A = {[1 2 3],[5 8 9]}
A{1,2}(1,2) = []
gives the error: A null assignment can have only one non-colon index.
But A{1,2}(2) = [] is okey.
How do you read the data from a dynamic array out?
ReDim idx(1 To nItemsToPick)
ReDim varRandomItems(1 To nItemsToPick)
For i = 1 To nItemsToPick
Do
booIndexIsUnique = True
idx(i) = Int(nItemsTotal * Rnd + 1)
For j = 1 To i - 1
If idx(i) = idx(j) Then
booIndexIsUnique = False
Exit For
End If
Next j
If booIndexIsUnique = True Then
Exit Do
End If
Loop
varRandomItems(i) = rngList.Cells(idx(i), 1)
Next i
Thank you!
Somehow you have to get the user to input a starting cell and whether they want the data horizontally or vertically.
Then if the user inputs "A1", and there are 10 elements and the orientation is horizontal you need to turn that into a string -> "A1:A10"
Range("A1:J10") = varRandomItems
or
Range("A1:A10") = Application.Transpose(varRandomItems)
(apologies, I'm forgetting how to put the strings together at this point)