Repeating formula over a range - excel

Sub Test3()
Range("C23").Formula = "=SUM(C16:C22)"
Range("D23").Formula = "=SUM(D16:D22)"
Range("E23").Formula = "=SUM(E16:E22)"
Range("F23").Formula = "=SUM(F16:F22)"
Range("G23").Formula = "=SUM(G16:G22)"
Range("H23").Formula = "=SUM(H16:H22)"
Range("I23").Formula = "=SUM(I16:I22)"
Range("K16").Formula = "=J16-C23"
Range("K17").Formula = "=J17-D23"
Range("K18").Formula = "=J18-E23"
Range("K19").Formula = "=J19-F23"
Range("K20").Formula = "=J20-G23"
Range("K21").Formula = "=J21-H23"
Range("K22").Formula = "=J22-I23"
End Sub
How do I simplify this code?

If you are asking whether your code can be simplified, then yes. For the first group, you can use this instead...
Range("C23:I23").Formula = "=SUM(C16:C22)"
For the second one, you can use this...
Range("K16:K22").Formula = "=SUM(J16-INDEX($C$23:$I$23, ROWS(K$16:K16)))"

Related

Receive error when double click data in Listbox

I am newbies in VBA. Recently I have created a "Search Form". The search data have 21 columns which will display in the "Search List Box". Unfortunately, it only can show until 9 columns and will return an error for the 10 and above data. Your guidance is very much appreciated. Below is my code:
Private Sub dtlist_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
CDSv1.dptname.Text = CDSv1.dtlist.Column(1)
CDSv1.dptadd.Text = CDSv1.dtlist.Column(2)
CDSv1.divcom.Text = CDSv1.dtlist.Column(3)
CDSv1.ctcname.Text = CDSv1.dtlist.Column(4)
CDSv1.ctcno.Text = CDSv1.dtlist.Column(5)
CDSv1.nid.Text = CDSv1.dtlist.Column(6)
CDSv1.serno.Text = CDSv1.dtlist.Column(7)
CDSv1.ipinfo.Text = CDSv1.dtlist.Column(8)
CDSv1.snet.Text = CDSv1.dtlist.Column(9)
CDSv1.gateway.Text = CDSv1.dtlist.Column(10) '<---- The error starts here.
CDSv1.vlinfo.Text = CDSv1.dtlist.Column(11)
CDSv1.netcatcom.Text = CDSv1.dtlist.Column(12)
CDSv1.netsubcatcom.Text = CDSv1.dtlist.Column(13)
CDSv1.ispcom.Text = CDSv1.dtlist.Column(14)
CDSv1.snscom.Text = CDSv1.dtlist.Column(15)
CDSv1.cirt.Text = CDSv1.dtlist.Column(16)
CDSv1.bdwh.Text = CDSv1.dtlist.Column(17)
CDSv1.statcom.Text = CDSv1.dtlist.Column(18)
CDSv1.remf.Text = CDSv1.dtlist.Column(20)
End Sub

How can i add this other condition to this code?

i have this subroutine that i use to make labels. for some reason i cant just make another subroutine where instead of "CGI_SAMPLE_LABEL" it uses "YCI_SAMPLE_LABEL" because of other subroutines. any suggestions on how i can add this so that it chooses either one or the other. if tried using WHERE OR but that didn't work. i edited some of the tags because LIMS basic language is also like smalltalk.
LabelType = "CGI_SAMPLE_LABEL"
pTableNameStr = "SAMPLE"
pLabelNameStr = "CGI_SAMPLE_LABEL"
'Breakpoint(aReason)
NumSamples = Ubound(selectedObjects, 1)
FOR X = 1 TO NumSamples
SampleNumber = selectedObjects[x]
pKeyNameArr[1] = SampleNumber
pNumLabelsInt = 1
pLabelNameStr = "CGI_SAMPLE_LABEL"
pReasonStr = "Auto Label Generation"
pActivityStr = "Label printed for sample logged event"
GOSUB FN_LABEL_PRINT_ALL
NEXT 'Sample
RETURN

How to add Calculated Member to Pivot Table via Python win32

