how to fill other part of a table - vba

I have in cells A1 19,200, B1 13/05/2020 and cells C1 72. When i execute the VBA a table is created in Word as per below and it continues to 72
Instal No Amt(Rs) Due Date Instal No Amt(Rs) Due Date
1 19200 13/05/2020
2 19200 13/06/2020
3 19200 13/07/2020
4 19200 13/08/2020
5 19200 13/09/2020
6 19200 13/10/2020
7 19200 13/11/2020
8 19200 13/12/2020
9 19200 13/01/2021
10 19200 13/02/2021
11 19200 13/03/2021
12 19200 13/04/2021
13 19200 13/05/2021
14 19200 13/06/2021
15 19200 13/07/2021
16 19200 13/08/2021
Please note that C1 is the number of months(i,e Instal No).
What i want to achieve is to fill the other part to the right of the blank of the table.Let me clarify if C1= 72 months then split it half that is send 36 months to the other side of the table.My number of months are even numbers(24,36,48,60,98)
You will notice that i have added 1 to "lngRows = Range("C1").Value + 1" because of the headings
my codes are as follows :-
Sub CreateTableInWord()
Dim objWord As Object
Dim objDoc As Object
Dim objTbl As Object
Dim objRow As Object
Dim objCol As Object
Dim lngCols As Long
Dim lngRows As Long
Dim I As Long
lngCols = 6
lngRows = 72
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add(DocumentType:=0)
Set objTbl = objDoc.Tables.Add(Range:=objDoc.Paragraphs(1).Range, NumRows:=lngRows, NumColumns:=lngCols)
Set objRow = objTbl.Rows(1)
objTbl.Cell(1, 1).Range.Text = "Instal No"
objTbl.Cell(1, 1).Range.Bold = True
objTbl.Cell(1, 2).Range.Text = "Amt(Rs)"
objTbl.Cell(1, 2).Range.Bold = True
objTbl.Cell(1, 3).Range.Text = "Due Date"
objTbl.Cell(2, 3) = Range("B1").Value
objTbl.Cell(1, 3).Range.Bold = True
objTbl.Cell(1, 4).Range.Text = "Instal No"
objTbl.Cell(1, 4).Range.Bold = True
objTbl.Cell(1, 5).Range.Text = "Amt(Rs)"
objTbl.Cell(1, 5).Range.Bold = True
objTbl.Cell(1, 6).Range.Text = "Due Date"
objTbl.Cell(1, 6).Range.Bold = True
objTbl.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
For I = 2 To lngRows
' For j = 1 To intNoOfColumns
objTbl.Cell(I, 1).Range = I - 1
Next
For S = 2 To lngRows
objTbl.Cell(S, 2) = Range("A1").Value
Next
For T = 2 To lngRows
objTbl.Cell(T, 3).Range.Text = Format(DateAdd("m", T - 2, Range("B1").Value), "dd/mm/yyyy")
Next T
Set objCol = Nothing
Set objRow = Nothing
Set objDoc = Nothing
Set objWord = Nothing
End Sub

Try this out:
Sub CreateTableInWord()
Dim objWord As Object, objDoc As Object, objTbl As Object, objRow As Object
Dim objCol As Object, colSets As Long, numMonths As Long, i As Long, n As Long, c As Long
Dim amt, dtStart, tblRows As Long, tblCols As Long, rw As Long, col As Long
numMonths = Range("A1").Value
amt = Range("B1").Value
dtStart = Range("C1").Value
colSets = Range("D1").Value 'how many sets of columns ?
tblRows = 1 + Application.Ceiling(numMonths / colSets, 1) 'how many table rows?
tblCols = colSets * 3 'how many table cols?
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add(DocumentType:=0)
Set objTbl = objDoc.Tables.Add(Range:=objDoc.Paragraphs(1).Range, _
NumRows:=tblRows, NumColumns:=tblCols)
c = 0
For n = 1 To colSets
objTbl.Cell(1, c + 1).Range.Text = "Instal No"
objTbl.Cell(1, c + 1).Range.Bold = True
objTbl.Cell(1, c + 2).Range.Text = "Amt(Rs)"
objTbl.Cell(1, c + 2).Range.Bold = True
objTbl.Cell(1, c + 3).Range.Text = "Due Date"
objTbl.Cell(1, c + 3).Range.Bold = True
c = c + 3
Next n
objTbl.Range.ParagraphFormat.Alignment = 1 ' wdAlignParagraphCenter
rw = 2
col = 0
For i = 1 To numMonths
'rw = 1 + Application.Ceiling(i / colSets, 1) 'fill across and then down
rw = IIf(i Mod (tblRows - 1) = 1, 2, rw + 1) 'fill down then across
objTbl.Cell(rw, col + 1).Range.Text = i
objTbl.Cell(rw, col + 2).Range.Text = amt
objTbl.Cell(rw, col + 3).Range.Text = DateAdd("m", i - 1, dtStart)
'col = IIf(i Mod colSets = 0, 0, col + 3) 'fill across and then down
col = IIf(i Mod (tblRows - 1) = 0, col + 3, col) 'fill down and then across
Next i
End Sub

