Why am I receiving 'type mismatch error' for this code? - type-mismatch

I am trying to specify for a range of cells to show #N/A if character results are not shown, and another range of cells to show #DIV/0! if numeric results are not shown. Below is the code which causes me to receive 'Type mismatch' error. How should I edit this?
Sub novalue()
Dim x As Integer
For x = 2 To 100
If Cells(x, 7).Value = 0 Then
Cells(x, 7).Value = "#N/A"
ElseIf Cells(x, 8).Value = 0 Then
Cells(x, 8).Value = "#N/A"
End If
Next x
End Sub

Try this code please :
Sub novalue()
For Each cell In Range("A1:C100")
If cell.Value = "" Then
cell.Value = "#N/A"
End If
Next cell
For Each cell In Range("D1:BV100")
If cell.Value = "" Then
cell.Value = "#DIV/0!"
End If
Next cell
End Sub

Related

scope of a variable inside of a nested loop

I am trying to read through an ActiveStorage::Attached::One object that is a txt file uploaded by the user.
Could anyone help me why the variable 'grid' inside the line.chars.each_with_index is nil?
txt_file = self.file.download.delete(' ') # returns the content of the file as a string
txt_file.each_line.with_index do |line, row_index|
next line if row_index == 0
if row_index == 1
grid = self.grids.create(generation: 0, rows: line[0].to_i, cols: line[1].to_i)
end
line.chars.each_with_index do |cell, column_index|
grid.cells.create(alive: cell == "*", row_position: row_index, column_position: column_index)
end
end
thanks a lot
Whenever row_index is not equal to 0 or equal to 1, grid is not assigned and therefore evaluates to nil. In other words, whenever txt_fle has three lines or more, grid will be nil at some point.

I get a Run-time error 3075 - Syntax error (missing operator) in query expression '[OReqID] = And [Item] = '

I am making a database using MS Access for my NGO, where we receive requisitions for relief materials from different centers and accordingly, we place an order to the supplier or dispatch from our stock. So generally, one requisition will have any one outcome either an order or dispatch. But rarely we may also send some quantity as dispatch and the rest as order. For example, we received a requisition from CoochBehar for 500 blankets. We placed an order for 375 blankets and dispatched 125 blankets from our stock.
I have two buttons on the Requisition form 'Order' & 'Challan'. I want to enable or disable them according to the situation. For new records, both should be enabled. I made a subroutine at the beginning and call it on the load event and current event of the form. It works for the existing data but when I try to enter a new record the error pos up.
I am using the following code -
Sub Check_Order_Dispatch()
Dim OrdCount As Integer
Dim DispCount As Integer
Dim OrdQnty As Double
Dim DispQnty As Double
Dim Qnty As Double
OrdCount = DCount("[OrdID]", "T02_Order_Details", "[OReqID] = " & Me.Txt_ReqID & " And [Item] = " & Me.Req_Details_SubF.Form!Cmbo_Item)
DispCount = DCount("[DispID]", "T04_Dispatch_Details", "[DReqID] = " & Me.Txt_ReqID & " And [Item] = " & [Req_Details_SubF].[Form]![Cmbo_Item])
OrdQnty = Nz(DLookup("Quantity", "T02_Order_Details", "[OReqID] = " & [Txt_ReqID] & " And [Item] =" & [Req_Details_SubF].[Form]![Cmbo_Item]), 0)
DispQnty = Nz(DLookup("Quantity", "T04_Dispatch_Details", "[DReqID] = " & [Txt_ReqID] & " And [Item] =" & [Req_Details_SubF].[Form]![Cmbo_Item]), 0)
Qnty = Me.Req_Details_SubF.Form!Txt_Quantity
If IsNull(Me.Txt_ReqID) Then
Btn_Challan.Enabled = True
Btn_Order.Enabled = True
Else
If OrdCount > 0 And OrdQnty < Qnty Then
Btn_Challan.Enabled = True
Btn_Order.Enabled = True
ElseIf OrdCount > 0 And OrdQnty >= Qnty Then
Btn_Challan.Enabled = False
Btn_Order.Enabled = True
ElseIf DispCount > 0 And DispQnty < Qnty Then
Btn_Challan.Enabled = True
Btn_Order.Enabled = True
ElseIf DispCount > 0 And DispQnty >= Qnty Then
Btn_Challan.Enabled = True
Btn_Order.Enabled = False
Else
Btn_Challan.Enabled = False
Btn_Order.Enabled = False
End If
End If
End Sub
I tried all possibilities that came to mind but to no effect. I am not very good at VBA. Please guide me. Thanks in advance!

