I'm doing a project in vb6 i have a data where when i search it should give multiple answers in combo box. example i have a debtor code each debtor code as more than one addresses when i search in one debtor code how can i get multiple addresses in the combo box. my search engine command as follows
Adodc1.Recordset.MoveFirst
Adodc1.Recordset.Find "DEBTOR_CODE = '" & Text11.Text & "'"
If Adodc1.Recordset.EOF = True Or Adodc1.Recordset.BOF = True Then
MsgBox "Record Not Found!"
vbApplicationModal Adodc1.Recordset.MoveFirst
Me.Combo1.SetFocus
Me.Combo1.ListIndex = Me.Text11.Text
End If
Related
I'm using the latest Office 365 Excel on OS X. I've created a List Box Form Control (ActiveX controls don't seem to be available in OS X), called wb_from:
I'm trying to access this List Box from VBA using this code:
Sub my_Import()
Dim MailStr As String
MailStr = ""
If wb_from.SelectedItems.Count = 0 Then
MsgBox "No User Selected"
Exit Sub
End If
For i = 0 To (wb_from.Items.Count - 1)
If wb_from.Selected(i) Then
MailStr = MailStr & wb_from.Items.Item(i) & "; "
End If
Next i
a = 100
End Sub
Excel is giving me an object required error:
How can I correct this?
What cell did you link that control to via Format Control? That cell address should be used in your code.
I have an Excel document that uses VBA to generate 100+ quarterly reports from a central dataset. Pivot tables are copied from the Excel document and pasted into a Word document that serves as a template for the report.
One of the columns in the tables contains text that I would like to make into formatted hyperlinks to relevant pages related to the row data sources. I was unable to find a method for allowing the hyperlink to survive translation from the lookup table into the pivot table (the pivot table simple returns the display text, without the link).
My thought was to write a script that would search for the text string in the table and simply replace it with the formatted link. Unfortunately, I haven't been able to get this approach to work, despite trying several versions.
I'm fairly new to VBA, so may be missing something simple, but I'm stuck pretty good now. Here's what I've tried so far:
First Version
Tried to copy the formatted hyperlink from a designated cell in the Excel document and then Replace the search text with "^c"
ThisWorkbook.Worksheets("SheetA").Range("A1").Copy
With myDoc.Content.Find
.Execute findText:="target text string", ReplaceWith:="^c", Replace:=wdReplaceAll
End With
This version crashed with "Run-time error '6015': Method 'Execute' of object 'Find' failed" The specific error sometimes varies, but always triggers after replacing the first target text string with the copied cell. I thought that part of the issue might be that it was pasting the entire copied cell from Excel into the cell of the Word table (not just the hyperlink), but I couldn't find a way to paste just the link.
Second Version
Tried to directly code the search and link
Dim h, urlString, displayText as String
h = "target text string"
urlString = "desired address"
displayText = "hyperlink display text"
myDoc.Content.Select
With Selection.Find
.ClearFormatting
.Text = h
.Forward = True
.Wrap = wdFindContinue
End With
Do While Selection.Find.Execute
Selection.Text = "h"
ActiveDocument.Hyperlinks.Add Selection.Range, _
Address:=urlString, SubAddress:="", _
TextToDisplay:=displayText
Loop
This version gives me a "Run-time error '450': Wrong number of arguments or invalid property assignment" on the 'With Selection.Find' line.
I've tried a few other versions (and various combinations thereof) mostly trying to work from the appended links, but have gotten a similar lack of results. Hoping it's just something silly I've missed - appreciate any assistance!
Source 1
Source 2
Source 3
Source 4
The examples you looked at are either for vbscript or Word macros.
See here or here for Excel macro.
Sub update_links()
Const WORD_DOC = "C:\tmp\test.docx"
Const TARGET = "target text string"
Const URL = "desired address"
Const HYPERLINK = "hyperlink display text"
Dim apWord As Variant, wdDoc As Document, count As Integer
Set apWord = New Word.Application
apWord.Visible = True
Set wdDoc = apWord.Documents.Open(WORD_DOC)
wdDoc.Activate
count = 0
With wdDoc.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.Text = TARGET
.Replacement.Text = ""
.Execute
End With
Do While .Find.Found = True
With .Find
apWord.ActiveDocument.Hyperlinks.Add _
Anchor:=.Parent, Address:=URL, _
TextToDisplay:=HYPERLINK
count = count + 1
End With
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
wdDoc.SaveAs "c:\tmp\test_updated.docx"
wdDoc.Close
apWord.Quit
Set apWord = Nothing
MsgBox count & " links added to " & WORD_DOC, vbInformation
End Sub
So currently under specific conditions I have this InputBox poping up for the user to enter notes into and then that box fill those notes to a cell. What I would like to try to do is if notes are already in the cell that the InputBox is filling notes to, those notes will already appear in the input field so that the user can add to them. I have no idea if this can be done and could not find anything on google. If this cannot be done ill just use a user form insted.
The current code that brings up the input box is below, "notes" is the feild that the InputBox fills to:
If InStr(OPs, "Incomplete") > 0 Or InStr(OPs, "Miss") Then
notes.Interior.Color = RGB(255, 200, 0)
If notes = "" Then
Do While notes = ""
notes = notes & InputBox("You must imput notes for " & Desc & " !", "Notes")
Loop
End If
End If
Use the Default parameter of InputBox:
Something like:
notes = notes & InputBox("You must input notes for " & Desc & " !", "Notes", notes)
However, if you use this, you don't want to use the notes = notes & ... as the string field will quickly overfill.. so perhaps just:
notes = InputBox("You must input notes for " & Desc & " !", "Notes", notes)
What I am trying to do is basically you click a button, it brings up the Excel MailEnvelope to send an email, and you can then send it off to the relevant button email address'.
However one of the email addresses needs to be modifiable by the end user.
So I want a drop down where you select said email, and it then inputs that into the VBA code.
I know basically nothing about VBA and I could not find a way of doing this by searching around the web.
I figured I need some way of setting a variable to read a cell (the drop down cell), and then input that into the MailEnvelope Item.CC but I was struggling.
Any help would be appreciated.
This is what I have so far;
Sub Send_Range_Email()
' Select the range of cells on the active worksheet.
ActiveSheet.Range("B6:D302").Select
' Show the envelope on the ActiveWorkbook.
ActiveWorkbook.EnvelopeVisible = True
' Set the optional introduction field thats adds
' some header text to the email body. It also sets
' the To, CC and Subject lines.
With ActiveSheet.MailEnvelope
.Introduction = ""
.Item.To = "Email 0"
.Item.Subject = "Email Tracker Results"
.Item.CC = "Email 1" & text input here & "Email 2"
End With
End Sub
When using formulas, if you want to put a variable in there, just break it apart and add in the variable. As commented,
.Item.CC = "email 1" & "," & Range("A1").Value & ", " & "Email 2"
So to make super clear, say we want to add A1's value in this string: str = The man lives in STATE all the time by doing str = "The man lives in " & Range("A1").Value & " all the time"
Currently I need to change over hundreds of cells of hyperlinks to a new format that is being used.
Previously we would have for example "https://oldServer:oldPort/project_code/Some_File"
and we are now moving to
"https://newServer:newPort/project_code/Some_File".
I only want to be able to change the "oldServer:oldPort" to "newServer:newPort" without changing the rest of the hyperlink.
I know an excel macro would be the easiest solution but I do not have the experience to be able to create one.
You'd want to loop through each of the hyperlink objects in each sheet.
Code something like this should work. (Make sure you backup before testing this, since I can't test on your actual data!)
Also, note that this changes the link but not the text displayed, since I'm not sure what that consists of. The text could be changed with the same procedure by updating TextToDisplay, or else by using Find & Replace.
Edit: (added confirmation on each change.)
Sub changeLinks()
Const oldPrefix = "https://oldServer:oldPort/"
Const newPrefix = "https://newServer:newPort/"
Dim h As Hyperlink, oldLink As String, newLink As String
For Each h In ActiveSheet.Hyperlinks
'this will change Address but not TextToDisplay
oldLink = h.Address
Debug.Print "Found link: " & oldLink
If Left(oldLink, Len(oldPrefix)) = oldPrefix Then
newLink = newPrefix & Right(h.Address, Len(h.Address) - Len(oldPrefix))
If MsgBox("Click OK to change:" & vbLf & vbLf & oldLink & _
vbLf & vbLf & "to" & vbLf & vbLf & newLink, vbOKCancel, _
"Confirmation?") <> vbOK Then Exit Sub
h.Address = newLink
Debug.Print " Changed to " & h.Address
End If
Next h
End Sub
I removed the hyperlink from the column containing the old server information. Then I created a couple of new columns. I typed in the full path to the new server in the first new columns and filled it down. In the other new column I created a formula to concatenate the full path and the friendly name (which happened to be what the link was originally named). This created the new server path along with the file name. I copied/pasted values of this column and deleted the contents of the first new column I created. I then used the HYPERLINK formula to put the hyperlink and the friendly name back together. It sounds more convoluted than it was - took me longer to write it down here than to actually do it in my Excel spreadsheet. I hope this makes sense.