I'm trying to add a calculated column into this pivot table that is being generated via a python script with import win32com.client.
Code is posted below that is generating the pivot table. I'm not sure how to add a column. In VBA it would be wb.PivotTable.CalculatedField.Add but that didn't work (at least the syntax I tried).
I'm trying to calculate [OoCUnits] / [GrossUnits]
#Make Pivot version with four weeks data
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
win32c = win32com.client.constants
wb = Excel.Workbooks.Open(filename)
ws3 = wb.Worksheets('Dataset')
cl1 = ws3.Cells(1,1)
cl2 = ws3.Cells(max_row,max_col)
PivotSourceRange = ws3.Range(cl1,cl2)
ws3.Activate()
PivotSourceRange.Select()
wb.Sheets.Add (After=wb.Sheets(3))
ws4 = wb.Worksheets(4)
ws4.Name = 'Pivot'
cl3 = ws4.Cells(4,1)
PivotTargetRange = ws4.Range(cl3,cl3)
PivotTableName = 'OoCPivot'
#Make Pivot Table
PivotCache = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=PivotSourceRange, Version=win32c.xlPivotTableVersion14)
PivotTable = PivotCache.CreatePivotTable(TableDestination=PivotTargetRange, TableName=PivotTableName, DefaultVersion=win32c.xlPivotTableVersion14)
PivotTable.PivotFields('Product Name').Orientation = win32c.xlRowField
PivotTable.PivotFields('Product Name').Position = 1
PivotTable.PivotFields('Customer Number').Orientation = win32c.xlPageField
PivotTable.PivotFields('Customer Number').Position = 1
PivotTable.PivotFields('Customer Name').Orientation = win32c.xlPageField
PivotTable.PivotFields('Customer Name').Position = 2
PivotTable.PivotFields('Week Ending Date').Orientation = win32c.xlColumnField
PivotTable.PivotFields('Week Ending Date').Position = 1
DataField = PivotTable.AddDataField(PivotTable.PivotFields('GrossUnits'))
DataField.NumberFormat = '#0.00'
DataField = PivotTable.AddDataField(PivotTable.PivotFields('OoCUnits'))
DataField.NumberFormat = '#0.00'
I'm inserting the values with the DataField. The script is not presenting any issues as is.
EDIT: The exact code I implemented to solve. The last two lines are just formatting. I'm including in case it helps someone else.
CalcField = PivotTable.CalculatedFields().Add('OoC Unit %','= OoCUnits / GrossUnits')
DataField = PivotTable.AddDataField(PivotTable.PivotFields('OoC Unit %'))
DataField.NumberFormat = '#%'
PivotTable.DisplayErrorString = True
Actually, the method is PivotTable.CalculatedFields().Add() according to docs. Consider placing below at bottom:
CalcField = PivotTable.CalculatedFields().Add("OC_GrossPct", "= OoCUnits / GrossUnits")
PivotTable.PivotFields("OC_GrossPct").Orientation = win32c.xlDataField

Excel VBA For statement

I want to create a loop for the following code . The only thing i need the to loop to do is move to the next column (which would be column F). the rows will stay the same. any suggestions?
xlSheet.Range("e23") = (Me.overrings)
xlSheet.Range("e33") = (Me.overingTax)
xlSheet.Range("e27") = (Me.Sampling)
xlSheet.Range("e28") = (Me.Waste)
xlSheet.Range("e29") = (Me.Promo)
xlSheet.Range("e42") = (Me.Online)
xlSheet.Range("e34") = (Me.freebieTax)
xlSheet.Range("e49") = (Me.totalDepo)
xlSheet.Range("e38") = (Me.CreditCards)
If you really want the same value in the adjacent cell, you can modify your existing code like this (no loop required):
xlSheet.Range("e23:f23") = (Me.overrings)
For x= 5 to 6
With xlSheet.columns(x)
.cells(23).value=Me.overrings
'etc
End with
Next x

How to write data to existing Sharepoint 2007 Calender List

