Query relating to use of Offset - VBA - vba

I wrote a code for the command button which copies particular data from a particular cell of sheet 1 and pastes in particular cell of sheet 2.
Next time i run the code i want the values to be placed in the next row that i used earlier in sheet 2
What is that i have to change in the code to get it so
Private Sub CommandButton1_Click()
text = Sheets(1).Range("B15")
Sheets(2).Range("B2").Value = Left(text, 4)
Sheets(2).Range("E2").Value = Right(text, 20)
text = Sheets(1).Range("C15")
Sheets(2).Range("C2").Value = text
text = Sheets(1).Range("B10")
Sheets(2).Range("G2").Value = text
text = Sheets(1).Range("B27")
Sheets(2).Range("H2").Value = text
text = Sheets(1).Range("B11")
Sheets(2).Range("J2").Value = text
text = Sheets(1).Range("B20")
Sheets(2).Range("K2").Value = text
text = Sheets(1).Range("D9")
Sheets(2).Range("L2").Value = text
End Sub

Private Sub CommandButton1_Click()
Dim c as Range
'assuming each row on sheet2 has a value in columnB...
Set c = Sheets(2).Cells(Rows.Count,"B").End(xlUp).Offset(1,0)
With c.EntireRow
text = Sheets(1).Range("B15")
.Cells(1,"B").Value = Left(text, 4)
.Cells(1,"E").Value = Right(text, 20)
.Cells(1,"C").Value = Sheets(1).Range("C15").Value
.Cells(1,"G").Value = Sheets(1).Range("B10").Value
.Cells(1,"H").Value = Sheets(1).Range("B27").Value
.Cells(1,"J").Value = Sheets(1).Range("B11").Value
.Cells(1,"K").Value = Sheets(1).Range("B20").Value
.Cells(1,"L").Value = Sheets(1).Range("D9").Value
End With
End Sub

Related

Word VBA- On error go to msgbox rather than crashing userform

How would I incorporate "on error" into my code so that if a user enters something into a textbox that is not a date formatted to MM/DD/YYYY or left blank it will not exit out of my userform. Other solutions are appreciated as well!
The code:
Private Sub CommandButton1_Click()
Dim s As String
s = Me.TextBoxx25.Text
If s = "" Then
ElseIf DateDiff("d", Me.TextBoxx25.Text, "11/12/2020") > 0 Then
Doc1.Bookmarks("bmrow1").Range.Cells.Delete
Doc2.Variables("TextBoxx25").Value = Me.TextBoxx25.Text
Doc2.Variables("TextBoxx1").Value = Me.TextBoxx1.Text
Doc2.Variables("TextBoxx7").Value = Me.TextBoxx7.Text
Doc2.Variables("TextBoxx13").Value = Me.TextBoxx13.Text
Doc2.Variables("TextBoxx19").Value = Me.TextBoxx19.Text
Doc2.Variables("TextBoxx31").Value = Me.TextBoxx31.Text
Else
Doc1.Variables("TextBoxx25").Value = Me.TextBoxx25.Text
Doc1.Variables("TextBoxx1").Value = Me.TextBoxx1.Text
Doc1.Variables("TextBoxx7").Value = Me.TextBoxx7.Text
Doc1.Variables("TextBoxx13").Value = Me.TextBoxx13.Text
Doc1.Variables("TextBoxx19").Value = Me.TextBoxx19.Text
Doc1.Variables("TextBoxx31").Value = Me.TextBoxx31.Text
Doc2.Bookmarks("bmrow1").Range.Cells.Delete
End If
It would have to display a msgbox that says please format as (MM/DD/YYYY)

VB.NET: DataGridView different button in same Column

