Word VBA Text Box on first page not anchoring to table in document with a table and textbox each page - ms-word

I have the following Word VBA code which creates a word document, puts a table on each page, and anchors a textbox to each table. On pages 2-6 the textboxes are correctly anchored and appear underneath the table. But on page 1 the table is not anchored correctly, and seems to be placed at 0, 0 on the page.
Simplified code that demonstrates the issue:
Dim myDoc
Set myDoc = Documents.Add
myDoc.Select
Dim i
For i = 1 To 6
Dim aRange
Set aRange = ActiveDocument.Paragraphs.Last.Range
Dim myTable
Set myTable = myDoc.Tables.Add(aRange, 10, 10)
myTable.Columns.Width = CentimetersToPoints(0.8)
myTable.Rows.Height = CentimetersToPoints(0.8)
Dim tb
Set tb = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=CentimetersToPoints(1.2), Width:=CentimetersToPoints(8), Height:=CentimetersToPoints(10), _
Anchor:=myTable.Cell(10, 1).Range.Characters.First)
Set aRange = ActiveDocument.Paragraphs.Last.Range
aRange.InsertBreak
Next i
Is there a way to get page 1 to display correctly, with the textbox appearing underneath the table?

For example:
Dim myDoc As Document, i As Long
Set myDoc = Documents.Add
With myDoc
For i = 1 To 6
.Tables.Add Range:=.Range.Characters.Last, NumRows:=10, NumColumns:=10
With .Tables(i)
.Columns.Width = CentimetersToPoints(0.8)
.Rows.Height = CentimetersToPoints(0.8)
End With
.Shapes.AddTextbox Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=CentimetersToPoints(1.2), Width:=CentimetersToPoints(8), Height:=CentimetersToPoints(10), _
Anchor:=.Tables(i).Cell(10, 1).Range.Characters.First
If i < 6 Then .Range.Characters.Last.InsertAfter Chr(12)
Next i
End With

Related

Get Row and Column number from "ThisComponent.CurrentSelection" in libreoffice calc basic

I have this code where I can get which one is the current selected cell and use it to modify its value:
theSelection = ThisComponent.CurrentSelection
theSelection.setString("some value")
Now I want to move to the next column to the right, if it was Microsoft excel VBA I could just use something like theSelection.Offset(0,1) but that's not the case. So I'm doing some workarounds of course:
nextCell = oActiveSheet.getCellByPosition( ???currentColumn + 1, ???currentRow)
ThisComponent.CurrentController.select( nextCell )
I just want to know the simplest way to replace these ??? to the actual values of the theSelection var to move to the next column to the right.
I also tried this:
nextCell = oActiveSheet.getCellByPosition( column() + 1, row())
But I don't know why it is always returning column() = 1 and row() = 1 in regardless of which is the value of the CurrentSelection. Thanks in advance for the help.
Get the cell address.
Sub ChangeAndThenGoToCellToRightOfSelection
oActiveSheet = ThisComponent.getCurrentController().getActiveSheet()
oSels = ThisComponent.getCurrentSelection()
If oSels.supportsService("com.sun.star.sheet.SheetCell") Then
'A single cell is selected.
oSels.setString("some value")
address = oSels.getCellAddress()
nextCell = oActiveSheet.getCellByPosition(address.Column + 1, address.Row)
ThisComponent.CurrentController.select(nextCell)
End If
End Sub
To see what an object can do, use an introspection tool such as XrayTool or MRI.

VBA Autofilter subset contains extra row up top