Inserting row to table, with values two levels deep

When looping through a table trying to add 'sub properties' I get an error that i'm attempting to index field '?'. I am also sure that count and v.name both contain values.
Send help?
count = 0
local tbl = {}
for k,v in pairs(list) do
if string.find(v.name,"Jim") then
count = count + 1
tbl[count] = count
--below throws an error
--tbl[count]["name"] = {v.name}
end
end

Constructing Key/Value Table in Lua

I'm trying to build a script for a MUD I play that will create a table to keep track of average xp for each mob. I'm having trouble with the syntax of checking whether an element in a table exists and if not creating it. I tried something like this but keep getting: attempt to index field '?' (a nil value)
mobz_buried = {
{mob = "troll", quantity = 2}
{mob = "warrior", quantity = 1}
{mob = "wizard", quantity = 1}} -- sample data
number_of_mobz_buried = 4
xp_from_bury = 2000 -- another script generates these values, these are all just examples
xp_per_corpse = xp_from_bury / number_of_mobz_buried
for _, v in ipairs(mobz_buried) do
if type(mobz[v].kc) == "variable" then -- kc for 'kill count', number of times killed
mobz[v].kc = mobz[v].kc + 1 -- if it exists increment kc
else
mobz[v].kc = 1 -- if it doesn't exist create a key value that matches the mobs name and make the kc 1
end
if type(mobz[v].xp) == "variable" then -- xp for average experience points
mobz[v].xp = (((mobz[v].kc - 1) * mobz[v].xp + xp_per_corpse)/mobz[v].kc) -- just my formula to find the average xp over a range of differant buries
else
mobz[v].xp = xp_per_corpse -- if it doesn't exist create the table just like before
end
end
I'm trying to end up with mobz.troll = {kc, xp}, mobz.warrior = {kc, xp}, mobz.wizard = {kc, xp} and the ability to add more key values based off of the names mobz_buried gives me.
Based on extra info from your comments, it sounds like you didn't construct a table for mobz. Try this:
local mobz = {}
for _, v in ipairs(mobz_buried) do
mobz[v.mob] = mobz[v.mob] or {}
mobz[v.mob].kc = (mobz[v.mob].kc or 0) + 1
-- etc...
end

Attempt to index field 'other'

What is the problem here? This code is supposed to remove texts after I shoot it and at the same time increasing the score. Also, can someone explain what does the other.name actually mean? I don't quite fully understand it..(And yes its the first if statement that has the error)
function wordCollision(e)
if (e.other.name == 'balloonText') then -- error here: attempt to index field 'other'(a nil value)
display.remove(e.other)
e.other = nil
score.text = score.text + 50
score.anchorX = 0
score.anchorY = 0
score.x = 200
score.y = 50
target.text = target.text - 1
else
if (e.other.name == 'balloonTextt') then
display.remove(e.other)
e.other = nil
score.text = score.text + 50
score.anchorX = 0
score.anchorY = 0
score.x = 200
score.y = 50
target.text = target.text - 1
end
end
end
It simply means that there is no entry with key 'other' in the table e.
If if you want to look up something in e.other you'll have to assign a table to that key:
e.other = {}
Using metatables, you could make it go automatically:
mt = {}
mt.__index=function(t,k) if ~rawget(t,k) then t[k]=setmetatable({},mt) end return t[k] end
e={}
e=setmetatable(e,mt)
e.other.name='foo'
Watch out with this though, because any lookup to a non-existant field will create a new table for it, which may or may not be what you want (aside of the fact that this overwrites any existing metatable on e):
for k,v in pairs(e) do print(k,v) end
print(e.bar)
for k,v in pairs(e) do print(k,v) end
Problem could be that you have the e.other = nil in there, but don't reset e.other to something after, so when the wordCollision() gets called again, it is e.other is nil. Could also be that e.other is never initialized in the first place. Verify it is initialized somewhere before wordCollision() is ever called, and verify that it is re-set to something between two calls to wordCollision().

Resources