I have written a code (excel VBA) to output powerpoint report. Part of the code is to create a table, then supply the data into it from the excel. My problem now is copying a certain data from excel. I do not want to copy any information inside the parenthesis as well as the parenthesis itself. For example, the data is the name of the employee together with their employee ID. I just need the employee's name. Do you know how to do it?
Below is part of the code. It's quite long since im still new in VBA coding. From the code below, the last part .Text = Range("F1") is the code where I will copy the data I will translate to the powerpoint.
Set myShape = myPresentation.Slides(2).Shapes.AddTable(10, 4, 50, 100, 800)
myShape.Table.Rows.Add
myShape.Height = 0
With myShape.Table
.Cell(1, 1).Merge MergeTo:=.Cell(1, 2)
.Cell(1, 2).Merge MergeTo:=.Cell(1, 3)
.Cell(1, 3).Merge MergeTo:=.Cell(1, 4)
With .Cell(1, 4).Shape
With .TextFrame.TextRange
.Text = "General Information"
.Font.Size = 13
.Font.Name = "Arial"
.Font.Color = RGB(0, 0, 0)
End With
End With
With .Cell(2, 1).Shape
With .TextFrame.TextRange
.Text = "FA Site"
.Font.Size = 13
.Font.Name = "Arial"
.Font.Color = RGB(0, 0, 0)
End With
End With
With .Cell(2, 2).Shape
With .TextFrame.TextRange
.Text = "Singapore"
.Font.Size = 13
.Font.Name = "Arial"
.Font.Color = RGB(0, 0, 0)
End With
End With
With .Cell(2, 3).Shape
With .TextFrame.TextRange
.Text = Range("F1")
.Font.Size = 13
.Font.Name = "Arial"
.Font.Color = RGB(0, 0, 0)
End With
End With
I dont know how to write the VBA code for this problem. I hope someone can help me.
As a possible answer, depending on your reply to my comment earlier, here's a function that will return the original text you pass to it with any text between open and close parentheses removed.
It's not very robust; you'll want to add further checks to make sure that, for example, there ARE both open and close parens in the string.
Function RemoveParens(sText As String) As String
Dim sTemp As String
Dim lParenStart As Long
Dim lParenEnd As Long
Dim sParentheticalText As String
lParenStart = InStr(sText, "(")
lParenEnd = InStr(sText, ")")
sTemp = Mid$(sText, 1, lParenStart - 1)
sTemp = sTemp & Mid$(sText, lParenEnd + 1)
RemoveParens = sTemp
End Function
Here is another option. It has it's pros and cons.
Pros:
*** Will work on multiple strings! Will not remove parts you want to keep in between bracketed areas, and will always remove all bracketed areas.
Will not throw errors if nothing to remove (might also be a con in some situations)
Short easy to use/edit function
Cons:
You need to have a cell free to use for the calculations (Either A1 on a hidden sheet, or some cell you know is free
Slow for large datasets, This takes about half a second per string on a slow work computer. if you're only dealing with =< 50 strings, great! otherwise this would take a few minutes to calculate. This may actually be the slowest way to do this calculation.
Option Explicit
Sub TextToStrip()
Debug.Print StripOutParaText("Test (ouch) and Some ( dog) Number 1")
' Output = "Test and Some Number 1"
End Sub
Function StripOutParaText(RefText As String) As String
Dim RG As Range
Set RG = Worksheets("Data").Range("A1")
RG.Value = RefText
RG.Replace " (*)", "", xlPart
StripOutParaText = RG.Value
End Function
Related
A little background:
A former employee wrote a VBA program to run in AutoCAD to generate G-code based off of CAD entities. The immediate problem is that it currently only runs in AutoCAD 2002 on a computer running Windows XP on a virtual desktop. Obviously, that doesn't work so I'm trying to get it to work on BricsCad V21. My current issue is that I keep getting a Run time error 91 at the following place and I do not understand what the issue is or how to overcome it.
Please note that I am a VERY beginner programmer and am still trying to wrap my head around how all of this works. Any help you can provide would be most appreciated.
Public ExcelApp As Excel.Application
Public wbkObj As Excel.Workbook
Public shtObj As Excel.Worksheet
Public rngObj As Excel.Range
These are the relevant declarations at the beginning of the program
Sub CAM_A_CHEST()
Set ExcelApp = CreateObject("Excel.Application")
Set wbkObj = ExcelApp.workbooks.Add
Set shtObj = ExcelApp.Worksheets(1)
If Err <> 0 Then
MsgBox "Could not start Excel", vbExclamation
End
Else
ExcelApp.Visible = True
Application.Visible = True
ExcelApp.ScreenUpdating = True
Set rngObj = shtObj.Range(Cells(1, 1), Cells(1, 5))
With rngObj
.NumberFormat = "0"
.Font.Name = "Arial"
.Font.FontStyle = "Bold"
.Font.Size = 10
'.Font.ColorIndex = xlAutomatic
End With
With shtObj.Range(Cells(2, 1), Cells(2000, 13))
.NumberFormat = "0.000"
.Font.Name = "Arial"
.Font.FontStyle = "Regular"
.Font.Size = 9
.Value = ""
End With
With shtObj.Range(Cells(2, 5), Cells(2000, 5))
.NumberFormat = "0"
End With
shtObj.Range("A1:D1").Select
Selection.NumberFormat = "General"
shtObj.Cells(1, 1).ColumnWidth = 18
shtObj.Cells(1, 1) = "Layer"
shtObj.Cells(1, 2) = "Center X"
shtObj.Cells(1, 3) = "Center Y"
shtObj.Cells(1, 4) = "Diameter"
shtObj.Cells(1, 5) = "Sort"
shtObj.Cells(1, 7).Font.FontStyle = "Bold"
shtObj.Cells(1, 7).Font.Size = 10
Range("A2").Select
ActiveWindow.FreezePanes = True
End If
'ExcelApp.Visible = False
transZ$ = "2.00"
divNum$ = ""
grpNum$ = "0"
Load UserForm1
Load UserForm2
Load UserForm3
Load procUserForm
Call UserForm_Initialize
That is the chunk of code that opens Excel, formats the Worksheet, and prepares for inputs from AutoCAD. Later on in the code, The following Sub is called:
Sub CAM_TopAndBottomBoards()
Dim I As Integer
Dim mspaceObj As AcadObject
Dim centerPoint As Variant
Dim ExcelApp As Excel.Application
increment = 0
Call InitializeCounters
SelectStuff:
'Find entities representing Pitman Holes, Pipe Holes, etc., among items selected,
' dump data into Excel sheet
ExcelApp.Visible = True
It's that last line that is generating the error.
At this point I've spent a couple of days on this issue and I'm completely stuck. Help!
I'm working on a project using VBA with Excel. By clicking a button, we generate an array. One line refers to one product. The information is stored in each column (column A = reference, column B = name...). The generation is made with some code using VBA.
The problem concerns the pictures. I managed the shape of the pictures, so they are placed in the cell, with a certain height, width, placement...
When I generate the array from my computer, there's no problem, the pictures are placed perfectly.
When the array is generated from another computer, the pictures look stretched and shifted. That's it from the 12th line to the end (but the first 11 lines are okay). I don't understand why it starts from the 12th line because the code is exactly the same for every line of the array. And above all, I don't understand why the array isn't well generated on every computer.
The Excel version is the same and the pictures options too.
Have you heard about something like that?
Thanks a lot for your comments!
Here's the code:
Function SetImageViewer(Ref As String, Cell As Range) As String
Dim cmt As Comment
Dim sPicName As String
Dim ImageCell As Range
Dim OrderFormWS As Worksheet
sPicName = GetParameter("PicturesPath") & "\" & Ref & ".jpg"
Set ImageCell = Cell.MergeArea
Set OrderFormWS = ThisWorkbook.Sheets("OrderForm")
sPicFile = Dir(sPicName)
If sPicFile <> vbNullString Then
Set Pic = OrderFormWS.Shapes.AddPicture(sPicName, linktofile:=msoFalse, SaveWithDocument:=msoCTrue, Left:=0, Top:=0, Width:=0, Height:=0)
With Pic
.LockAspectRatio = msoTrue
.Left = ImageCell.Left + 5
.Top = ImageCell.Top + 5
.Width = 40
.Height = 40
.Placement = xlMoveAndSize
End With
Set cmt = Cell.Comment
If cmt Is Nothing Then Set cmt = Cell.AddComment()
cmt.Text " "
cmt.Shape.Fill.UserPicture sPicName
cmt.Shape.Height = 300
cmt.Shape.Width = 300
SetImageViewer = ChrW(&H25BA)
Else
Set cmt = Cell.Comment
If Not cmt Is Nothing Then cmt.Delete
SetImageViewer = "No picture"
End If
End Function
Im working with a workbook with many sheets and im using a UserForm with more than 150 Combobox and arround 200 Labels.
I want to set the charasteristic and design of the comboboxes in only one and i want to be applied in a lot of them, so i do not want to repeat the code hundreds of times.
How should i do? i ve been reading but i cant match the examples with mine.
This is the wrong code:
Private Sub ComboBox7_Change()
Dim ws2 As Worksheet: Set ws2 = Sheets("C. VfM Questionnaire ")
Dim i, p As Integer
For i = 7 To 31
If Controls("ComboBox" & i).Value = "Yes" Then
ws2.Range("G7") = 4
Me.Controls("Label" & i).BackColor = RGB(146, 208, 80)
ElseIf Controls("ComboBox" & i).Value = "No" Then
ws2.Range("G7") = 4
Me.Controls("Label" & i).BackColor = RGB(255, 33, 26)
ElseIf Controls("ComboBox" & i).Value = "Not Applicable" Then
ws2.Range("G7") = 4
Me.Controls("Label" & i).BackColor = RGB(255, 204, 0)
End If
Next i
End Sub
Also i do not know how to trigger the code? is it in te user form initalize or in a module? Im so newbee as you can see!
Thanks a lot in advance.
If I understood your question correctly, you have a lot of ComboBoxes, and you want to change their style. But instead of changing all of them, you want to change one of them, and then have the form apply this style to all of them, right?
So we pick a box to be the "style guide" and assign that to a variable.
Then we loop though all the ComboBoxes in the Form, and apply the traits.
As for how to trigger it, UserForm initalize would certainly work.
This code for example:
Private Sub UserForm_Initialize()
Dim origin As ComboBox, c As Variant
Set origin = Me.ComboBox1 'Combobox to copy style from
For Each c In Me.Controls
If TypeName(c) = "ComboBox" Then
c.BackColor = origin.BackColor
c.BackStyle = origin.BackStyle
c.BorderColor = origin.BorderColor
c.BorderStyle = origin.BorderStyle
c.ForeColor = origin.ForeColor
End If
Next
End Sub
Changes this:
To this:
Applying the style of the first box, in the top left corner, called ComboBox1
I am trying to copy the contents (text and format) from a text box on one sheet to another text box on another sheet within the same workbook. I have been able to successfully copy over almost everything, but the justification (center/left/right) is not working for each individual line. I am doing this in a very clunky way: copy the text, then loop through each character to get the format set. There does not seem to be an easy way in excel vba to copy both the text and ALL of the format over. Essentially I am trying to do a "select all (Cntrl-A)", "copy (Cnrl-C)" on the origin textbox, then do a "paste special (keep source formatting)" on the destination text box. IT works wonderfully using the mouse, but I do not want to do that. I just want to run a macro to do the same thing. Also, I noted that when the macro runs, the destination text box applies justification global to the text and I am no longer able to individually select a single line and set its justification (i.e. either all lines are centered or all lines are left justified vs. being able to adjust each line individually). Again, this weird behavior only happens after the macro is run. If I use the mouse cut-and-paste method, the text is able to be justified line-by-line again. Here is my clunky code:
Sub Update_CARD_LEG_BACK()
' Set varibles to reduce typing and make changing origin and destination text boxes easier.
Set Orig = Sheets("MAIN_INPUT2").Shapes("CARD_LEG_BACK")
Set Orig_Sheet = Sheets("MAIN_INPUT2")
Set Dest = Sheets("CARD_LEGACY").Shapes("BACK")
Set Dest_Sheet = Sheets("CARD_LEGACY")
'Copy text from origin text box to destination text box. Copies only the text NO formating.
Dest.TextFrame.Characters.Text = Orig.TextFrame.Characters.Text
For i = 1 To Len(Orig.TextFrame.Characters.Text)
Dest.TextFrame.Characters(i, 1).Font.Underline = Orig.TextFrame.Characters(i, 1).Font.Underline
With Dest.TextFrame2.TextRange.Characters(i, 1)
.Text = Orig.TextFrame2.TextRange.Characters(i, 1).Text
With .Font
.Name = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Name
.Size = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Size
.Bold = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Bold
.Strikethrough = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Strikethrough
.Superscript = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Superscript
.Subscript = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Subscript
.Fill.ForeColor.RGB = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Fill.ForeColor.RGB
.Fill.BackColor.RGB = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Fill.BackColor.RGB
.Fill.Visible = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Fill.Visible
.Fill.Transparency = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Fill.Transparency
End With
With .ParagraphFormat
.BaselineAlignment = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.BaselineAlignment
.SpaceWithin = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.SpaceWithin
.SpaceBefore = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.SpaceBefore
.SpaceAfter = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.SpaceAfter
.IndentLevel = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.IndentLevel
.FirstLineIndent = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.FirstLineIndent
.Alignment = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.Alignment
.HangingPunctuation = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.HangingPunctuation
End With
End With
Next i
'Copy fill color of origin text box to destination text box. Also copies transparancy (required for 'no fill' option to copy correctly).
Dest.Fill.ForeColor.RGB = Orig.Fill.ForeColor.RGB
Dest.Fill.Transparency = Orig.Fill.Transparency
End Sub
You could replace the second with a copy of the first:
Sub Tester()
ReplaceWithCopy Sheet1.Shapes("SourceTB"), Sheet2.Shapes("DestTB")
End Sub
Sub ReplaceWithCopy(shpSrc As Shape, shpDest As Shape)
Dim nm As String
shpSrc.Copy
shpDest.Parent.Paste
With shpDest.Parent.Shapes(shpDest.Parent.Shapes.Count)
.Left = shpDest.Left
.Top = shpDest.Top
.Width = shpDest.Width
.Height = shpDest.Height
nm = shpDest.Name
shpDest.Delete 'remove the shape being replaced
.Name = nm 'rename copy to just-deleted shape
End With
End Sub
I have managed with input the textbox to the formula, as per the following query, which I raised a while ago...
VBA Excel how to write Excel formula in the textbox
and everything is fine, but I have got problems with input the proper font features into this textbox.
Basically I have two separate sets of code, which I would love to combine into the one
Sub Duct1()
Set myDocument = ActiveSheet
With myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 300, 140, 180, 30)
.name = "Duct1"
With .TextFrame
.HorizontalAlignment = xlLeft
With .Characters
.Text = "1W-20mm/90' upturn"
.Font.ColorIndex = 3
.Font.Size = 16
.Font.Bold = True
End With
End With
.Rotation = 25
.Fill.Visible = False
.Line.Visible = False
End With
End Sub
Sub Duct1Desc()
ActiveSheet.Shapes("Duct1").OLEFormat.Object.Formula = "=AB1"
End Sub
For the second code I tried also:
Sub Duct1Desc()
ActiveSheet.Shapes("Duct1").OLEFormat.Object.Formula = "=AB1"
With ActiveSheet.Shapes("Duct1")
.Font.ColorIndex = 3
.Font.Size = 16
.Font.Bold = True
End With
End Sub
But in this issue I have got an error, that VBA doesn't support this property or method.
Can anyone help me to bind these 2 codes together?
Thanks
This works for me:
Dim s As Shape
Set s = ActiveSheet.Shapes("myBox")
s.DrawingObject.Formula = "=B2"
OK I thought the problem was the linking, not the formatting: this works for me.
Sub Duct1Desc()
Dim s
Set s = ActiveSheet.Shapes("Duct1")
s.OLEFormat.Object.Formula = "=A1"
With s.DrawingObject
.Font.ColorIndex = 3
.Font.Size = 20
.Font.Bold = True
End With
End Sub