Related

Find string in table VBA Powerpoint

After 2 days trying to find a solution to my problem, I need your help please.
I'm working on powerpoint VBA script, and I've got a Table (3,3). In the row 1, I've already input some string in cells.
I want to know why my script doesn't want to write NOK in cells when the string does'nt match "comp" for example
Here is my VBA script:
Public Sub CreateTable1()
' déclaration of variables
Dim objSld As Slide
Dim objShp As Shape
Dim foundText1 As Object
Dim FindWhat As String
Dim I As Integer
Dim j As Integer
Set objSld = ActivePresentation.Slides(1)
Set objShp = objSld.Shapes.AddTable(3, 3, 15, 150, 700, 500)
' Give a name to table
objShp.Name = "Table1"
' Define size of cells
With objSld.Shapes("Table1").Table
.Columns(1).Width = 115
.Columns(2).Width = 115
.Columns(3).Width = 115
.Rows(1).Height = 120
.Rows(2).Height = 120
.Rows(3).Height = 120
'Write in cells
With .Cell(1, 1).Shape.TextFrame
.TextRange.Text = "Composition"
End With
With .Cell(2, 1).Shape.TextFrame
.TextRange.Text = "Material"
End With
With .Cell(3, 1).Shape.TextFrame
.TextRange.Text = "Method"
End With
' Define text position
For I = 1 To 3
For j = 1 To 3
With .Cell(j, I).Shape.TextFrame
.VerticalAnchor = msoAnchorMiddle
.HorizontalAnchor = msoAnchorCenter
.TextRange.Font.Size = 18
End With
Next j
Next I
'Command find
'Browse row 1 from line 1 to 3
For x = 1 To 3
Set foundText1 = objSld.Shapes("Table1").Table.Cell(x, 1).Shape.TextFrame.TextRange.Find(FindWhat:="Comp")
If foundText1 = "Comp" Then
'MsgBox foundText1 & x
'Will write in cell (x,2) OK and x
objSld.Shapes("Table1").Table.Cell(x, 2).Shape.TextFrame.TextRange.Text = "OK " & x
Else
'Will write in cell (x,2) NOK and x
'Doesn't works !! Why??
objSld.Shapes("Table1").Table.Cell(x, 2).Shape.TextFrame.TextRange.Text = "NOK " & x
End If
Next x
End With
End Sub
I Would like to know if you see where is the mistake. The function Else seems not working..
I found the solution !!
For those who're lost with this same problem, here is my code:
Public Sub CreateTable1()
' déclaration of variables
Dim objSld As Slide
Dim objShp As Shape
Dim foundText1 As Object
Dim TextRng As TextRange
Dim FindWhat As String
Dim I As Integer
Dim j As Integer
Set objSld = ActivePresentation.Slides(8)
Set objShp = objSld.Shapes.AddTable(3, 3, 15, 150, 700, 500)
' Give a name to table
objShp.Name = "Table1"
' Define size of cells
With objSld.Shapes("Table1").Table
.Columns(1).Width = 115
.Columns(2).Width = 115
.Columns(3).Width = 115
.Rows(1).Height = 120
.Rows(2).Height = 120
.Rows(3).Height = 120
'Write in cells
With .Cell(1, 1).Shape.TextFrame
.TextRange.Text = "Composition"
End With
With .Cell(2, 1).Shape.TextFrame
.TextRange.Text = "Material"
End With
With .Cell(3, 1).Shape.TextFrame
.TextRange.Text = "Method"
End With
' Define text position
For I = 1 To 3
For j = 1 To 3
With .Cell(j, I).Shape.TextFrame
.VerticalAnchor = msoAnchorMiddle
.HorizontalAnchor = msoAnchorCenter
.TextRange.Font.Size = 18
End With
Next j
Next I
'Command find
'Browse row 1 from line 1 to 3
End With
End Sub
Creation of a second sub to understand where does script failed
Sub yolo()
Dim objSld As Slide
Dim oTbl As Table
Dim lRow As Long
Dim lCol As Long
Dim foundText1 As Object
Set objSld = ActivePresentation.Slides(8)
Set oTbl = objSld.Shapes("Table1").Table
With oTbl
For lRow = 1 To .Rows.Count
With .Cell(lRow, 1).Shape
'Do something with each cell's text
'Does this shape has text?
If .HasTextFrame Then
Set TextRng = oTbl.Cell(lRow, 1).Shape.TextFrame.TextRange
Set foundText1 = TextRng.Find(FindWhat:="Comp")
Do While Not (foundText1 Is Nothing)
With foundText1
oTbl.Cell(lRow, 2).Shape.TextFrame.TextRange.Text = "OK"
Set foundText1 = TextRng.Find(FindWhat:="Comp", After:=.Start + .Length - 1)
End With
Loop
End If
End With
Next lRow
End With
End Sub

