I have read through the examples on Stackoverflow and still can't seem to get this statement right - can anyone point me to where I am going wrong please?
The error is a type mismatch at the point where I am trying to split the line of text held in LineText into a multidimensional array Orders(). I tried from RawOrders(j) to Orders(y, x) but same result.
Dim RawOrders() As String
Dim Orders() As String
Dim LineText As String
Dim h As Integer
Dim p As Integer
Dim x As Integer
Dim y As Integer
Dim j As Integer
Dim FilePath As String
Dim FileName As String
Dim FileNum As Integer
FileNum = FreeFile()
Open FileName For Input As #FileNum
RawOrders = Split(Input$(LOF(FileNum), #FileNum), vbNewLine)
Close #FileNum
ReDim Orders(3, 21)
h = 1
p = 0
j = 0
x = 0
y = 0
Do While Not RawOrders(p) = ""
LineText = RawOrders(h)
Do While j <> 21
Orders(y, x) = Split(LineText, ",") *Errors out here giving Type MissMatch*
x = x + 1
j = j + 1
Loop
y = y + 1
h = h + 1
p = p + 1
Loop
Dim splitRes() as Variant 'one dimension
Dim orders()
splitRes = yoursplitfunction
ReDim Orders(3, 21)
Do While j <> 21
Orders(y, x) = splitRes(j) 'guessing that you have 21 values in your lineText
'if you have less you get an error
x = x + 1
j = j + 1
Loop
This is what I got to after changing the Orders() to a variant.
Dim Count As Integer
Dim RawOrders() As String
Dim Orders() As Variant
Dim y As Integer
Dim h As Integer
FileNum = FreeFile()
Open FileName For Input As #FileNum
RawOrders = Split(Input$(LOF(FileNum), #FileNum), vbNewLine)
Close #FileNum
Count = UBound(RawOrders, 1)
ReDim Orders(Count - 1)
h = 1
y = 0
Do While Not RawOrders(h) = ""
Orders(y) = Split(RawOrders(h), ",")
y = y + 1
h = h + 1
Loop
Related
Trying to create a row range/list that stores all the row numbers with values in column A.
When I run the code I get the last VarRow() -> last row number instead of the entire range/list.
Many times, I could not run through VarRow(VarCount) = z + 1.
It stops at the row when the cell has the value so I cannot finish going to the next line of code (shows Subscript out of range).
Dim VarRow() As Double
VarCount = 0
VarCount2 = 0
For z = 1 To 350
If Range("A1").Offset(z, 0).Value <> 0 Then
VarCount = VarCount + 1
End If
Next z
ReDim Preserve VarRow(VaCount2)
For z = 1 To 350
If Range("A1").Offset(z, 0).Value <> 0 Then
VarCount2 = VarCount2 + 1
VarRow(VarCount) = z + 1
End If
Next z
Loop Through the Rows of a One-Column Range
Option Explicit
Sub RowNumbersToArray()
Const fRow As Long = 2
Const Col As String = "A"
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
Dim lRow As Long: lRow = ws.Cells(ws.Rows.Count, Col).End(xlUp).Row
Dim RowNumbers() As Long
Dim r As Long
Dim n As Long
For r = fRow To lRow
If ws.Cells(r, Col).Value <> 0 Then
ReDim Preserve RowNumbers(0 To n)
RowNumbers(n) = r
n = n + 1
End If
Next r
For n = 0 To n - 1
Debug.Print n, RowNumbers(n)
Next n
End Sub
How do I reference an array address where the WorksheetFunction.Max has found the largest value within the array? There can be multiple highs within the array.
Dim myArraySum(1 to 4) as long
Dim myArrayAddress(1 to 4) as integer
myArraySum(1) = 2
myArraySum(2) = 5
myArraySum(3) = 7
myArraySum(4) = 7
myArrayHigh = Application.WorksheetFunction.Max(myArraySum)
myArrayAddress = 'code needed
My desired output is
myArrayHigh = 7
myArrayAddress(1) = 3
myArrayAddress(2) = 4
The most straightforward way is to use a loop to check the values of every array element.
Function GetMaxIndicesArray(ByRef myArraySum() As Long, ByRef myArrayAddress() As Integer) As Integer
Dim i As Integer, j As Integer, iLow As Integer, iUp As Integer
Dim lMax As Long
iLow = LBound(myArraySum)
iUp = UBound(myArraySum)
lMax = Application.WorksheetFunction.Max(myArraySum)
j = 1
For i = iLow To iUp
If (myArraySum(i) = lMax) Then
myArrayAddress(j) = i
j = j + 1
End If
Next
GetMaxIndicesArray = j - 1
End Function
Sub test()
Dim myArraySum(1 To 4) As Long
Dim myArrayAddress(1 To 4) As Integer
Dim i As Integer, n As Integer
myArraySum(1) = 2
myArraySum(2) = 5
myArraySum(3) = 7
myArraySum(4) = 7
myArrayHigh = Application.WorksheetFunction.Max(myArraySum)
'myArrayAddress = "" 'code needed
n = GetMaxIndicesArray(myArraySum, myArrayAddress)
Debug.Print "myArrayHigh = " & myArrayHigh
For i = 1 To n
Debug.Print "myArrayAddress(" & i & ") = " & myArrayAddress(i)
Next
End Sub
Then in you debugger window, just type
test
The Result is like:
myArrayHigh = 7
myArrayAddress(1) = 3
myArrayAddress(2) = 4
Use a simple loop:
Sub dural()
Dim myArraySum(1 To 4) As Long
Dim myArrayAddress(1 To 4) As Integer
Dim myArrayHigh As Long, k As Long, msg As String
myArraySum(1) = 2
myArraySum(2) = 5
myArraySum(3) = 7
myArraySum(4) = 7
myArrayHigh = Application.WorksheetFunction.Max(myArraySum)
k = 1
For i = LBound(myArraySum) To UBound(myArraySum)
If myArraySum(i) = myArrayHigh Then
myArrayAddress(k) = i
k = k + 1
End If
Next i
msg = ""
For i = LBound(myArrayAddress) To UBound(myArrayAddress)
msg = msg & vbCrLf & i & vbTab & myArrayAddress(i)
Next i
MsgBox msg
End Sub
I am trying to have my code prompt the user to select a range of data of 3 width and variable length. There will only be 30 values those with some rows being blank. I want to have these 30 values populate into 30 text boxes in a userform of mine (this is so values don't have to be manually entered). I looked around and figured my route should be Application.Inputbox and then pass it into an array were the blank rows can be weeded out with for loops. I don't know how to pass the user selected table into a 2D array though.
Sub selectRange()
Dim r(1 To 14, 1 To 3) As Variant, ran As Range, calB(1 To 30) As Long, i As Integer, j As Integer, k As Integer, l As Integer
dozerCal.Hide
Set r = Application.InputBox("Select the Cal B table.", Type:=8)
For j = 1 To 14
For i = 1 To 3
If Abs(r(j, i)) > 0 Then
calB(l) = r(j, i)
l = l + 1
End If
Next
Next
lx = calB(1)
ly = calB(2)
lz = calB(3)
rx = calB(4)
ry = calB(5)
rz = calB(6)
ix = calB(7)
iy = calB(8)
iz = calB(9)
sx = calB(10)
sy = calB(11)
sz = calB(12)
p1x = calB(13)
p1y = calB(14)
p1z = calB(15)
p2x = calB(16)
p2y = calB(17)
p2z = calB(18)
lfx = calB(19)
lfy = calB(20)
lfz = calB(21)
lrx = calB(22)
lry = calB(23)
lrz = calB(24)
rfx = calB(25)
rfy = calB(26)
rfz = calB(27)
rrx = calB(28)
rry = calB(29)
rrz = calB(30)
ActiveWorkbook.Close
dozercall.Show
End Sub
Thanks in advance for everyone's help.
Edit: I missed that you were using the input box wrong, however I will leave this answer as it presents a way to collapse a variable range of user input from a multidimensional array into a single dimension array.
This should get you started. Basically it will read the user's input, dynamically create a one-dimensional array of the correct size (rows * columns), and read all the values in the range the user selects to this one dimensional array. It will then loop through the one dimensional array and print the values back out to the window.
I think this is what you're looking for, but if you need further clarification I can add some. I added comments so you can see what each section is doing.
Option Explicit
Private Sub TestArrays()
Dim calBTemp() As Variant, calB() As Variant
Dim i As Long, j As Long, x As Long
Dim rngInput As Range
Set rngInput = Application.InputBox("Select the Cal B table.", "Select Range", Type:=8)
'Read the user input, check for empty input
'If empty input, exit the subroutine
If Not rngInput Is Nothing Then
calBTemp = rngInput
Else
Exit Sub
End If
'Create the one-dimensional array dynamically based on user selection
ReDim calB((UBound(calBTemp, 1) - LBound(calBTemp, 1) + 1) * (UBound(calBTemp, 2) - LBound(calBTemp, 2) + 1))
'Loop through our multidimensional array
For i = LBound(calBTemp, 1) To UBound(calBTemp, 1)
For j = LBound(calBTemp, 2) To UBound(calBTemp, 2)
'Assign the value to our one dimensional array
calB(x) = calBTemp(i, j)
x = x + 1
Next j
Next i
'Loop through our one dimensional array
For i = LBound(calB) To UBound(calB)
Debug.Print calB(i)
Next i
End Sub
So I just wasn't using the Application.Inputbox right. If you return it as a range it will configure to the proper sized 2D array it seams and you can call/manipulate data from there. Here is a working sub.
Sub selectRange()
Dim ran As Range, calB(1 To 30) As Double, i As Integer, j As Integer, k As Integer, l As Integer
dozerCal.Hide
Set ran = Application.InputBox("Select the Cal B table.", Type:=8)
l = 1
For j = 1 To 14
For i = 1 To 3
If Abs(ran(j, i)) > 0 Then
calB(l) = ran(j, i)
l = l + 1
End If
Next
Next
lx = calB(1)
ly = calB(2)
lz = calB(3)
rx = calB(4)
ry = calB(5)
rz = calB(6)
ix = calB(7)
iy = calB(8)
iz = calB(9)
sx = calB(10)
sy = calB(11)
sz = calB(12)
p1x = calB(13)
p1y = calB(14)
p1z = calB(15)
p2x = calB(16)
p2y = calB(17)
p2z = calB(18)
lfx = calB(19)
lfy = calB(20)
lfz = calB(21)
lrx = calB(22)
lry = calB(23)
lrz = calB(24)
rfx = calB(25)
rfy = calB(26)
rfz = calB(27)
rrx = calB(28)
rry = calB(29)
rrz = calB(30)
ActiveWorkbook.Close
dozerCal.Show
End Sub
This code will do the trick (and forces the user to select 3 columns and 14 rows):
Sub selectRange()
Dim selectedRange As Range
Dim errorMessage As String
errorMessage = vbNullString
Dim ran As Range, calB(1 To 30) As Long, i As Integer, j As Integer, k As Integer, l As Integer
Do
'doesn't handle cancel event
Set selectedRange = Application.InputBox("Select the Cal B table.", _
Type:=8, Title:="Please select 14 rows and 3 columns" & errorMessage)
errorMessage = "; previous selection was invalid"
Loop While selectedRange.Columns.Count <> 3 Or selectedRange.Rows.Count <> 14
For j = 1 To 14
For i = 1 To 3
If Abs(selectedRange.Cells(j, i)) > 0 Then
calB(l) = selectedRange.Cells(j, i)
l = l + 1
End If
Next
Next
...rest of your code
I am trying to go through an array to find duplicate entries in a single column of that array and delete the entire row.
I am getting figuring out rangeStart, rangeEnd, and lastrow above this and that part is working fine.
data = Range(rangeStart, rangeEnd)
For i = lastrow - 1 To 2 Step -1
If data(i - 1, x) = data(i, x) Then
'Delete data(i)
End If
Next
Any help with this would be awesome!
Sub RemoveDups()
Const COMPARE_COL as Long = 1
Dim a, aNew(), nr As Long, nc As Long
Dim r As Long, c As Long, rNew As Long
Dim v As String, tmp
a = Selection.Value
nr = UBound(a, 1)
nc = UBound(a, 2)
ReDim aNew(1 To nr, 1 To nc)
rNew = 0
v = Chr(0)
For r = 1 To nr
tmp = a(r, COMPARE_COL)
If tmp <> v Then
rNew = rNew + 1
For c = 1 To nc
aNew(rNew, c) = a(r, c)
Next c
v = tmp
End If
Next r
Selection.Value = aNew
End Sub
Does this help?:
If data(i - 1, x) = data(i, x) Then
data(i,x).EntireRow.Delete
End If
Why not use Excel's inbuilt Unique options (Data ... Remove Duplicates)?
Another efficient VBA method is to use a Dictionary.
Sub A_Unique_B()
Dim X
Dim objDict As Object
Dim lngRow As Long
Set objDict = CreateObject("Scripting.Dictionary")
X = Application.Transpose(Range([a1], Cells(Rows.Count, "A").End(xlUp)))
For lngRow = 1 To UBound(X, 1)
objDict(X(lngRow)) = 1
Next
Range("B1:B" & objDict.Count) = Application.Transpose(objDict.Keys)
End Sub
I've this script on my program to read R component of an image and save it on an array:
Dim citra_asli As Bitmap = New Bitmap(PictureBoxAsli.Image)
Dim i As Integer = 0
Dim j As Integer = 0
Dim redValue(i, j) As Integer
ListBox3.Items.Add("Piksel--R--G--B")
For i = 0 To ((citra_asli.Height) - 1)
For j = 0 To ((citra_asli.Width) - 1)
Dim R As Integer = citra_asli.GetPixel(i, j).R
redValue(i, j) = R
ListBox3.Items.Add((i.ToString + ("," + (j.ToString + (" " + (redValue(i, j).ToString))))))
Next
Next
unfortunately I always get this error message "Index was outside the bounds of the array.". As long as I know, the redValue() array and citra_asli bitmap have identically dimension but why the error message appear? Somebody help me please or maybe there's another method to save it on an array. Thank you and please forgive me for my poor English.
This ought to be correct and i have tested it.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim citra_asli As Bitmap = New Bitmap(PictureBoxAsli.Image)
Dim x As Integer = 0
Dim y As Integer = 0
Dim R As Integer = 0
ListBox1.Items.Add("Piksel--R--G--B")
y = citra_asli.Height
x = citra_asli.Width
Dim redValue(x, y) As Integer
For y = 0 To (citra_asli.Height) - 1
For x = 0 To (citra_asli.Width) - 1
R = citra_asli.GetPixel(x, y).R
redValue(x, y) = R
ListBox1.Items.Add("[" & x.ToString & "," & y.ToString & "] " & "(" & redValue(x, y).ToString & ",grn,blu)")
Next
Next
End Sub