Domestic demand,We must let our develop system form copy to the WSS 3.0 Calendar list
Simply put, I know need to use ADO to update [Alluserdata] this table and also know whitch
tp_listId.
However, after updating, has written to the database, it can not appear in Sharepoint page
What am I do some thing wrong...
ps: suspected tp_size field related, but not sure ....
Public Sub Pm_woToSharePoint(ByVal PM_WO_MAIN_VIEWRow As PM_WO_MAIN_VIEW)
Try
Dim cdataobj As New cDataACCESS
Dim dt_Prolist As Data.DataTable
Dim selectString As String
selectString = "select top 1 * from AllUserData where tp_listId='4C0516DB-B090-4740-A0F0-B99E6DC31C0C' order by tp_id desc"
dt_Prolist = cdataobj.sqlDt(selectString, "SharepointConnectionString")
Dim CalenderListRow As Data.DataRow = dt_Prolist.NewRow
Dim CalenderListRow2 As Data.DataRow = dt_Prolist.Rows(0)
With CalenderListRow2
CalenderListRow.Item("tp_ID") = .Item("tp_id") + 1
CalenderListRow.Item("tp_ListId") = .Item("tp_ListId")
CalenderListRow.Item("tp_SiteId") = .Item("tp_SiteId")
' CalenderListRow.Item("tp_RowOrdinal") = 0
CalenderListRow.Item("tp_Version") = 1
CalenderListRow.Item("tp_Author") = 96
CalenderListRow.Item("tp_Editor") = 96
CalenderListRow.Item("tp_Modified") = Now
CalenderListRow.Item("tp_Created") = Now
CalenderListRow.Item("tp_Ordering") = DBNull.Value
CalenderListRow.Item("tp_ThreadIndex") = DBNull.Value
' CalenderListRow.Item("tp_HasAttachment") = 0
'CalenderListRow.Item("tp_ModerationStatus") = 0
CalenderListRow.Item("tp_IsCurrent") = 1
CalenderListRow.Item("tp_ItemOrder") = 100 * CalenderListRow.Item("tp_ID")
CalenderListRow.Item("tp_InstanceID") = DBNull.Value
' CalenderListRow.Item("tp_GUID") = 4
' CalenderListRow.Item("tp_CopySource") = DBNull.Value
'CalenderListRow.Item("tp_HasCopyDestinations") = DBNull.Value
CalenderListRow.Item("tp_AuditFlags") = DBNull.Value
CalenderListRow.Item("tp_InheritAuditFlags") = DBNull.Value
CalenderListRow.Item("tp_Size") = .Item("tp_Size")
CalenderListRow.Item("tp_WorkflowVersion") = 1
CalenderListRow.Item("tp_WorkflowInstanceID") = DBNull.Value
CalenderListRow.Item("tp_DirName") = .Item("tp_DirName")
CalenderListRow.Item("tp_LeafName") = CStr(CalenderListRow.Item("tp_ID")) & "_.000"
CalenderListRow.Item("tp_DeleteTransactionId") = .Item("tp_DeleteTransactionId")
CalenderListRow.Item("tp_ContentType") = .Item("tp_ContentType")
CalenderListRow.Item("tp_ContentTypeId") = .Item("tp_ContentTypeId")
CalenderListRow.Item("nvarchar1") = PM_WO_MAIN_VIEWRow.ITEM_NAME & " " & PM_WO_MAIN_VIEWRow.ITEM_VALUE
CalenderListRow.Item("nvarchar3") = PM_WO_MAIN_VIEWRow.ST_NAME_CHT
CalenderListRow.Item("ntext2") = "機台:" & PM_WO_MAIN_VIEWRow.MS_NAME_CHT & " 生產人員:" & PM_WO_MAIN_VIEWRow.ProductPerson
CalenderListRow.Item("datetime1") = DateAdd(DateInterval.Hour, -8, CType(PM_WO_MAIN_VIEWRow.START_TIME, DateTime))
CalenderListRow.Item("datetime2") = DateAdd(DateInterval.Hour, -8, CType(PM_WO_MAIN_VIEWRow.END_TIME, DateTime))
CalenderListRow.Item("int1") = 0
' CalenderListRow.Item("int2") = .Item("int2")
CalenderListRow.Item("bit1") = 0
CalenderListRow.Item("bit2") = 0
CalenderListRow.Item("bit3") = 0
CalenderListRow.Item("tp_Level") = 1
CalenderListRow.Item("tp_IsCurrentVersion") = 1
CalenderListRow.Item("tp_UIVersion") = 512
CalenderListRow.Item("tp_CalculatedVersion") = 0
CalenderListRow.Item("tp_UIVersionString") = "1.0"
CalenderListRow.Item("tp_DraftOwnerId") = DBNull.Value
CalenderListRow.Item("tp_UIVersionString") = DBNull.Value
CalenderListRow.Item("tp_CheckoutUserId") = DBNull.Value
End With
dt_Prolist.Rows.Add(CalenderListRow)
cdataobj.ExecSQL(dt_Prolist, selectString, "SharepointConnectionString")
Catch ex As System.Exception
Throw
End Try
End Sub
You are on totally the wrong track here accessing the database directly, its dangerous and unsupported.
The correct way to do things like this is the object model or web services (or client object model in 2010) - see here and here to get started.
This is for 2010, but its identical steps 2007 - How to add a event to a SharePoint 2010 Calendar
SPListItem newEvent = yourCalendar.Items.Add();
newEvent["Location"] = "This is location";
newEvent["Start Time"] = DateTime.Now;
newEvent["End Time"] = DateTime.Now.AddHours(1);
newEvent["Description"] = "Going for Meting";
newEvent["Title"] = "New Meting";
newEvent["fAllDayEvent"] = false;
newEvent["Category"] = "Meting";
//Create Event in the sharpoint.
newEvent.Update();

Resources