search combination of words in worksheet using VBA

I need to find the row which as combination of words (Keyword - Column 1 , keyword 2 - column 2 , keyword 3 - column 3) of sheet1 with sheet 2 which as more than 800 rows and 275 columns.
I have done a coding but it gives a result as "not responding". Please help me to sort out this issue.
below is the coding:-
Private Sub CommandButton1_Click()
Dim keyword As String
Dim keyword1 As String
Dim keyword2 As String
Dim keyword3 As String
Dim k As Long
Dim k1 As Long
Application.ScreenUpdating = False
Set XML = ThisWorkbook.Worksheets("XML")
Set rn = XML.UsedRange
k = rn.Rows.Count + rn.Row - 1
Debug.Print (k)
For i = 1 To k
k1 = rn.Columns.Count + rn.Column - 1
Debug.Print (k1)
For j = 1 To k1
cellAYvalue = XML.Cells(i, j)
For a = 2 To 261
MatchAttempt = 0
keyword_Flag = False
keyword1_Flag = False
keyword2_Flag = False
keyword3_Flag = False
keyword4_Flag = False
keyword5_Flag = False
keyword = Trim(UCase(ThisWorkbook.Worksheets("Keyword").Cells(a, 2)))
keyword1 = Trim(UCase(ThisWorkbook.Worksheets("Keyword").Cells(a, 3)))
keyword2 = Trim(UCase(ThisWorkbook.Worksheets("Keyword").Cells(a, 4)))
keyword3 = Trim(UCase(ThisWorkbook.Worksheets("Keyword").Cells(a, 5)))
keyword4 = Trim(UCase(ThisWorkbook.Worksheets("Keyword").Cells(a, 6)))
keyword5 = Trim(UCase(ThisWorkbook.Worksheets("Keyword").Cells(a, 7)))
If keyword <> "" Then
keyword_Flag = True: MatchAttempt = MatchAttempt + 1
End If
If keyword1 <> "" Then
keyword1_Flag = True: MatchAttempt = MatchAttempt + 1
End If
If keyword2 <> "" Then
keyword2_Flag = True: MatchAttempt = MatchAttempt + 1
End If
If keyword3 <> "" Then
keyword3_Flag = True: MatchAttempt = MatchAttempt + 1
End If
If keyword4 <> "" Then
keyword4_Flag = True: MatchAttempt = MatchAttempt + 1
End If
If keyword5 <> "" Then
keyword5_Flag = True: MatchAttempt = MatchAttempt + 1
End If
MatchedCount = 0
Description = Trim(UCase(cellAYvalue = XML.Cells(i, j)))
Description1 = Trim(UCase(cellAYvalue = XML.Cells(i, j)))
Description2 = Trim(UCase(cellAYvalue = XML.Cells(i, j)))
Description3 = Trim(UCase(cellAYvalue = XML.Cells(i, j)))
Description4 = Trim(UCase(cellAYvalue = XML.Cells(i, j)))
Description5 = Trim(UCase(cellAYvalue = XML.Cells(i, j)))
EXITloop = Trim(UCase(ThisWorkbook.Worksheets("Keyword").Cells(a, 1)))
If EXITloop = "" Then
Exit For
End If
MatchComplete = False
If keyword_Flag = True Then
If keyword = Description Then
MatchedCount = MatchedCount + 1
If MatchAttempt = MatchedCount Then MatchComplete = True
End If
End If
If keyword_Flag1 = True Then
If keyword1 = Description1 Then
MatchedCount = MatchedCount + 1
If MatchAttempt = MatchedCount Then MatchComplete = True
End If
End If
If keyword_Flag2 = True Then
If keyword2 = Description2 Then
MatchedCount = MatchedCount + 1
If MatchAttempt = MatchedCount Then MatchComplete = True
End If
End If
If keyword_Flag3 = True Then
If keyword3 = Description3 Then
MatchedCount = MatchedCount + 1
If MatchAttempt = MatchedCount Then MatchComplete = True
End If
End If
If keyword_Flag4 = True Then
If keyword4 = Description4 Then
MatchedCount = MatchedCount + 1
If MatchAttempt = MatchedCount Then MatchComplete = True
End If
End If
If keyword_Flag5 = True Then
If keyword5 = Description5 Then
MatchedCount = MatchedCount + 1
If MatchAttempt = MatchedCount Then MatchComplete = True
End If
End If
inin = Trim(UCase(ThisWorkbook.Worksheets("XML").Cells(i, 112)))
ouou = Trim(UCase(ThisWorkbook.Worksheets("Keyword").Cells(a, 8)))
If MatchComplete = True Then
ouou = inin
End If
a = a + 0
Next
j = j + 0
Next
i = i + 0
Next
Application.ScreenUpdating = True
MsgBox "Completed"
End Sub
Edit: More details
I have a workbook with two worksheets
Sheet 1 is having “N” number of data with 807 rows and 277 column
Sheet 2 is having the standard keyword combination (201 combinations) set.
Note: - each combination from sheet 2 can be available in any of the row or columns of sheet 1, But combination match should be in row wise alone.
Requirements: - Need to find the keyword combination from sheet 2 in sheet 1 once the combination found in sheet 1 we need to fetch the output.
Sheet 1 (Data Sheet)
Sheet 2 (Keyword Sheet)
Searching keywords from sheet 2 in sheet 1
keywords can find in many cells of sheet 1 (Yellow highlighted) but the combination will be find in only one row and we need to find that row (Green highlighted)
once we have found the row in sheet 1 which has the combination we need to fetch the fourth value from the last combination word and paste it in the 10th column of sheet 2.
E.g
in sheet 1
we have found the combination 100th row
in that row keyword 1 in (100,20)
keyword 2 in (100,40)
keyword 3 in (100,60)
then output should be need to copy the value from cell (100,64) in sheet 1 then need to paste in 10th column of sheet 2 to respective combination row of sheet 2.
This identifies Sheet2 rows in Sheet1, based on the first 3 columns as keywords
Once a record is found, it copies value from the 3rd col in Sheet1 in the 10th column of Sheet2
Option Explicit
Private Sub CommandButton1_Click()
Const FR = 2 'Start row
Const KC = 3 'Last Keyword column
Const TC = 10 'Target column
Dim ws1 As Worksheet: Set ws1 = Sheet1 'Or: ThisWorkbook.Worksheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = Sheet2
Dim lr1 As Long: lr1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
Dim lr2 As Long: lr2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
Dim arr1 As Variant: arr1 = ws1.Range(ws1.Cells(FR, 1), ws1.Cells(lr1, KC))
Dim arr2 As Variant: arr2 = ws2.Range(ws2.Cells(FR, 1), ws2.Cells(lr2, KC))
Dim d1 As Object: Set d1 = CreateObject("Scripting.Dictionary")
Dim d2 As Object: Set d2 = CreateObject("Scripting.Dictionary")
Dim dr As Object: Set dr = CreateObject("Scripting.Dictionary") 'Result
LoadDictionary d1, arr1
LoadDictionary d2, arr2
GetKeywords d2, d1, dr
Dim r As Long
arr2 = ws2.Range(ws2.Cells(FR, 1), ws2.Cells(lr2, TC))
If dr.Count > 0 Then
For r = 1 To lr2
If dr.Exists(r) Then arr2(r, TC) = arr2(r, KC) 'Or arr2(r, TC) = dr(r)
Next
End If
ws2.Range(ws2.Cells(FR, 1), ws2.Cells(lr2, TC)) = arr2
End Sub
Private Sub LoadDictionary(ByRef d As Object, arr As Variant) 'Expects 2-d array
Dim r As Long, c As Long, k As String
For r = 1 To UBound(arr, 1)
k = "|"
For c = 1 To UBound(arr, 2)
k = k & arr(r, c) & "|" 'Concatenate all columns
Next
d(k) = r
Next
End Sub
Private Sub GetKeywords(ByRef d1 As Object, ByRef d2 As Object, ByRef dr As Object)
Dim r As Long, k As String, arr As Variant
For r = 0 To d1.Count - 1
k = d1.Keys()(r)
If d2.Exists(k) Then
arr = Split(k, "|")
dr(d1(k)) = arr(UBound(arr) - 1)
End If
Next
End Sub
.
Test Sheet 1
Test Sheet 2
Sheet1 Rows: 1,001, Cols: 501; Sheet2 Rows: 1,001, Cols: 501 - Time: 0.023 sec
New info:
Row 1 - Keyword 1 , keyword 2 , keyword 3 (once we find the row with
this order then we need to fetch the 4th value from keyword 3 in the
same row) and paste in 10 column of sheet 2