I am tring to initialize a checker that checks the cells of a certain column in datagridview, if the cell is equal to StaffMixname then the button text should be VIEW if not then the button text is LOCKED
here is my code so far.
Dim dgButtonColumn As New DataGridViewButtonColumn
Dim i As Integer
MetroGrid7.Columns.Add(dgButtonColumn)
dgButtonColumn.HeaderText = "Security"
dgButtonColumn.UseColumnTextForButtonValue = True
For i = 0 To MetroGrid7.Rows.Count
If MetroGrid7.Rows(i).Cells.Item(4).Value.ToString() = StaffMixname.Text Then
dgButtonColumn.Text = "VIEW"
dgButtonColumn.Name = "viewBtn"
dgButtonColumn.ToolTipText = "View"
Else
dgButtonColumn.Text = "LOCKED"
dgButtonColumn.Name = "searchSecurityBtn"
dgButtonColumn.ToolTipText = "LOCKED"
End If
Next
My desired result is like this
It doesn't work like this. In DataGridViewButtonColumn each cell contains a button but you can not access it. You can get the DataGridViewButtonCell and change these two properties value and ToolTipText. There is no name property in DataGridViewButtonCell. So to change them:
Dim dgButtonColumn As New DataGridViewButtonColumn
Dim i As Integer
MetroGrid7.Columns.Add(dgButtonColumn)
dgButtonColumn.HeaderText = "Security"
'remove this line
'dgButtonColumn.UseColumnTextForButtonValue = True
For i = 0 To MetroGrid7.Rows.Count
If MetroGrid7.Rows(i).Cells.Item(4).Value.ToString() = StaffMixname.Text Then
MetroGrid7.Rows(i).Cells.Item(6).Value = "VIEW"
MetroGrid7.Rows(i).Cells.Item(6).ToolTipText = "View"
Else
MetroGrid7.Rows(i).Cells.Item(6).Value = "LOCKED"
MetroGrid7.Rows(i).Cells.Item(6).ToolTipText = "LOCKED"
End If
Next
Dim status As String
status = status & Me.dgTitleList.CurrentRow.Cells("BDO").Value
status = status & Chr(13)
Insert the Value of the Datagridview Column to String and do the condition.
If status = StaffMixname.Text Then
dgButtonColumn.Text = "VIEW"
dgButtonColumn.Name = "viewBtn"
dgButtonColumn.ToolTipText = "View"
Else
dgButtonColumn.Text = "LOCKED"
dgButtonColumn.Name = "searchSecurityBtn"
dgButtonColumn.ToolTipText = "LOCKED"
End If
And I suggest is to use BreakPoint and check if the StaffMixName.Text has a Value.

OpenOffice BASIC how to insert checkbox in sheet

I'm using OpenOffice Calc.
And I am writing macro's in OpenOffice BASIC.
I need the right code to insert a checkbox in the sheet.
I now have
Dim Doc as Object
Doc = ThisComponent
Dim cbName As Object
cbName = "checkbox_name"
Dim oCheckBoxModel as Object
// dlg is a dialog, (don't know how to create a checkbox else)
oCheckBoxModel = dlg.getmodel().createInstance( "com.sun.star.awt.UnoControlCheckBoxModel" )
oCheckBoxModel.PositionX = 100
oCheckBoxModel.PositionY = 100
oCheckBoxModel.Width = 50
oCheckBoxModel.Height = 30
oCheckBoxModel.Label = id
oCheckBoxModel.Name = cbName
oCheckBoxModel.Enabled = True
oCheckBoxModel.TabIndex = 1
Doc.Sheets().insertByName( cbName, oCheckBoxModel ) // This line is totally wrong, but I hope it's clear what I want to do
So I want to create a checkbox, and then insert it into the sheet. (In a specific cell, or just by setting a X and Y position).
I searched on the internet, but I only find information about inserting controls into a dialog, not into a sheet
To create check boxes manually, see here. To create check boxes dynamically:
Sub CreateCheckbox
oDoc = ThisComponent
oSheet = oDoc.Sheets.getByIndex(0)
oDrawPage = oSheet.DrawPage 'Was oDrawPage = oDoc.getDrawPage()
oCheckboxModel = AddNewCheckbox("Checkbox_1", "Check this box", oDoc, oDrawPage)
End Sub
Function AddNewCheckbox(sName As String, sLabel As String, _
oDoc As Object, oDrawPage As Object) As Object
oControlShape = oDoc.createInstance("com.sun.star.drawing.ControlShape")
aPoint = CreateUnoStruct("com.sun.star.awt.Point")
aSize = CreateUnoStruct("com.sun.star.awt.Size")
aPoint.X = 1000
aPoint.Y = 1000
aSize.Width = 3000
aSize.Height = 1000
oControlShape.setPosition(aPoint)
oControlShape.setSize(aSize)
oButtonModel = CreateUnoService("com.sun.star.form.component.CheckBox")
oButtonModel.Name = sName
oButtonModel.Label = sLabel
oControlShape.setControl(oButtonModel)
oDrawPage.add(oControlShape)
AddNewCheckbox = oButtonModel
End Function
This code was adapted from https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=46391.

DevExpress.XtraGrid.StyleFormatCondition using vb.net

