Word VBA: Need to remove frames but maintain formatting - vba

I currently have a word document formatted in columns and rows like such (it is about 100 rows and 6 columns in the real example):
a b c d
1 x x x x
2 x x x x
3 x x x x
4 x x x x
Everything has a separate frame, i.e. there are 24 frames in this example. However, when I remove any frame, the formatting does not remain the same (the text tends to go to the top left of the word document).
The reason this is an issue is because I cannot add to the document by typing due to the 'frame formatting' .
I converted the document from xps to docx.
I tried this VBA code which did remove the frames but did not keep the formatting.
Sub CleanUpExport()
Dim aFrame As Frame
Dim p As Paragraph
Dim l As Single
For Each aFrame In ActiveDocument.Frames
aFrame.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
l = aFrame.HorizontalPosition
For each p In aFrame.Range.Paragraphs
p.LeftIndent = l
Next p
aFrame.Delete
Next aFrame
End Sub
Any help appreciated.

If you copy the content of the frame before deleting the frame and then paste in the same location, you should get the same formatting as before

Related

Conditional Formatting custom formula to apply a red highlight to cell ranges that have 6 consecutive values

I am trying to apply a red highlight in a Google Sheets file for cell ranges that have 6 consecutive values. I have tried to apply the conditional formatting via App Scripts but this approach causes conflicts with conditional formatting rules applies directly in the front-end of Google Sheets (the conditional formatting applied directly in the "UI" of Google Sheets will overwrite the one applies from the script).
Therefore, I am looking for help with a custom formula that will allow me to apply the red highlight directly in the front-end to avoid this conflict.
The custom formula should go through every cell in the range(s) and detect cells that contain 6 consecutive values. The ranges that meet this condition should be highlighted red.
In the example below ranges A3:F3 and A5:F5 should be highlighted.
Column A
Column B
Column C
Column D
Column E
Column F
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X

Matrix Columns plot in VB.NET

I have a matrix whose dimensions are unknown until the code executes. It could be any size. I want to plot the points using different colours for each column.
Say column 1 uses one colour, column 2 uses another colour, and so on.
I have this code, but it plots all the points with the same colour. Any ideas?
Dim s1 As New System.Windows.Forms.DataVisualization.Charting.Series
s1.ChartType = SeriesChartType.Point
s1.Name = "Clusters"
For h = 1 To Num_clusters 'number of columns
For ss = 1 To jobs 'maximum row size
If clusters(ss, h) <> 0 Then 'depending of code execution, it could be rows empty, so checking only non-empty rows
s1.Points.AddXY(x(clusters(ss, h)), y(clusters(ss, h))) 'Just taking the X-Y components of the data
s1.Color = Color.FromArgb(h + 100, 162, 178) 'Here trying to make all columns different colours with the h+100 (h=column index)
End If
Next 'Row
'after reading the rows from this column, add the points with a colour
'Chart1.Series.Add(s1) 'If I run this here an error happens and it doesn't plot
Next 'Column
'If I add chart series here would plot all with the same colour
I was thinking if the first loop h something like this
For h = 1 To Num_clusters 'number of columns
Dim (s & h) As New Series 'Trying to automatically create new series: s1,s2,...,sNum_clusters
For ss = 1 To jobs 'maximum row size
'code
'
'
'
Next 'rows
'Chart1.Series.Add(s & h)
Next 'column
But I don't know what the correct syntax to auto create new series s and h would be.
How can I fix this?

I'm trying to make a cumulative distribution chart in VBA

First time poster. To make this cumulative disti chart I make a straight lined xy scatter plot. The y axis values are the percentiles, all of which are in A2:A100. The corresponding x values are in Columns B through n. n being variable as it depends on how many simulated strategies are designated by the user for chart creation. I need the VBA code to create an xy scatter plot where A2:A100 is the y values for all x value series that are located in the adjascent columns. A named cell, cumulativeColumns counts the number columns with x values. My first thought based on my research of how SeriesCollections seemed to work was to use a Do Until Loop that would stop based on the result of the cumulativeColumns formula. I thought this code would work:
q = 1
Do Until q = Range("cumulativeColumns").Value
ActiveChart.SeriesCollection(q).XValues = _
"=Range(Cells(2,q+1), Cells(100,q+1))"
ActiveChart.SeriesCollection(q).Values = _
"=Range(Cells(2,1), Cells(100,1))"
q = q + 1
Loop
The first time through the loop q = 1 so the 1st series will get Col B data. The second time through the loop q = 2 so I figured the 2nd series would be defined and get the data in Column C, etc. until it stops when there are no more columns with data per the result of the cumulativeColumns.Value. But I get an error 1004, application defined or object defined error.
Tested:
q = 1
Do Until q = Range("cumulativeColumns").Value
With ActiveChart.SeriesCollection.NewSeries
.XValues = Range(Cells(2, q + 1), Cells(100, q + 1))
.Values = Range(Cells(2, 1), Cells(100, 1))
q = q + 1
End With
Loop