So how to I reference the value of one cell as a series name using VBA?

this question is probably easy to solve but I cannot figure out how to do it and a quick web search didn't lead to anything. So here is my code:
Option Explicit
'Description: This macro is used to number plot all specimens into the stress-strain curve since this has become a task that
'has to be done very frequently
Sub PlotAllSpecimens_Tensile()
Dim ws As Worksheet, wb As Workbook
Dim xrng As Range, yrng As Range, namerng As Range
Dim CH As Chart, CHcond As Chart, CHdry As Chart
Dim Material As String, state As String, Temperatur As String, name As String
Dim i As Integer, j As Integer, idry As Integer, icond As Integer, k As Integer
Dim ser As series
Dim startrow As Integer
Set wb = ActiveWorkbook
idry = 1
icond = 1
For Each CH In wb.Charts
If CH.name = "Stress-Strain curve cond" Then
Set CHcond = CH
ElseIf CH.name = "Stress-Strain curve dry" Then
Set CHdry = CH
End If
Next CH
If Not CHdry Is Nothing Then
For Each ser In CHdry.SeriesCollection
ser.Delete
Next ser
End If
If Not CHcond Is Nothing Then
For Each ser In CHcond.SeriesCollection
ser.Delete
Next ser
End If
For Each ws In wb.Worksheets
If ws.name <> "Start" And ws.name <> "Auswertung" And ws.name <> "Zusammenfassung" Then
i = 1
For k = 1 To 15
If ws.Cells(k, 5 * i - 4) = "Material" Or ws.Cells(k, 5 * i - 4) = "Werkstoff" Then
Material = ws.Cells(k, 5 * i - 3).Value
ElseIf ws.Cells(k, 5 * i - 4) = "Temperatur" Then
Temperatur = ws.Cells(k, 5 * i - 3).Value
ElseIf ws.Cells(k, 5 * i - 4) = "Zustand" Then
state = ws.Cells(k, 5 * i - 3).Value
End If
Next k
While Not IsEmpty(ws.Cells(i * 5 - 4).Value)
name = Material & "_" & i & ", " & state & ", " & Temperatur
Set namerng = ws.Cells(1, 5 * i - 1).End(xlDown).Offset(-1, -1)
namerng.Value = name
startrow = ws.Cells(1, 5 * i - 1).End(xlDown).Row
Set xrng = Range(ws.Cells(startrow, 5 * i - 2), ws.Cells(startrow, 5 * i - 2).End(xlDown))
Set yrng = Range(ws.Cells(startrow, 5 * i - 1), ws.Cells(startrow, 5 * i - 1).End(xlDown))
If Not (CHdry Is Nothing) And state = "dry" Then
CHdry.SeriesCollection.NewSeries
CHdry.SeriesCollection(idry).XValues = xrng
CHdry.SeriesCollection(idry).Values = yrng
CHdry.SeriesCollection(idry).name = ??????????
CHdry.SeriesCollection(idry).Border.ColorIndex = 42 + ws.Index Mod 5
idry = idry + 1
End If
If Not (CHcond Is Nothing) And state = "conditioned" Then
CHcond.SeriesCollection.NewSeries
CHcond.SeriesCollection(icond).XValues = xrng
CHcond.SeriesCollection(icond).Values = yrng
CHcond.SeriesCollection(icond).Border.ColorIndex = 42 + ws.Index Mod 5
CHcond.SeriesCollection(icond).name = ???????
icond = icond + 1
End If
i = i + 1
Wend
End If
Next ws
End Sub
????? marks the issue. I want to name my series the value of the cell "namerng" so if i later change this cell the name in the plot will update. This can be done manually in excel by selecting a cell as the name range. If I use:
CHcond.SeriesCollection(icond).name = namerng.value
The results will be correct but do not change after I change the value of the namerng. So how to I reference the value of one cell as a series name using VBA?
I got it it is:
CHcond.SeriesCollection(icond).name = "='" & ws.name & "'!" & namerng.Address
Not very elegant but it works.