The macro attempts to filter Sheet "Temp" for one Criteria at a time (PE, AR, DC,FI), and copy column 5 that contains non-duplicate data into another sheet "Detail". Please help me understand two issues. (1) The macro does correct filtering for each of the 4 criteria. However, the filtered list for each of the criteria always contains the first item from the filtered list of the very first criteria "PE". I.e. the filtered list for criteria 2, "AR", contains all items in AR, but starts with the first item in "PE". There's a header row, but it doesn't seem to make a difference. How can I get rid of that first item in all cases except when filtering for "PE" (where it belongs)? (2) I would like to be able to count and store the number of visible rows for each filtered list. I would like to be able to paste each filtered list into another spreadsheet ("Detail"), starting in cell A4. Each consecutive list should start two rows below the list that was just pasted. For example, if the first list contains 16 items, then the next list should start in cell A22 (A4+16+2). For some reason, copiedrows (used to remember number of rows in a filtered list) is correct the first time around (=16), but not the second time (=1?). It looks like q's 1 & 2 are related. Perhaps, if I figure out #1, I can do something about #2. I reviewed exiting posts on Autofiltering, but still feel a bit stuck here. Really appreciate your help!
Sub FilterCategories()
Dim LastRow As Long
Dim startpos As Integer
Dim k As Integer
Dim copiedrows(1 To 4) As Long
Dim AG(1 To 4) As String
Dim rng As Range
AG(1) = "PE"
AG(2) = "AR"
AG(3) = "DC"
AG(4) = "FI"
'Autofilter based on each AG and copy over to 'Detail'. Create temporary
sheet for filtering.
startpos = 4
For k = LBound(AG) To UBound(AG)
Application.DisplayAlerts = False
On Error Resume Next
Sheets("Temp").Delete
Sheets("Lookup").AutoFilterMode = False
Sheets("Lookup").Copy After:=Sheets("Lookup")
ActiveSheet.Name = "Temp"
With Sheets("Temp")
AutoFilterMode = False
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
With .Range("A2:E" & LastRow)
.AutoFilter Field:=4, Criteria1:=AG(k)
.RemoveDuplicates Columns:=5
.Columns(5).SpecialCells(xlCellTypeVisible).Copy
Destination:=Sheets("Detail").Range("A" & startpos)
copiedrows(k) = .SpecialCells(xlCellTypeVisible).Rows.Count
Debug.Print copiedrows(k)
startpos = copiedrows(k) + 6
Debug.Print startpos
End With
End With
Next
End Sub

VBA Vlookup of range of dates in different sheets

I have zero coding experience and am new to VBA, so I don’t even know basics, but giving it a shot. I have a Workbook, with multiple sheet in it. The one that I care about are 2 sheets called DG, and Asp. DG has a button that grabs raw data from a server and populates the sheets ( multiple date columns with data value in adjacent cells). Asp has a button that grabs data as well but on a 30-day avg so every day in a month (columns A in Asp). This is the same case with DG sheet, but DG has data from different dates in a month, because it is not a 30 day pull. So that sets an image for you, now what I want to do is create a button, with a code that can go through a date column in DG and match it with a date from asp date and if there is a match, then copy and paste the adjacent cells values in DG to asp.
This is what I have so far with searches on the internet, showing just a vlookup for a single columns I want filled out in Asp, but its not working
Private Sub CommandButton2_Click()
Dim results As Double
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lrow As Long
Dim i As Long
Set ws1 = Worksheets("DG")
Set ws2 = Worksheets("Asp")
lrow = Worksheets("Asp").Range("A5", ws2.Range("A5").End(xlUp)).Rows.Count
For i = 5 To lrow
On Error Resume Next
result = Application.WorksheetFunction.VLookup((ws2.Range("A5" & i)), (ws1.Range("A11:B200")), 2, True)
ws2.Range("AG5").Value = result
If Err.Number = 0 Then
End If
On Error GoTo 0
Next i
End Sub
DG [1]: https://i.stack.imgur.com/ZrwfZ.jpg
ASP [2]: https://i.stack.imgur.com/tTsl0.jpg
It's Friday, Here you go, something to look at and study.
I am sorry I didn't use Vlookup, I have spent too much time chasing ghosts with that.
Perhaps others have had better success, I think what I don't like is Vlookup if it fails an exact match sometimes it chooses an adjacent row and throws everything into turmoil.
Here it is:
Option Explicit
Private Sub CommandButton2_Click()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lrow2 As Long
Dim lrow1 As Long
Dim firstDataRow As Double
Dim matchRange As Range
Dim matchRow As Long
Dim i As Long
'Set up your Worksheet variables
Set ws1 = ThisWorkbook.Worksheets("DG")
Set ws2 = ThisWorkbook.Worksheets("Asp")
'You used A5 several times, so I will assume dates are in Col A and start at row 5
'Set your row with first data, maybe you need two, if they are the same on both sheets you don't
firstDataRow = 5
'find the last row on each sheet, using column A, the date col
lrow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
lrow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
'In your mind you now have two data ranges, one for each sheet, rows 5-last dat rows
'Pick one sheet, the one with less dates would be more efficient, you mention DG has less
'So this might be built backwards from what you are thinking, since we are iterating each row in the col
'You want to use the shrter call, IF you know its shorter (your comments)
'Loop through each row trying to find a match on your other sheet
For i = firstDataRow To lrow1
If ws1.Cells(i, "A") <> "" Then 'Test for empty
'Here is the premise of Find/Match over Vlookup
Set matchRange = ws2.Range("A" & firstDataRow & ":A" & lrow2).Find(ws1.Cells(i, "A"))
On Error Resume Next
matchRow = matchRange.Row 'Returns row number or nothing
If (Not matchRange Is Nothing) Then
'we have a row number matched on Asp, for our search item on DG
'perform the "Copy", this can be done differently but here I am going to introduce you to a way
'that can later be used with offsets and col #s, so that you may skip columns, data is not always adjacent
ws2.Cells(matchRow, "E") = ws1.Cells(i, "B")
ws2.Cells(matchRow, "F") = ws1.Cells(i, "C")
ws2.Cells(matchRow, "G") = ws1.Cells(i, "D")
Else 'DO NOTHING
End If
Else 'DO NOTHING
End If
Next i
MsgBox "Search and Copy is complete.", vbInformation, "Completed"
End Sub
There is so much more to talk about in even this simple project to make this more bullet proof. But this is a good start for where you are at.
Cheers! Happy Coding! - WWC

