I have been searching for an hour or so and I can't seem to find anything on this issue. I may be wording it wrong so I'm not getting the results im looking for. I am having an issue with .offset (, 99). It seems that any time I put a like digit in there such as 99, 88, 11 or whatever combo there is, it raises runtime error 13 type mismatch.
But when I change it to 98 or any other non like combo it works fine. Is there some way that I need to program it if it has the same number multiple times?
Sub Scope()
Dim ws As Excel.Worksheet
Set ws = ThisWorkbook.Sheets("Future Ongoing Vetting") ' change to name of your sheet
Dim x As Long
x = 2
Do Until ws.Cells(x, 7) = ""
With ws.Cells(x, 5)
.Value = "• Customer name: " & .Offset(, 29) & _
Chr(10) & "• Customer Bus Org: " & .Offset(, 30) & _
Chr(10) & "• Internal Circuit ID: " & .Offset(, 2) & _
Chr(10) & "• Customer prem address: " & .Offset(, 12) & " " & .Offset(, 13) & " " & .Offset(, 14) & ", " & .Offset(, 15) & ", " & .Offset(, 16) & ", " & .Offset(, 17) & _
Chr(10) & "• Customer demarc: " & .Offset(, 18) & " " & .Offset(, 20) & ", " & .Offset(, 19) & " " & .Offset(, 21) & _
Chr(10) & "• MRR: " & .Offset(, 68) & _
Chr(10) & "• Current Off Net MRC: $" & .Offset(, 10) & _
Chr(10) & "• Margin Percent: " & .Offset(, 89) & _
Chr(10) & "• Bandwidth: " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb )" & _
Chr(10) & "• Customer term end date: ""TEXT(.Offset(, 32),""mmm-dd-yyyy"")" & _
Chr(10) & "• New Vendor: " & .Offset(, 106) & _
Chr(10) & "• New MRC: $" & .Offset(, 102) & _
Chr(10) & "• New NRC: $" & .Offset(, 103) & _
Chr(10) & "• New Install Interval: " & .Offset(, 105) & _
Chr(10) & "• New Term: " & .Offset(, 104) & _
Chr(10) & Chr(10) & "Planner Notes: This project is replacing the existing " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb ) based solution from ( " & .Offset(, 5) & " ) with a new " & .Offset(, 99) '& " ( " & .Offset(, 100) & "Mb ) based solution from ( " & .Offset(, 106) & " )."
' Chr(10) & "RFA # " & .Offset(, 107) & " install notes: " & _
' Chr(10) & "Please install ( " & .Offset(, 31) & " ) Ethernet " & .Offset(, 99) & " ( " & .Offset(, 100) & "Mb ) circuit with ( " & .Offset(, 106) & " ) from ( " & .Offset(, 101) & " ) to ( [Customer Prem] " & .Offset(, 12) & " " & .Offset(, 13) & " " & .Offset(, 14) & ", " & .Offset(, 15) & ", " & .Offset(, 17) & " )."
' Chr(10) & "This new circuit will be used to replace existing customer circuit ECCKT: ""&RC[1]&"", ""&RC[109]&"", ""&RC[110]&"", ICCKT: "" & RC[2] & ""." & _
' Chr(10) & "The customer prem address is ( ""&RC[12]&"" ""&RC[13]&"" ""&RC[14]&"", ""&RC[15]&"", ""&RC[16]&"", ""&RC[17]&"" ) and customer demarc is ( ""&RC[18]&"" ""&RC[20]&"", ""&RC[19]&"" ""&RC[21]&"" )."
End With
x = x + 1
Loop
End Sub
Your code is written in a way that makes it very difficult to debug. I would recommend replacing that extremely long statement by:
Dim s As String 'at the top of the module, and then in the loop ...
s = "• Customer name: " & .Offset(, 29)
s = s & Chr(10) & "• Customer Bus Org: " & .Offset(, 30)
s = s & Chr(10) & "• Internal Circuit ID: " & .Offset(, 2)
s = s & Chr(10) & "• Customer prem address: " & .Offset(, 12) & " " & .Offset(, 13) & " " & .Offset(, 14) & ", " & .Offset(, 15) & ", " & .Offset(, 16) & ", " & .Offset(, 17)
s = s & Chr(10) & "• Customer demarc: " & .Offset(, 18) & " " & .Offset(, 20) & ", " & .Offset(, 19) & " " & .Offset(, 21)
s = s & Chr(10) & "• MRR: " & .Offset(, 68)
s = s & Chr(10) & "• Current Off Net MRC: $" & .Offset(, 10)
s = s & Chr(10) & "• Margin Percent: " & .Offset(, 89)
s = s & Chr(10) & "• Bandwidth: " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb )"
s = s & Chr(10) & "• Customer term end date: ""TEXT(.Offset(, 32),""mmm-dd-yyyy"")"
s = s & Chr(10) & "• New Vendor: " & .Offset(, 106)
s = s & Chr(10) & "• New MRC: $" & .Offset(, 102)
s = s & Chr(10) & "• New NRC: $" & .Offset(, 103)
s = s & Chr(10) & "• New Install Interval: " & .Offset(, 105)
s = s & Chr(10) & "• New Term: " & .Offset(, 104)
s = s & Chr(10) & Chr(10) & "Planner Notes: This project is replacing the existing " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb ) based solution from ( " & .Offset(, 5) & " ) with a new " & .Offset(, 99) '& " ( " & .Offset(, 100) & "Mb ) based solution from ( " & .Offset(, 106) & " )."
.Value = s
Splitting the needlessly long statement into a series of shorter statements will enable you to better pinpoint where the type mismatch error is coming from.
I would prefer calling a function to build the string. You'll be able isolate the code and debug it in the Immediate Window without.
Note: I use ws.Rows(5).Cells to set a reference the cells in the row. In this way; I can reference each column by it's actual column number not the offset of Cells(RowNumber, 5). It also allows me to use a shorthand method of referring to cells on that row. e.g. r(1) refers to column 1 r(10) refers to column 10 of Rows(RowNumber).
Function getDescription(ws As Worksheet, RowNumber As Long) As String
Dim r As Range
Dim Data(14)
Set r = ws.Rows(5).Cells
Data(0) = "• Customer name: " & r(34)
Data(1) = "• Customer Bus Org: " & r(35)
Data(2) = "• Internal Circuit ID: " & r(7)
Data(3) = "• Customer prem address: " & r(17) & " " & r(18) & " " & r(19) & ", " & r(20) & ", " & r(21) & ", " & r(22)
Data(4) = "• Customer term end date: " & Chr(34) & Format(r(37), "mmm-dd-yyyy") & Chr(34)
Data(5) = "• Customer demarc: " & r(23) & " " & r(25) & ", " & r(24) & " " & r(26)
Data(6) = "• MRR: " & r(73)
Data(7) = "• Current Off Net MRC: $" & r(15)
Data(8) = "• Margin Percent: " & r(94)
Data(9) = "• Bandwidth: " & r(11) & " ( " & r(12) & "Mb )"
Data(10) = "• New Vendor: " & r(111)
Data(11) = "• New MRC: $" & r(107)
Data(12) = "• New NRC: $" & r(108)
Data(13) = "• New Install Interval: " & r(110)
Data(14) = "• New Term: " & r(109)
getDescription = Join(Data, Chr(10))
End Function
Related
strSQL = "INSERT INTO tbl_Processor_Info ([Division],[Service_Area],[Service_Line],[Service_Line_Process],[SLA_Type],[SLA_Ref],[Processing_Date],[Total_Processed],[Onshore_Error],[Invalid_Queries],[TAT_Missed],[RFT_Error],[Techinician]) VALUES('" & _
.Range("M" & i) & "','" & .Range("I" & i) & "','" & .Range("J" & i) & "','" & .Range("K" & i) & "','" & .Range("N" & i) & "','" & .Range("B" & i) & "',#" & .Range("A" & i) & "#," & .Range("C" & i) & "," & .Range("E" & i) & "," & .Range("F" & i) & "," & .Range("G" & i) & "," & .Range("H" & i) & ",'" & Environ("Username") & "')"
results in:
INSERT INTO tbl_Processor_Info ([Division],[Service_Area],[Service_Line],[Service_Line_Process],[SLA_Type],[SLA_Ref],[Processing_Date],[Total_Processed],[Onshore_Error],[Invalid_Queries],[TAT_Missed],[RFT_Error],[Techinician]) VALUES('ATA','PUT','DE','PBR','','SP-154',#02/01/2020#,4,,,1,,'arathod')
I have made a SQL statement that calculates the sum of the chosen fields I've gathered. I have the queries working within MS Access, but when I translate it to VBA coding within my database it spits out a compile error 3319: Syntax error. Below I have attached my working query as well as my query with the syntax.
Query within MS Access is below that works properly:
SELECT 1,'Passed - Depot' AS QRY, Sum(IIf(([PreStressStackDate]>=[StartDate] And [PreStressStackDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=5 And [CurrentLevelOfCompletion]<1073741829) Or [CurrentLevelOfCompletion]>1073741829),1,0)) AS [PreStress Stackup], Sum(IIf(([StackCompressionDate]>=[StartDate] And [StackCompressionDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=21 And [CurrentLevelOfCompletion]<1073741845) Or [CurrentLevelOfCompletion]>1073741845),1,0)) AS [Stack Compression], Sum(IIf(([TestingDate]>=[StartDate] And [TestingDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>85 And [CurrentLevelOfCompletion]<1073741909) Or [CurrentLevelOfCompletion]>1073741909),1,0)) AS Testing, Sum(IIf(([ShroudAssemblyDate]>=[StartDate] And [ShroudAssemblyDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=341 And [CurrentLevelOfCompletion]<1073742165) Or [CurrentLevelOfCompletion]>1073742165),1,0)) AS [Shroud Assembly], Sum(IIf(([TransformerInstallDate]>=[StartDate] And [TransformerInstallDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=1365 And [CurrentLevelOfCompletion]<1073743189)),1,0)) AS [Transformer Installation]
FROM TR343DrySide
WHERE (([TransducerSN] Not Like "CR*"));
UNION SELECT 2, 'Failed - Depot' AS QRY, Sum(IIf(([PreStressStackDate]>=[StartDate] And [PreStressStackDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073741829,1,0)) AS [PreStress Stackup], Sum(IIf(([StackCompressionDate]>=[StartDate] And [StackCompressionDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073741845,1,0)) AS [Stack Compression], Sum(IIf(([TestingDate]>=[StartDate] And [TestingDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073741909,1,0)) AS [Testing], Sum(IIf(([ShroudAssemblyDate]>=[StartDate] And [ShroudAssemblyDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073742165,1,0)) AS [Shroud Assembly], Sum(IIf(([TransformerInstallDate]>=[StartDate] And [TransformerInstallDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073743189,1,0)) AS [Transformer Installation]
FROM TR343DrySide
WHERE (([TransducerSN] Not Like "CR*"));
When this query is run, it tallies up the sum of the fields between the chosen dates.
Below I have attached my VBA code that comes up with a syntax compile error 3319:
Private Sub cmdDrySideRunReport_Click()
Dim strDrySQL_New, strDrySQL_Depot As String
Dim DryStartDate As Date
Dim DryEndDate As Date
'------------------------------------------------------------------------------------------------------
If IsNull(Me.txtDryStartDate) Or Me.txtDryStartDate = "" Or IsNull(Me.txtDryEndDate) Or Me.txtDryEndDate = "" Then
If IsNull(Me.txtDryStartDate) Or Me.txtDryStartDate = "" Then
MsgBox "Please enter the Start Date"
Me.txtDryStartDate.SetFocus
End If
If IsNull(Me.txtDryEndDate) Or Me.txtDryEndDate = "" Then
MsgBox "Please enter the End Date"
Me.txtDryEndDate.SetFocus
End If
Else
DryStartDate = Me.txtDryStartDate
DryEndDate = Me.txtDryEndDate + 1
'###########################################################
'DRYSIDE NEW
strDrySQL_New = "Select 1, 'Passed - New' AS QRY, Sum(IIf(([PreStressStackDate]>=#" & DryStartDate & "# And [PreStressStackDate]<=#" & DryEndDate & "#)" & _
" And (([CurrentLevelOfCompletion]>=5 And [CurrentLevelOfCompletion]<1073741829) Or [CurrentLevelOfCompletion]>1073741829),1,0)) AS [PreStress Stackup]," & _
" Sum(IIf(([StackCompressionDate]>=#" & DryStartDate & "# And [StackCompressionDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=21" & _
" And [CurrentLevelOfCompletion]<1073741845) Or [CurrentLevelOfCompletion]>1073741845),1,0)) AS [Stack Compression]," & _
" Sum(IIf(([TestingDate]>=#" & DryStartDate & "# And [TestingDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=85" & _
vbCrLf & " And [CurrentLevelOfCompletion]<1073741909) Or [CurrentLevelOfCompletion]>1073741909),1,0)) AS [Testing]," & _
" Sum(IIf(([ShroudAssemblyDate]>=#" & DryStartDate & "# And [ShroudAssemblyDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=341" & _
" And [CurrentLevelOfCompletion]<1073742165) Or [CurrentLevelOfCompletion]>1073742165),1,0)) AS [Shroud Assembly]," & _
" Sum(IIf(([TransformerInstallDate]>=#" & DryStartDate & "# And [TransformerInstallDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=1365 And [CurrentLevelOfCompletion]<1073743189)),1,0)) AS [Transformer Installation]" & _
" FROM TR343DrySide" & _
" WHERE (([TransducerSN] Like ""CR*""))" & _
vbCrLf & " UNION SELECT 2, 'Failed - New' AS QRY, Sum(IIf(([PreStressStackDate]>=#" & DryStartDate & "# And [PreStressStackDate]<=#" & DryEndDate & "#)" & _
" And [CurrentLevelOfCompletion]=1073741829),1,0)) AS [PreStress Stackup], Sum(IIf(([StackCompressionDate]>=#" & DryStartDate & "#" & _
" And [StackCompressionDate]<=#" & DryEndDate & "#) And [CurrentLevelOfCompletion]=1073741845,1,0)) AS [Stack Compression]," & _
" Sum(IIf(([TestingDate]>=#" & DryStartDate & "# And [TestingDate]<=#" & DryEndDate & "#) And [CurrentLevelOfCompletion]=1073741909,1,0)) AS [Testing]," & _
" Sum(IIf(([ShroudAssemblyDate]>=#" & DryStartDate & "# And [ShroudAssemblyDate]<=#" & DryEndDate & "#) And ([CurrentLevelOfCompletion]=1073742165 Or" & _
" [CurrentLevelOfCompletion]=1073742165),1,0)) AS [Shroud Assembly], Sum(IIf(([TransformerInstallDate]>=#" & DryStartDate & "# And [TransformerInstallDate]<=#" & DryEndDate & "#)" & _
" And [CurrentLevelOfCompletion]=1073743189,1,0)) AS [Transformer Installation]" & _
" FROM TR343Dryside" & _
" WHERE (([TransducerSN] Like ""CR*""));"
Me.sfrmCraneDrySidePassFailDateRange_New.Form.RecordSource = strDrySQL_New
Me.sfrmCraneDrySidePassFailDateRange_New.Visible = True
End If
End Sub
The result when the query is activated is: Run-time error '3319':
Syntax error within union query.
The problem is raised within the line of:
Me.sfrmCraneDrySidePassFailDateRange_New.Form.RecordSource = strDrySQL_New
At least here, you have an extra closing parenthesis:
.. And [CurrentLevelOfCompletion]=1073741829),1,0))
Should be:
.. And [CurrentLevelOfCompletion]=1073741829,1,0))
Insert a line:
Debug.Print strDrySQL_New
and study the print.
I need a .FormulaArray inserted into my worksheet, however it is well over the 255 character limit (it's 420). I've tried the .Replace workaround, but it doesn't work. Using .Formula = will get it into the cell, but it doesn't calculate and produces #VALUE!, so I would have to crtl+shift+Enter manually. Is there a way VBA can replicate this?
.Cells(Application.WorksheetFunction.Match("Red Car", .Range("A:A"), 0), Application.WorksheetFunction.Match(ComboBox1.Value & " " & Year(Date), .Range("A6:BZ6"), 0)).FormulaArray = "=INDEX('" & Root & sourceSheet & ws.Name & " " & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$D:$D,MATCH(""Dealer1"",'" & Root & sourceSheet & ws.Name & " " & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$A:$A&'" & Root & sourceSheet & ws.Name & " " & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$B:$B,0))"
The .Replace setup I'm using is:
Dim theFormulaPart1 As String
Dim theFormulaPart2 As String
theFormulaPart1 = "=INDEX('" & Root & sourceSheet & ws.Name & " " & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$D:$D,MATCH(""Dealer1"",'" & Root & sourceSheet & ws.Name & " " & "X_X_X())"
theFormulaPart2 = "&" & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$A:$A&'" & Root & sourceSheet & ws.Name & " " & "SUM" & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$B:$B,0))"
.Cells(Application.WorksheetFunction.Match("Red Car", .Range("A:A"), 0), Application.WorksheetFunction.Match(ComboBox1.Value & " " & Year(Date), .Range("A6:BZ6"), 0)).FormulaArray = theFormulaPart1
.Cells(Application.WorksheetFunction.Match("Red Car", .Range("A:A"), 0), Application.WorksheetFunction.Match(ComboBox1.Value & " " & Year(Date), .Range("A6:BZ6"), 0)).Reaplce "X_X_X())", theFormulaPart2
I am trying to populate a column with a SUMIFS formula if the criteria is matched.
cell.Offset(0, 2).Value = "=SUMIFS(PickData!E:E,PickData!A:A, _
" & cell.Address(Rowabsolute:=False, Column:=False) & ", PickData!C:C, _
"Retail",PickData!C:C, PickData!L:L, "Report1.TextBox1.Value")"
I can't see where i'm going wrong with it looking up the specific work Retail in PickData|C:C & the value from TextBox1 (this is date)
Any help would be greatly appreciated.
Thanks
Al
If you want a formula in the cell(s) then try this.
cell.Offset(0, 2).Formula = "=SUMIFS(PickData!E:E, PickData!A:A, " _
& cell.Address(0, 0) & ", PickData!C:C, " & Chr(34) & "Retail" & Chr(34) _
& ", PickData!L:L, DATEVALUE(" & Report1.TextBox1.Value & "))"
That should give you a valid SUMIFS(...) formula.
Addendum: looking at that a second time, the form's textbox value might need to be in quotes.
cell.Offset(0, 2).Formula = "=SUMIFS(PickData!E:E, PickData!A:A, " _
& cell.Address(0, 0) & ", PickData!C:C, " & Chr(34) & "Retail" & Chr(34) _
& ", PickData!L:L, DATEVALUE(" & Chr(34) & Report1.TextBox1.Value & Chr(34) & "))"
I want to insert data to my SQL server database using a datagrid view.I added some data to the grid and need to commit them using a for loop.
Dim i, rowCount As Integer
rowCount = dgOrder.Rows.Count
For i = 0 To (rowCount - 1)
objcon.DoExecute("INSERT INTO OrderMF (ItemType,ItemNm,UnitPrice,Quantity,Discount,TotalValue,FreeItem)VALUES('" _
& dgOrder.Item(i, 1).Value & "','" _
& dgOrder.Item(i, 0).Value & "','" _
& txtOrdNo.Text & "','" _
& dgOrder.Item(i, 3).Value & "','" _
& dgOrder.Item(i, 2).Value & "','" _
& dgOrder.Item(i, 5).Value & "','" _
& dgOrder.Item(i, 4).Value & "','" _
& dgOrder.Item(i, 6).Value & "')")
If objcon.m_Success = "0" Then
MsgBox("Record Added Successfully", MsgBoxStyle.Information, "ROBBIALAK")
Call ClearFields1()
cmdOrder.Enabled = False
End If
Next
This is my code for the button click.But it dosen't work for me.it is getting a exception saying "Index was out of range.Must be non negative and less than the size of the collection,parameter name:index" .In this code dgorder is my datagridview name and I used a method call DoExecute to Execute the SQL String.please help me to get through this problem.
Against intuition, it is .Item(columnIndex, rowIndex) not .Item(rowIndex, columnIndex). Luckily there is also .Item(columnName, rowIndex) and I advise you to use that, because that would increase readability a lot. So I think you should change
dgOrder.Item(i, 1).Value
to
dgOrder.Item("ItemType", i).Value
(if column name is "ItemType") and so on.
Also, it seems that you are trying to insert more values than there are columns specified
ItemType,
ItemNm,
UnitPrice,
Quantity,
Discount,
TotalValue,
FreeItem - 7 columns:
dgOrder.Item(i, 1).Value & "','" _
& dgOrder.Item(i, 0).Value & "','" _
& txtOrdNo.Text & "','" _
& dgOrder.Item(i, 3).Value & "','" _
& dgOrder.Item(i, 2).Value & "','" _
& dgOrder.Item(i, 5).Value & "','" _
& dgOrder.Item(i, 4).Value & "','" _
& dgOrder.Item(i, 6).Value & "' - 8 values