excel vba loop max value and location more sheets

I need the location of max value (one column) or address, so i can locate two cells left of the max value cell. next is finding the higher value of the two new cells and dividing the higher value with the max value. last step is returning the value to sheet "List1". that s the basic logic :)
thx for any help
the locating of max value and locating cells left of it, that is my main concern.
i cant figure it out. been looking for it but cant get it to work.
Sub DoIt()
'ONE MAIN SHEET (List1)
'MORE SECONDARY SHEETS WHERE DATA FOR MAX VALUE IS
Dim strSheet As String
Dim rng As Range
Dim dblMax As Double
Dim r As Range
i = 4
j = 8
g = 4
h = 20
s = 22
Dim cell As Range
Dim col, row
Do While Cells(i, j).Value <> ""
strSheet = Sheets(1).Cells(i, j)
Sheets(strSheet).Activate
'w = 2
'e = 27
'a = 2
'Do While Cells(a, s).Value >= "0"
'Range("AA1") = "IT WORKS"
'Cells(w, e) = Cells(a, s).Value
'a = a + 1
'w = w + 1
'Loop
Set rng = Sheets(strSheet).Range("V2:V8761")
dblMax = Application.WorksheetFunction.Max(rng)
'CODE FOR MAX VALUE LOCATION
'LOCATING TWO LEFT CELLS OF LOCATION MAX VALUE CELL
'DETERMINING THE HIGHER VALUE
'DIVIDING
Range("Z2") = dblMax 'CONTROL
i = i + 1
Range("Z1") = "IT WORKS" 'CONTROL
Sheets("List1").Activate
Cells(g, h) = "AAA" 'result of higher value cell by max value cell
g = g + 1
Loop
End Sub
thx for help i did it. code is not refind. here is the code:
Sub DoIt()
Dim strSheet As String
Dim rng As Range
Dim dblMax As Double
Dim r As Range
Dim dely As Double
i = 4
j = 8
g = 4
h = 20
l = 21
s = 22
Dim cell As Range
Dim col, row
Do While Cells(i, j).Value <> ""
strSheet = Sheets(1).Cells(i, j)
Sheets(strSheet).Activate 'error on no strsheet
w = 2
e = 27
a = 2
sumall = Application.Sum(Range("v2:v9000"))
'Do While Cells(a, s).Value >= "0"
'Range("AA1") = "nekaj dela"
'Cells(w, e) = Cells(a, s).Value
'a = a + 1
'w = w + 1
'Loop
Set rng = Sheets(strSheet).Range("V2:V9000")
dblMax = Application.WorksheetFunction.Max(rng)
mmm = Application.WorksheetFunction.Match(dblMax, Sheets(strSheet).Range("v:v"), 0)
positionRange = Sheets(strSheet).Range("v:v")
'iii = Application.WorksheetFunction.Index(positionRange, mmm)
'ooo = mmm.Offset(0, -1)
sum1 = Cells(mmm, 12)
sum2 = Cells(mmm, 21)
If sum1 > sum2 Then
'sumall = Application.Sum(Range("l2:l8761"))
PLDP = sumall / 365
dely = sum1 / PLDP * 100
smer = "1"
Else
'sumall = Application.Sum(Range("u2:u8761"))
PLDP = sumall / 365
dely = sum2 / PLDP * 100
smer = "2"
End If
'Range("AA2") = iii
Range("AB2") = sum1
Range("AC2") = sum2
Range("ad2") = dely
Range("ae2") = sumall
Range("af2") = PLDP
Range("Z2") = dblMax 'test cell
i = i + 1
Range("Z1") = "IT WORKS"
Sheets("List1").Activate
Cells(g, h) = smer
Cells(g, l) = dely
g = g + 1
Loop
End Sub