Assigning a previous fieldvalue to a field in the next record

I am working on a loan amortization schedule using Access, I have created the following tables:
tblMembers - autoMemberID|txtLN|txtFN|etc
tblLoans - autoLoanID|numMemberID|etc
tblSchedules -autoScheduleID|numLoanID|numPayment#|datDue|dblBegin|...|dblEnd
tblPayments - autoPaymID|numLoanID|curAmtPaid|datPaidDate
and forms:
frmLoans - autoLoanID|etc
sbfSchedules - numPaymID|datDue|dblBegin|...|dblEnd
sbfPayments - dblAmount|datPaid
I have been able to generate the schedule (using DAO recordset) clicking a btnRepaymentSchedule the challenge is when I click a btnAddPayment still on the sbfSchedules, I want the value of the dblEnd field to be the value of the next record dblBegin field! I have tried using
dlookup(txtBeginBalance = Dlookup("EndBalance", _
"Schedules","[ScheduleID] = Form![Schedules]![ScheduleID]-1)
but there is no effect but if I use
txtBeginBalance = txtEndBalance
the value of txtEndBalance is assigned to the value of txtBeginBalance of the same record (want next record).
Secondly, how can I set focus to the next record if a contro(txtAmountPaid) is not null on clicking the btnAddPayment or assigning the value of a calculation when the control is 0 or null ?
Thanks
Have you tried a recordset instead of a (much slower) dlookup? Something like...
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("select [EndBalance] from [Schedules] where [ScheduleID] = " & Form!Schedules!ScheduleID - 1)
If Not rs.EOF Then
Forms!Schedules!DblBegin = rs!EndBalance
End If
rs.Close
Set rs = Nothing
...for the second q:
If Not IsNull(Forms!Schedules!txtAmountPaid) then
DoCmd.GoToRecord , , acNext
Else
Forms!Schedules!Your_Field_Name = calculation_goes_here
End If

set Datagirdviewcombobox column value in Vb.net

I have datagrid with two datagridviewcombo column, one column is dynamic fill and one has fixed hardcoded values.
The problem is I can't set the value of dynamic GridViewComboBox, when I try to set it generates continues errors.
System.FormateException: DataGridViewComboBoxCell Value is not valid.
My code to load the grid is
Dim dt As DataTable
dt = GetDataTable("Select valuecol,displayCol From mytable") 'GetDataTable gives me datatable
cmbAntibiotics.DataSource = dt
cmbAntibiotics.DisplayMember = "Antibiotics"
cmbAntibiotics.ValueMember = "AntibioticsID"
Dim Index As Integer
Dim dgr As DataGridViewRow
For Each dr As DataRow In dtFromDB.Rows 'This datatable is filled with database
Index = dtFromDB.Rows.Count - 1
GRDAntimicrobials.Rows.Add()
GRDAntimicrobials.Rows(Index).Cells("cmbAntibiotics").Value = dr("AntibioticsID").ToString 'At this point it shows value (1,2,3) rather then showing its display members
GRDAntimicrobials.Rows(Index).Cells("AntibioticsStatus").Value = dr("AntibioticsStatus").ToString
Next
Pls help with me
It seems like you're trying to assign the value to whatever there is on the cell rather than instantiate the object that resides in the cell and then assign its value. I would try something like this:
Dim vComboBoxColumn As DropDownList = DirectCast(GRDAntimicrobials.Rows(index).Cells("cmbAntibiotics"))
vComboBoxColumn.Value = dr("AntibioticsStatus").ToString