This is a simple question. I have this code:
CurrentRow = 3
MyColumn = 2
CurrentCell = (CurrentRow & "," & MyColumn)
WorkingSheet.Cells(CurrentCell).value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)
I thought that this would place the data on the 'WorkingSheet' in the position "B3" but it places the data in the cell "AF1".
Why is this?
Thanks,
Josh
Cell is not expected to be used as you are using it; you have to input the row and column indices (as integers) separated by a comma. Thus the right code is:
WorkingSheet.Cells(CurrentRow, MyColumn).Value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)
Another alternative you have is using Range. Example:
WorkingSheet.Range("B3").Value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)
Related
i had this error when i was trying to INSERT records to a local table with VBA.
I have checked the data type and putting in the quotes for the the short text data type but it doesn't work.
table_newid = "SELECT Cint(t1." & id_name(i) & "_new), " & Replace(select_column_str, local_table_name, "t2") & " FROM " & vbCrLf & _
"(SELECT CInt(DCount(""[" & id_name(i) & "]"", """ & qry_distinct_id_name & """, ""[" & id_name(i) & "]<="" & [" & id_name(i) & "])) as " & id_name(i) & "_new, * FROM " & qry_distinct_id_name & ") AS t1 " & vbCrLf & _
"LEFT JOIN " & local_table_name & "_ALL as t2 " & vbCrLf & _
"ON t1." & id_name(i) & " = t2." & id_name(i) & " " & vbCrLf & _
"WHERE t2.database = '" & database_name & "'"
strQuery = "INSERT INTO " & local_table_name & "_temp (" & temp_field(i) & ", " & Replace(select_column_str, local_table_name & ".", "") & ") " & vbCrLf & table_newid
Debug.Print strQuery
DoCmd.SetWarnings False
db.Execute strQuery
DoCmd.SetWarnings True
From the debug.print, i have got:
INSERT INTO TblLUMachineTypes_temp (MachTypeID_new, MachTypeID, MachTypeCode, MachTypeMod, MachTypeDesc, MachTypeDisc, NewCode, Approved, mttime, CreatedBy, CreatedTS, ModifiedBy, ModifiedTS)
SELECT t1.MachTypeID_new, t2.MachTypeID, t2.MachTypeCode, t2.MachTypeMod, t2.MachTypeDesc, t2.MachTypeDisc, t2.NewCode, t2.Approved, t2.mttime, t2.CreatedBy, t2.CreatedTS, t2.ModifiedBy, t2.ModifiedTS FROM
(SELECT CInt(DCount("[MachTypeID]", "qry_TblLUMachineTypes_id_distinct", "[MachTypeID]<=" & [MachTypeID])) as MachTypeID_new, * FROM qry_TblLUMachineTypes_id_distinct) AS t1
LEFT JOIN TblLUMachineTypes_ALL as t2
ON t1.MachTypeID = t2.MachTypeID
WHERE t2.database = 'CPM-252-2'
When i copied this query and execute it manually, it works fine but not with VBA. Any idea?
Thanks in advance.
Remove all the & vbCrLf from your code, they are not necessary and I assume they corrupt the SQL syntax.
I found the problem. I found that qry_distinct_id_name query table has a Dlookup function in there that returns a string value, which will work when executing the query manual but doesn't work when you run it with VBA. So I have re-written the code to put in the quote before and after dlookup() function.
I have the following formula that I am inputting in a function.
dim minfee, feetier3, feetier4, feetier5, bpspread1, bpsread2, bpspread3 as double
call insformulaincell("=IF(K2 = 100, ""#NA"", IF(K2 <" & minfee & "," & feetier3 & ",IF(K2<" & feetier4 & ",K2+ " & bpspread1 & ",IF(K2<" & feetier5 & ",K2+ " & bpspread2 & ",K2+ " & bpspread3 & "))))")
'all the function does is paste the formula into a cell
'How would I format the formula so that it can be stored as a single string?
'Ex:
dim sFormula as string
sformula = ""=IF(K2 = 100, ""#NA"", IF(K2 <" & minfee & "," & feetier3 & ",IF(K2<" & feetier4 & ",K2+ " & bpspread1 & ",IF(K2<" & feetier5 & ",K2+ " & bpspread2 & ",K2+ " & bpspread3 & "))))""
call insformulaincell(sFormula)
The main issue is that the variables such as minfee would not reference its actual values but instead have the actual string variable name appear.
Ex: "... If(K2 <" & minfee & "," ... )
as opposed to
"... If(K2 < 1) ..." ' assuming that minfee = 1
In VBA " serves as a delimiter for string literals, like this:
Dim foo As String
foo = "some string"
If your string literal needs to contain " double quotes, you need to escape them, by doubling them up between the string delimiters:
foo = """some string"""
Printing the above would yield "some string", including the double quotes.
So you do need to remove the leading & trailing double quotes.
sformula = "=IF(K2 = 100, ""#NA"", IF(K2 <" & minfee & "," & feetier3 & ",IF(K2<" & feetier4 & ",K2+ " & bpspread1 & ",IF(K2<" & feetier5 & ",K2+ " & bpspread2 & ",K2+ " & bpspread3 & "))))"
Breaking this down, it's a concatenation of the following literals:
"=IF(K2 = 100, ""#NA"", IF(K2 <" (note, "#NA" is a bad idea IMO. Use the NA() function to yield an actual worksheet error instead of a string value that looks like one)
","
",IF(K2<"
",K2+ "
",IF(K2<"
",K2+ "
",K2+ "
"))))"
Which looks right to me.
Arguably, such concatenation is annoyingly confusing. A custom StringFormat function can help mitigate this, by abstracting away the concatenations:
sFormula = StringFormat("=IF(K2=100, ""#NA"", IF(K2<{0},{1},IF(K2<{2},K2+{3},IF(K2<{4},K2+{5},K2+{6}", _
minfee, feetier3, feetier4, bpspread1, feetier5, bpspread2, bpspread3)
I've created this 2 player game called Go and I've made it so that the statistics from the game is recorded in a text file. If the same 2 players play each other more than once, their game will be recorded in the same text file on the next line.
I have done all of that, but now I want the previous games to be displayed on screen so that the players can see the scores from previous matches. I don't want the final line to be displayed as that is the score from the game that has just finished. Here's my code so far:
Dim file As System.IO.StreamWriter
'Imports the data from previous forms to form 3
handikomi = Form1.handi_komi
prisonerB = Form2.prisonerB
prisonerW = Form2.prisonerW
bpass = Form2.bpass
wpass = Form2.wpass
bstones = Form2.bstones
wstones = Form2.wstones
btotalscore = prisonerB + handikomi(0, 2)
wtotalscore = prisonerW + handikomi(1, 2)
If btotalscore > wtotalscore Then
winnerlbl.Text = (handikomi(0, 0) & " IS THE WINNER!")
ElseIf wtotalscore > btotalscore Then
winnerlbl.Text = (handikomi(1, 0) & " IS THE WINNER!")
End If
file = My.Computer.FileSystem.OpenTextFileWriter("F:\My Go project flood fill2\Text files\" & handikomi(0, 0) & " VS " & handikomi(1, 0) & ".txt", True)
'Displays statistics from current match
file.WriteLine("BLACK" & "," & "WHITE" & "," & "Total Score" & "," & btotalscore & "," & wtotalscore & "," & "Prisoners" & "," & prisonerB & "," & prisonerW & "," & "Passes" & "," & bpass & "," & wpass & "," & "Stones" & "," & bstones & "," & wstones)
file.Close()
breakdwnlbl.Text = " BLACK WHITE"
breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Total Score" & " " & btotalscore & " " & wtotalscore)
breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Prisoners" & " " & prisonerB & " " & prisonerW)
breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Passes" & " " & bpass & " " & wpass)
breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Stones" & " " & bstones & " " & wstones)
Dim data As String 'to hold value of file line
Dim filename As String 'declare file
filename = "F:\My Go project flood fill2\Text files\" & handikomi(0, 0) & " VS " & handikomi(1, 0) & ".txt" 'path to file on system
FileOpen(1, filename, OpenMode.Input) 'open file for reading
'try amend
Do While Not EOF(1)
data = LineInput(1)
MsgBox(data)
Loop
I think that I shouldn't be using EOF for this case, but I don't know what else to use as I'm still a beginner. I appreciate any help!
Wouldn't it be easier to read all lines and just skip the last one:
Dim allLines() As String = File.ReadAllLines(filename)
Dim allButLast = allLines.Take(allLines.Length - 1)
The variable allButLast is an IEnumerable(Of String) containing all the lines of the file, except for the last one.
Update:
Here's an example to show each line in a MessageBox:
For Each line As String In allButLast
MessageBox.Show(line)
Next
I'd like to make the cell to appear as follows()
Desired outcome-- as I hover over cell
'E15'=(1+9.27%)*(1+C15)-1" and as I hover away the equation is evaluated
What I don't want E15 = (0.0000007)-1
I have two formulas that I was working on but I just can't seem to get it right.
I'd appreciate any help. Thanks in advance.
Range("E15").Formula = "=(" & 1 + "(" & Range("D15").Value & )"") * (" & 1 + "(" & Range("C15") & "))" - 1"
Range("E15").Formula = "=(" & 1 + "(" & Range("D15").Value & ")" & ") * (" & 1 + "(" & Range("C15") & ") & ") - 1"
Running:
Sub Macro1()
With Range("E15")
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Chr(39) & .Address(0, 0) & Chr(39) & " " & .Formula
End With
End Sub
produces:
NOTE:
It is equally easy to place the Comment in an adjacent cell.
I am new to using Access 2010. I wish to execute the following sql update statement, but I have problems with the syntax. The table is called "Forecasts", and users will edit & update the qty forecasted.
Problem - The table fieldnames are 2014_1, 2014_2, 2014_3 ... to represent the different months, stored in an array. I have done abit of research and I believe the way to dynamically do this is:
Dim sqlString As String
sqlString = "UPDATE Forecasts " & _
" SET Branch_Plant=" & Me.txtBranch_Plant & _
", Item_Number_Short='" & Me.txtItem_Number_Short & "'" & _
", Description='" & Me.txtDescription & "'" & _
", UOM='" & Me.txtUOM & "'" & _
", Estimated_Cost=" & Me.txtEstimated_Cost & _
", Requesting_Business_Unit='" & Me.txtRequesting_Business_Unit & "'" & _
", End_Customer='" & Me.txtEnd_Customer & "'" & _
", Project='" & Me.txtProject & "'" & _
", Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume " & _
" WHERE ID =" & Me.txtID.Tag
MsgBox ("This is the output: " & sqlString)
CurrentDb.Execute sqlString
It was working fine until this line was added
Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume
The msgbx output now shows: "False". Whats wrong with sqlString?
Please help! Thank you very much.
", Forecasts.[" & arrMonthToDisplay(0) & "] = " & Me.txtProjectedJanVolume & _
" WHERE ID =" & Me.txtID.Tag
You compare string to string.
Change
", Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume " &
to
", Forecasts." & "[" & arrMonthToDisplay(0) & "] = " & " Me.txtProjectedJanVolume " &