VBA Inserting Chart At Same Position In Each Page

I extracted the values of each cell from the table in word document, and I created charts based on those values. The charts are fine.
However, it keep insert at the first page. Does anyone know how can I insert my chart in at same position in each page?
The word document generated by Mail Merge. Will that cause the problem?
Also, dose anyone know how to insert a chart into table cell?
Dim pge As Page
Dim i As Integer
i = 3
Dim j As Integer
j = 1
For peg = 1 To Selection.Information(wdNumberOfPagesInDocument)
Dim tTable As Table
Set tTable = ActiveDocument.Tables(i)
Set cTable = ActiveDocument.Tables(j)
Dim wChart As Chart
Dim chartWorkSheet As Excel.Worksheet
Dim ThisYrSumCon As Integer
Dim ThisYrWinCon As Integer
Dim PreYrSumCon As Integer
Dim PreYrWinCon As Integer
Dim BefPreYrSumCon As Integer
Dim BefPreYrWinCon As Integer
'•
ThisYrSumCon = CInt(Left(tTable.Cell(2, 2).Range.Text, Len(tTable.Cell(3, 2).Range.Text)))
ThisYrWinCon = CInt(Left(tTable.Cell(3, 2).Range.Text, Len(tTable.Cell(3, 2).Range.Text) - 1))
PreYrSumCon = CInt(Left(tTable.Cell(2, 3).Range.Text, Len(tTable.Cell(3, 2).Range.Text)))
PreYrWinCon = CInt(Left(tTable.Cell(3, 3).Range.Text, Len(tTable.Cell(3, 2).Range.Text)))
BePreYrSumCon = CInt(Left(tTable.Cell(2, 4).Range.Text, Len(tTable.Cell(3, 2).Range.Text)))
BePreYrWinCon = CInt(Left(tTable.Cell(3, 4).Range.Text, Len(tTable.Cell(3, 2).Range.Text)))
'MsgBox (ThisYrSumCon)
'cTable.Cell(3, 4).Range.Text = "test"
'cTable.Cell(12, 3).Range.Text = "test"
Set wChart = ActiveDocument.Shapes.AddChart.Chart
With wChart.Parent
.Top = 105
.Left = 205
.Width = 300
.Height = 150
End With
Set chartWorkSheet = wChart.ChartData.Workbook.WorkSheets(1)
chartWorkSheet.ListObjects("Table1").Resize chartWorkSheet.Range("A1:G2")
chartWorkSheet.Range("Table1[[#Headers],[Series 1]]").FormulaR1C1 = "Water Consumption Records"
wChart.ChartType = xlColumnClustered
chartWorkSheet.Range("A1").FormulaR1C1 = ""
chartWorkSheet.Range("B1").FormulaR1C1 = "2012 Summer"
chartWorkSheet.Range("C1").FormulaR1C1 = "2012 Winter"
chartWorkSheet.Range("D1").FormulaR1C1 = "2013 Summer"
chartWorkSheet.Range("E1").FormulaR1C1 = "2013 Winter"
chartWorkSheet.Range("F1").FormulaR1C1 = "2014 Summer"
chartWorkSheet.Range("G1").FormulaR1C1 = "2014 Winter"
chartWorkSheet.Range("A2").FormulaR1C1 = ""
chartWorkSheet.Range("B2").FormulaR1C1 = BePreYrSumCon
chartWorkSheet.Range("C2").FormulaR1C1 = BePreYrWinCon
chartWorkSheet.Range("D2").FormulaR1C1 = PreYrSumCon
chartWorkSheet.Range("E2").FormulaR1C1 = PreYrWinCon
chartWorkSheet.Range("F2").FormulaR1C1 = ThisYrSumCon
chartWorkSheet.Range("G2").FormulaR1C1 = ThisYrWinCon
wChart.ChartData.Workbook.Application.Quit
i = i + 5
j = j + 5
Selection.GoTo What:=wdGoToPage, Which:=lNextPage
Next
Lol, I am so happy that I can answer my own question... :)
Here's the answer for creating a chart base on the same format word table in each page, and put the chart at same spot each page.
The i Integer is for me to find the same table in each page.
Dim Rng As Range, pg As Long
Dim i As Integer
i = 3
With ActiveDocument
Set Rng = .Range(0, 0)
For pg = 1 To .ComputeStatistics(wdStatisticPages)
Set Rng = Rng.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pg)
Rng.Collapse wdCollapseStart
Dim tTable As Table
Set tTable = ActiveDocument.Tables(i)
Dim wChart As Chart
Dim chartWorkSheet As Excel.Worksheet
Dim ThisYrSumCon As Integer
Dim ThisYrWinCon As Integer
Dim PreYrSumCon As Integer
Dim PreYrWinCon As Integer
Dim BefPreYrSumCon As Integer
Dim BefPreYrWinCon As Integer
ThisYrSumCon = CInt(Left(tTable.Cell(2, 2).Range.Text, Len(tTable.Cell(2, 2).Range.Text) - 1))
ThisYrWinCon = CInt(Left(tTable.Cell(3, 2).Range.Text, Len(tTable.Cell(3, 2).Range.Text) - 1))
PreYrSumCon = CInt(Left(tTable.Cell(2, 3).Range.Text, Len(tTable.Cell(2, 3).Range.Text) - 1))
PreYrWinCon = CInt(Left(tTable.Cell(3, 3).Range.Text, Len(tTable.Cell(3, 3).Range.Text) - 1))
BePreYrSumCon = CInt(Left(tTable.Cell(2, 4).Range.Text, Len(tTable.Cell(2, 4).Range.Text) - 1))
BePreYrWinCon = CInt(Left(tTable.Cell(3, 4).Range.Text, Len(tTable.Cell(3, 4).Range.Text) - 1))
Set wChart = .Shapes.AddChart(xlColumnClustered, 270, 105, 230, 150, Rng).Chart
Set chartWorkSheet = wChart.ChartData.Workbook.WorkSheets(1)
chartWorkSheet.ListObjects("Table1").Resize chartWorkSheet.Range("A1:G2")
chartWorkSheet.Range("Table1[[#Headers],[Series 1]]").FormulaR1C1 = "Water Consumption Records"
chartWorkSheet.Range("A1").FormulaR1C1 = ""
chartWorkSheet.Range("B1").FormulaR1C1 = "2012 Summer"
chartWorkSheet.Range("C1").FormulaR1C1 = "2012 Winter"
chartWorkSheet.Range("D1").FormulaR1C1 = "2013 Summer"
chartWorkSheet.Range("E1").FormulaR1C1 = "2013 Winter"
chartWorkSheet.Range("F1").FormulaR1C1 = "2014 Summer"
chartWorkSheet.Range("G1").FormulaR1C1 = "2014 Winter"
chartWorkSheet.Range("A2").FormulaR1C1 = ""
chartWorkSheet.Range("B2").FormulaR1C1 = BePreYrSumCon
chartWorkSheet.Range("C2").FormulaR1C1 = BePreYrWinCon
chartWorkSheet.Range("D2").FormulaR1C1 = PreYrSumCon
chartWorkSheet.Range("E2").FormulaR1C1 = PreYrWinCon
chartWorkSheet.Range("F2").FormulaR1C1 = ThisYrSumCon
chartWorkSheet.Range("G2").FormulaR1C1 = ThisYrWinCon
wChart.ChartData.Workbook.Application.Quit
i = i + 5
j = j + 5
Next
End With