The below code is writing an invoice's fields to an array. Most rows have a PO# starting with "MX" (eg. MX111111, MX222222), and it's always either in column E or F. All other data fields are the same offset relative to the PO#. For example, "item code" always one cell away, "hours" always two cells away. Hence the array populates to the value of POCol + 1, POCol + 2 etc.
I'm getting a "VBA run-time error 1004 : Application-defined or object-defined error" on line 23 of below code - when it starts trying to write cell values to the array.
I'm 99% sure it's because integer variable "POCol" is not getting a value. Why? If I set POCol = 5 outside the if statements - no errors - but it defeats the purpose of check cols E & F for the invoices it's in F.
So... I'm guessing there is an issue with my InStr if statement? The MsgBox's never pop-up, so I'm guessing my if logic never comes through.
Can anyone see any issues, and is there any more info I need to give?
Thanks - really appreciate any help.
line = 1
For row = 1 To 100
' When ColA hits "Comments", the line numbers are done, so skip below ie. stop looking for more line numbers and go to next invoice
If Cells(row, "A").Value = "Comments" Then
Exit For
End If
' Find which column the PO number is in
POCol = 0
If InStr(1, Cells(row, "E").Value, "MX") <> 0 Then
MsgBox "Found MX ColE"
POCol = 5
End If
If InStr(1, Cells(row, "F").Value, "MX") <> 0 Then
MsgBox "Found MX ColF"
POCol = 6
End If
If IsNumeric(Cells(row, "A")) And Len(Cells(row, "A")) = 1 Then ' if it's a single digit in ColA, it's a line number
If Cells(row, "A").Value = 1 And Len(Cells(row, "A")) = 1 Then ' if first line, refer to columns one-row-down
DataArray(invoice, 7, line, 1) = Cells(row, "A").Value ' Line #
DataArray(invoice, 7, line, 2) = Cells(row + 1, POCol).Value ' PO #
DataArray(invoice, 7, line, 3) = Cells(row + 1, POCol + 1).Value ' Item code
DataArray(invoice, 7, line, 4) = Cells(row + 1, POCol + 2).Value ' Hours
DataArray(invoice, 7, line, 5) = Cells(row + 1, POCol + 4).Value ' Line total
'DataArray(invoice, 7, line, 6) = DataArray(invoice, 7, line, 5) / 10 ' Line GST
line = line + 1 ' Move to next array item
End If
If Cells(row, "A").Value > 1 And Len(Cells(row, "A")) = 1 Then ' if other lines, refer to columns in same row
DataArray(invoice, 7, line, 1) = Cells(row, "A").Value ' Line #
DataArray(invoice, 7, line, 2) = Cells(row, POCol).Value ' PO #
DataArray(invoice, 7, line, 3) = Cells(row, POCol + 1).Value ' Item code
DataArray(invoice, 7, line, 4) = Cells(row, POCol + 2).Value ' Hours
DataArray(invoice, 7, line, 5) = Cells(row, POCol + 4).Value ' Line total
'DataArray(invoice, 7, line, 6) = DataArray(invoice, 7, line, 5) / 10 ' Line GST
line = line + 1 ' Move to next array item
End If
End If
Related
Sub blockofdatatoreport()
Dim i As Integer
Dim x As Integer
Dim y As Integer
For i = 1 To 95
actvrw = Sheet1.Range("A:A").Find(what = i, searchdirection = xlNext).Row
'searching cells top to bottom
lr = Sheet2.Range("A:A").Find(what = "*", searchdirection = xlprevious).Row + 1
'searching cells bottom to top
For x = 1 To 5
Sheet2.Cells(lr, 1).Value = Sheet1.Cells(actvrw + (x - 1), 3).Value
'looping the first five columns in sheet2
Next
For y = 1 To 4
Sheet2.Cells(lr, 5 + y).Value = Sheet1.Cells(actvrw + (y - 1), 6).Value
'looping the next four columns after the first four is done in sheet2
Next
'You can also write like this or write a loop in two lines above.
'Sheet2.Cells(lr, 1).Value = Sheet1.Cells(actvrw, 3).Value
'Sheet2.Cells(lr, 2).Value = Sheet1.Cells(actvrw + 1, 3).Value
'Sheet2.Cells(lr, 3).Value = Sheet1.Cells(actvrw + 2, 3).Value
'Sheet2.Cells(lr, 4).Value = Sheet1.Cells(actvrw + 3, 3).Value
'Sheet2.Cells(lr, 5).Value = Sheet1.Cells(actvrw + 4, 3).Value
Next
End Sub
I get error called error 13 y type mismatch, what is in the above code causing the error??
I received many Excel files from a client.
Their system extracted the data into a spreadsheet, but one column is having issues. If the text was too long, it would put the remaining text into the cell below it.
This causes all the other fields in that row to be blank, except for the overflow.
How can I merge cells at issue into one for all files I received?
I uploaded a screen shot of the file as an example. Notice on row 8 that H8 is the only cell. That needs to be merged with H7. Not every row is at issue though.
asuming A is the main (and empty for doubles)
asuming H holds the text
then in L1 and copy down
=H1&IF(LEN(A2),H2,"")
simplest way (then copy values from L to H and delete empty lines (simply with filter)
when having unknown number of lines (after splitting) you better use vba (or simply repeat the whole procedure till there no empty lines anymore...
doing it in VBA:
Sub testing()
Dim i As Long
While Len(Cells(i + 1, 8))
i = i + 1
While Len(Cells(i + 1, 1)) = 0 And Len(Cells(i + 1, 8))
Cells(i, 8) = Cells(i, 8) & Cells(i + 1, 8)
Rows(i + 1).Delete
Wend
Wend
End Sub
most programs skip spaces so you may want to use:
=H1&IF(LEN(A2)," "&H2,"")
or for vba change Cells(i, 8) = Cells(i, 8) & Cells(i + 1, 8) to Cells(i, 8) = Cells(i, 8) & " " & Cells(i + 1, 8)
This will concatenate the texts in H and delete the row that is not useful :
Sub test_bm11()
Dim wS As Worksheet, _
LastRow As Long, _
i As Long
Set wS = ActiveSheet
With wS
LastRow = .Range("H" & .Rows.Count).End(xlUp).Row
For i = LastRow To 2 Step -1
If .Cells(i, "A") <> vbNullString Then
Else
.Cells(i, "H").Offset(-1, 0) = .Cells(i, "H").Offset(-1, 0) & .Cells(i, "H")
.Cells(i, "H").EntireRow.Delete
End If
Next i
End With
End Sub
I have a great problem. I have a macro that does not run since the last week and I do not know why? I'm getting always the error: "Runtime Error 9 ...."
I know there must be a problem with the renames or arrays but I do not find anything.
Could you please check this code, maybe you can find the problem.
Thanks in advance!
Regards, Krisztian
It is just a part of the big macro.
The error is in the 8th row when "k" would be = "10"
Ubound is teoretically "9" (??)
EditRow(k) = EditRow(k) + (insdels * (k - 1))
'Populate input sheet
insdels = 0
For i = 1 To UBound(EditRow)
If FAT_Blocks(i).Range_Name <> "Absent_from_BCM" Then ' only process the blocks that were present
k = FAT_Blocks(i).GPI_Block_Number 'ERROR***************************************************************************
EditRow(k) = EditRow(k) + (insdels * (k - 1))
For m = 1 To FAT_Blocks(i).Rows
If FAT_Blocks(i).Data(m, 1) <> 0 Then ' ignore rows with zero cost/revenue in year 1
If GPIsheet.Cells(EditRow(k), 2).Interior.ColorIndex <> new_CI Then
new_InsDel (1)
insdels = insdels + 1
EditRow(k) = EditRow(k) + (k - 1)
End If
If FAT_Blocks(i).Row_Title(m) Like "*One Time*" Then
y = 0
ElseIf FAT_Blocks(i).Row_Title(m) Like "*Recurring*" Then
y = 1
Else
y = 2
End If
With GPIsheet
.Range("Original_Import_Data").Offset(EditRow(k) - 1, 0).ClearContents ' clear any mung before updating
' fill the "new_CI" coloured cells on the left
.Cells(EditRow(k), 2).value = BCMid & " " & FAT_Blocks(i).Row_Title(m) ' row description
.Cells(EditRow(k), 3).value = FAT_Blocks(i).Data(m, 1) ' unit value
.Cells(EditRow(k), 4).value = change_pc(y) ' change %
.Cells(EditRow(k), 5).value = change_year(y) ' change year
.Cells(EditRow(k), 6).value = vol_driver(y) ' volume driver
' fill the grey stuff on the right
With .Range("Original_Import_Data").Offset(EditRow(k) - 1, 0)
.Cells(1, 1) = yr ' Contract length
.Cells(1, 2) = BCMid ' BCM unique id
For j = 1 To yr
.Cells(1, 2 + j) = FAT_Blocks(i).Data(m, j) ' data for corresponding year
Next
End With
End With
EditRow(k) = EditRow(k) + 1
End If
Next m
End If
Next i
My function looks like this:
Sub sortNumbers()
Dim i As Integer
Dim j As Integer
Dim highestNumber As Integer
For i = 1 To 8
If IsEmpty(Cells(i + 4, 6).Value) = False Then
If Cells(i + 3, 6).Value > Cells(i + 4, 6).Value Then
highestNumber = Cells(i + 3, 6).Value
Cells(i + 3, 6).Value = Cells(i + 4, 6).Value
Cells(i + 4, 6).Value = highestNumber
End If
End If
Next i
For j = 1 To 8
If IsEmpty(Cells(j + 4, 6).Value) = False Then
If Cells(i + 3, 6).Value > Cells(i + 4, 6).Value Then
Call sortNumbers
Else
Exit Sub
End If
End If
Next j
End Sub
Everything gets sorted properly, but right after I get a message saying Out of stack space
Any help would be much appreciated!
EDIT
The excel sample data looks like this:
test data
1
100
1000
8
9
9
50
100
500
(from F3-F12)
If you remove the IsEmpty lines, Empty cells will be treated as 0. If you wish to leave them blank and sort around them you will need to impliment additional logic.
Your second loop needed to be adjusted. As it stood, the first time
call 1:
The first loop would give:
1 100 8 9 9 50 100 500 1000
Then the second loop would get to 1 > 100 and exit sub.
BUT... its best not to simply remove the exit sub call.
Its more efficient to only recall sortNumbers once per call.
If you had simply removed the exit sub.
then the second loop would get to 100 > 8 and trigger a recursion (Call 2).
Call 2:
the first loop would give:
1 8 9 9 50 100 100 500 1000
then the second loop would determine that Cells(i + 3,6) is never > Cells(i + 4,6) and exit Sub.
Since the 2nd call has returned we resume Call 1 where we left off. This means we finish the 2nd loop.
If this were a larger dataset you could have hundreds of recursions required to sort the dataset. When the last call (lets say its the 104th call) returns the previous 103 calls to the routine would all finish their 2nd loops (which since the 104th call returned, the data is already sorted, and thus is a waste)
The second loop should simply check to see if a recall is nessisary and if so, recall sortNumbers one time.
Sub sortNumbers()
Dim i As Integer
Dim j As Integer
Dim highestNumber As Integer
For i = 1 To 8
If Cells(i + 3, 6).value > Cells(i + 4, 6).value Then
highestNumber = Cells(i + 3, 6).value
Cells(i + 3, 6).value = Cells(i + 4, 6).value
Cells(i + 4, 6).value = highestNumber
End If
Next i
Dim ReCall As Boolean
ReCall = False
For i = 1 To 8
If Cells(i + 3, 6).value > Cells(i + 4, 6).value Then
ReCall = True
i = 8
End If
Next i
If ReCall Then Call sortNumbers
End Sub
I have a list of variables in my spreadsheet that belong to some points named "a,b,c,d..." (etc) for a varying number of points. This list is then followed by a similar list of variables (1,2,3,4..." (etc) which also varies in length.
For one column, what I'd like to do is this:
For an initial set of data, e.g. for 4 start points in rows "a","b","c","d", the first 5 cells' data would be copied from rows "a","b","c","d","d"; the next 4 would be copied from "b","c","d","d", the next 3 would be copied from "c","d","d", then "d","d".
See link (the formatting isn't necessary, I just had it in my text file to make it easier to spot the repeats). The data I would like to copy down is the column labelled "K-".
http://i.imgur.com/3gCPWxm.png
I'm sure there's a way of doing this with a couple of loops, I just can't get my head around it!
Dim str As String
str = "abcd"
str = str & Right(str, 1)
Debug.Print str
Do While Len(str) > 2
str = Right(str, Len(str) - 1)
Debug.Print str
Loop
outputs:
abcdd bcdd cdd dd
For the same operation done on cells:
lRow = Cells(1, 2).End(xlDown).Row
Range(Cells(lRow + 1, 2), Cells(lRow * 2 - 1, 2)).Value = Range(Cells(2, 2), Cells(lRow, 2)).Value
Cells(lRow * 2, 2).Value = Cells(lRow * 2 - 1, 2).Value
i = lRow - 1
lRow = lRow * 2
Do While i >= 2
Range(Cells(lRow + 1, 2), Cells(lRow + i, 2)).Value = Range(Cells(lRow - i + 1, 2), Cells(lRow, 2)).Value
lRow = lRow + i
i = i - 1
Loop