How can i format some cell value into something else using DevExpress.XtraGrid.StyleFormatCondition . For example I need to bold some value in one or more cell with same value and how to change only one column not entire row
Here it is my example:
For i As Integer = 0 To GridView1.DataRowCount
Dim a As String = (GridView1.Columns(0)).ToString
If a = "Vrsta računa" Then 'THE NAME OF COLUMN
Dim b = GridView1.GetRowCellValue(i, "accountsType") ' THE NAME OF CELL
If b = "KLASA" Then
Dim condition1 As StyleFormatCondition = New DevExpress.XtraGrid.StyleFormatCondition()
condition1.Appearance.BorderColor = Color.Blue
condition1.Appearance.BackColor = Color.Red
condition1.Appearance.Options.UseBackColor = True
condition1.Appearance.Options.UseBorderColor = True
'***** HOW TO BOLD CELL VALUE *******
condition1.Condition = FormatConditionEnum.Expression
condition1.ApplyToRow = False
condition1.Expression = "[accountsType] = 'KLASA'"
GridView1.FormatConditions.Add(condition1)
ElseIf b = "SINTETIKA" Then
Dim condition1 As StyleFormatCondition = New DevExpress.XtraGrid.StyleFormatCondition()
condition1.Appearance.BorderColor = Color.Blue
condition1.Appearance.BackColor = Color.HotPink
condition1.Appearance.Options.UseBackColor = True
condition1.Appearance.Options.UseBorderColor = True
condition1.Condition = FormatConditionEnum.Expression
condition1.ApplyToRow = False
condition1.Expression = "[accountsType] = 'SINTETIKA'"
' I WANT TO Have an effect on a cell , AND not on entire row
GridView1.FormatConditions.Add(condition1)
ElseIf b = "ANALITIKA" Then
Dim condition1 As StyleFormatCondition = New DevExpress.XtraGrid.StyleFormatCondition()
condition1.Appearance.BorderColor = Color.Blue
condition1.Appearance.BackColor = Color.Green
condition1.Appearance.Options.UseBackColor = True
condition1.Appearance.Options.UseBorderColor = True
condition1.Condition = FormatConditionEnum.Expression
condition1.ApplyToRow = False
condition1.Expression = "[accountsType] = 'ANALITIKA'"
GridView1.FormatConditions.Add(condition1)
End If
End If
Next
It looks like you are using Format Conditions not in a correct way.
There is no need to traverse through all rows and columns manually. Before painting each row, GridView automatically checks if it meets any condition in its FormatConditions collection. If so, GridView applies a corresponding style.
Please refer to the following help topic to learn how to use Format Conditions correctly:
https://documentation.devexpress.com/#WindowsForms/CustomDocument759

Excel VBA array data source for Chartspace in Userform

I try to generate a barchart within an Excel Userform - Chartspace Is there any possibility to use VBA generated array data as source for the bar chart. I can only find description for Spreadsheet as source.
Private Sub UserForm_Activate()
Dim z As Long, s As Integer
Dim cc
Dim ch1
Dim pt
For z = 1 To 9
For s = 1 To 2
Spreadsheet1.ActiveSheet.Cells(z, s) = Sheets("Tabelle1").Cells(z, s)
Next
Next
Set cc = ChartSpace1.Constants
Set ChartSpace1.DataSource = Spreadsheet1 '<-- does it need linked to a spreadsheet?
Set ch1 = ChartSpace1.Charts.Add
ch1.Type = cc.chChartTypeLineMarkers
ch1.SetData 1, 0, "A2:A9"
ch1.SeriesCollection(0).SetData 2, 0, "B2:B9"
End Sub
Is the a other way to show a bar chart in a userform where I can use the array source?
Thanks a lot.
Perhaps this suggests how to do it:
http://msdn.microsoft.com/en-us/library/office/aa193650(v=office.11).aspx
This example (slightly modified so that I could test from the link above) creates a chart using literal data arrays.
Output Example
Code Example
Sub BindChartToArrays()
Dim asSeriesNames(1)
Dim asCategories(7)
Dim aiValues(7)
Dim chConstants
Dim chtNewChart
Dim myChtSpace As ChartSpace
asSeriesNames(0) = "Satisfaction Data"
asCategories(0) = "Very Good"
asCategories(1) = "Good"
asCategories(2) = "N/A"
asCategories(3) = "Average"
asCategories(4) = "No Response"
asCategories(5) = "Poor"
asCategories(6) = "Very Poor"
aiValues(0) = 10
aiValues(1) = 22
aiValues(2) = 6
aiValues(3) = 31
aiValues(4) = 5
aiValues(5) = 14
aiValues(6) = 12
Set myChtSpace = UserForm1.ChartSpace1
Set chConstants = myChtSpace.Constants
' Add a new chart to Chartspace1.
Set chtNewChart = myChtSpace.Charts.Add
' Specify that the chart is a column chart.
chtNewChart.Type = chConstants.chChartTypeColumnClustered
' Bind the chart to the arrays.
chtNewChart.SetData chConstants.chDimSeriesNames, chConstants.chDataLiteral, asSeriesNames
chtNewChart.SetData chConstants.chDimCategories, chConstants.chDataLiteral, asCategories
chtNewChart.SeriesCollection(0).SetData chConstants.chDimValues, chConstants.chDataLiteral, aiValues
UserForm1.Show
End Sub