How to summarize/compress data and show multiple answers in 1 cell

So I'm neck deep in this macro that I've created. My peers are requesting more on outputs than I had originally displayed. I need a macro that can do the following. So the question I have could normally be solved with VLOOKUP. However, in this case it is very important to know if there are really two answers and what both would be, making VLOOKUP obsolete. So lets say I have 2 columns. One showing Run # and the other showing Vertical, Curve, or Lateral (they progress down the column in that order). lets say Run #1 is Vertical. Run #2 began vertical, but finished curved. Run #3 is curve, but finished lateral. Finally, run #4 is lateral.Example:
Run# Section
1 Vertical
1 Vertical
1 Vertical
2 Vertical
2 Vertical
2 Curve
2 Curve
3 Curve
3 Curve
3 Lateral
3 Lateral
4 Lateral
4 Lateral
4 Lateral
4 Lateral
What I need is this below. Basically before I would preform this, I would be using a pivot table with Run# in "Rows" and then collecting other data in it as well. I then will copy and paste the value's only to a different sheet where then I will need to add in the values in "section" as shown below.
Run# Section
1 Vertical
2 Vertical & Curve
3 Curve & Lateral
4 Lateral
Thanks for taking a look at this guys. I've tried finding something on this, but nothing shows multiple results in the same cell. Any help is greatly appreciated! Have a great day!
I have a udf called JOINDISTINCT (code below) - you can use this in conjunction with an array formula to do what you need.
For the formula, assuming that your run data is in columns A and B and that column E has the distinct run list, in F2 have the array formula (entered with Ctrl+Shift+Enter)
=JOINDISTINCT(IF($A$1:$A$16=E2,$B$1:$B$16,"")," & ")
Note that we don't use the whole column, just the populated range, in order to speed up execution. The array part checks column A values to be matching the run number in E2 and then will return the Section or blank. The JOINDISINCT takes that array and returns the distinct list, separated by (in this case) " & "
VBA Code for the function
Public Function JOINDISTINCT(rng As Variant, Optional separator = ", ") As String
JOINDISTINCT = ""
Dim v As String
'Dictionary needs REFERENCE: Microsoft Scripting Runtime
Dim dict As New Dictionary
For Each c In rng
If IsObject(c) Then
v = c.Text
Else
v = c
End If
If v <> "" Then
If Not (dict.Exists(v)) Then
dict.Add v, 1
If JOINDISTINCT = "" Then
JOINDISTINCT = v
Else
JOINDISTINCT = JOINDISTINCT & separator & v
End If
End If
End If
Next c
Set dict = Nothing
End Function

Trying to move rows with VBA but I am getting an error and I don't know how to fix it

So I have a code that goes through a spreadsheet (About 150,000 lines) and highlights specific ones red. Now, I'm trying to move those selected lines and here is the code I have:
Dim x As Long, iInsertedRows As Integer
x = Range("A1").End(xlDown).Row
For x = x To 1 Step -1
If Cells(x, 1).Interior.ColorIndex = 3 Then '3 is standard red color
Rows(x).Cut
'''The next row is where it breaks'''
Rows("1:1").Insert shift:=xlDown 'paste the cut row back as row 1
x = x + 1 'we've just moved everything down a row
iInsertedRows = iInsertedRows + 1
If iInsertedRows = x Then Exit For 'we don't need to sort the ones we put there
End If
Next
(Source: http://p2p.wrox.com/excel-vba/6791-move-rows-top-cells-columna-filled-red.html)
The issue is that I am getting an error that reads:
Run-time error '1004':
This selection is not valid.
There are several possible reasons:
Copy and paste areas cannot overlap unless they are the same size
If you are using the Create from Selection Command, the row or column containing the proposed names will not be included in the... (It gets cut off by the bottom of the error box).