由于我是 Access VBA 新手,请耐心等待。
这里有一个带有数据表的子表单,我想在弹出视图中显示它。
如果“弹出”为“否”,则会显示整个页面。
如果弹出为是,则仅显示以下内容:
这些是我的属性:
我尝试将“自动调整大小”设置为“否”,但发生了同样的事情。
我应该在属性中设置什么以便至少它会自动显示以下内容:
尝试修改此设置,以便只删除当前年份之外的记录。之前,我一直在研究滚动的 12 个月范围以证明这一概念,现在我只需要保留当前年份的数据。我尝试了几种不同的方法,但无法弄清楚,不想每个月都进入并将月份数更改 1,有没有办法说如果日期不在 2025 年之内,就将其删除
数据中日期格式的示例 13/09/2024 4:35:16 pm
以前使用的代码
Sub Delete_Rows() 'DELETE HISTORICAL DATA OVER 1 YEAR OLD
With Sheets("HistoricalData")
lr = .Cells(Rows.Count, "C").End(xlUp).Row
For i = lr To 2 Step -1
If CDate(.Cells(i, "C").Value) < DateAdd("m", -12, Date) Then 'SETS THE MONTH COUNT TO MORE THAN 12 MONTHS OLD
.Rows(i).EntireRow.Delete ' DELETES ROW
End If
Next i
End With
End Sub
我一直在按照本教程从 URL 中抓取数据,因为它非常符合我的需求(3 个 div 深)。不幸的是,StackOverflow 不再支持 IE,我无法测试本教程的代码以查看它是否按原样工作。就我而言,我无法使用 chrome 插件,并且必须先向网站进行身份验证,然后才能导航到 URL。我还尝试了问题 15191847 的解决方案 - 特别是gembird 的解决方案 - 它给了我同样的错误。
当我运行下面的程序时,我收到“运行时错误‘91’。我将 ie.document 打印到文本文件中,并验证我正在搜索的 div id 是正确的,并且它们已被捕获。错误继续出现Set Questions = QuestionList.Children
。有什么想法为什么它会向我显示错误?
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim QuestionList As IHTMLElement, QuestionField As IHTMLElement
Dim Questions As IHTMLElementCollection, QuestionFieldLinks As IHTMLElementCollection, QuestionFields As IHTMLElementCollection
Dim Question As IHTMLElement
Dim RowNumber As Long
Dim votes As String, url As String, views As String, QuestionId As String
url = "<<my url>>"
'open Internet Explorer in memory, and go to website
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate url
'Wait until IE is done loading page
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to " & url
DoEvents
Loop
Cells.Clear
'show text of HTML document returned
Set html = ie.Document
'close down IE and reset status bar
Set ie = Nothing
Application.StatusBar = ""
'put heading across the top of row 3
Range("A3").Value = "Field"
Range("B3").Value = "Values"
Set QuestionList = html.getElementByID("fieldgroup ")
Set Questions = QuestionList.Children
RowNumber = 4
For Each Question In Questions
If Question.className = "fieldrow _text-field" Then
'get a list of all of the parts of this question, and loop over them
Set QuestionFields = Question.all
For Each QuestionField In QuestionFields
'if this is the question's votes, store it (get rid of any surrounding text)
If QuestionField.className = "fieldlabel" Then
Cells(RowNumber, 1).Value = Trim(QuestionField.innerText)
End If
'likewise for views (getting rid of any text)
If QuestionField.className = "fieldvalue" Then
Cells(RowNumber, 2).Value = Trim(QuestionField.innerText)
End If
Next QuestionField
'go on to next row of worksheet
RowNumber = RowNumber + 1
End If
Next
Set html = Nothing
HTML 输出如下所示。
<div class="fieldgroup " style="" group-title="">
<div class="fieldrow _text-field">
<div class="fieldlabel">Reporting</div>
<div class="fieldvalue">Yes</div>
</div>
<div class="fieldrow _text-field">
<div class="fieldlabel">Annotate ''Yes''</div>
<div class="fieldvalue">Yes</div>
</div>
...
我有一个数据表(子)表单,它从存储过程接收记录源,因此它不可更新。我想将一列值(仅在 Access 表单上)复制到未绑定的复选框(新列)中,以便用户可以单击它,然后事情就会发生。
我尝试以不同的方式设置值,但我总是只得到第一行的值 - 插入到所有行。
我从存储过程收到的信息:
ID | 确认的 |
---|---|
1 | 真的 |
2 | 错误的 |
3 | 错误的 |
我想要的是:
ID | 确认的 | 确认副本(未绑定) |
---|---|---|
1 | 真的 | 真的 |
2 | 错误的 | 错误的 |
3 | 错误的 | 错误的 |
如果我将 ConfirmedCopy 的默认值设置为 =[Confirmed],或者使用 VBA 执行相同操作,将得到以下结果。因此,它获取顶行的值并将其插入到 ConfirmedCopy 字段中的所有行:
ID | 确认的 | 确认副本(未绑定) |
---|---|---|
1 | 真的 | 真的 |
2 | 错误的 | 真的 |
3 | 错误的 | 真的 |
我有一个 Word 文档,它是长聊天记录的导出,每个条目都包含一个 13 位 UNIX 时间戳。我需要将每个时间戳转换为常规/可读的日期/时间(例如,2025 年 1 月 17 日星期五,美国东部时间凌晨 2:47),并将常规转换附加在原始时间戳之后。我有一个宏,它基本上可以正常工作,但我的循环无法正确迭代。
该宏的目的是:
我不知道如何让 Range 变量将附加值粘贴/输入到文档中,然后迭代查找下一个时间戳(我太缺乏经验,不知道我做错了什么)
例如,第一个时间戳是“1702654591899”,预期的替换文本是:“1702654591899;2023 年 12 月 15 日星期五 - 美国东部标准时间上午 10:36:32”,我想重复下一个时间戳,直至文档结束。
以下是我带有注释的代码:
'Function converts UNIX 13 Digit Timestamp to date/time in Eastern Standard Time
'Subtracts 18,000,000 miliseconds to subtract 5 hours for Eastern Standard Time
'Adapted from FaneDuru's Solution at:
'https://stackoverflow.com/questions/73011816/excel-vba-convert-unix-timestamp-to-date-time
Function fromUNIX13DigitsEST(uT) As Date
''Original line that converts to UTC or GMT (I'm not sure which but it's
''5 hours later than intended so I commented it out but left it for info purposes)
'fromUNIX13DigitsEST = CDbl(uT) / 86400000 + DateSerial(1970, 1, 1)
'Modified from line above to account for Eastern Standard Time
fromUNIX13DigitsEST = (CDbl(uT) - 18000000) / 86400000 + DateSerial(1970, 1, 1)
End Function
'Loops and Finds all 13 digit Unix Timestamps and replaces them with the Timestamp plus
'conventional date/time
Sub FindUnix13DigitTimestampsAndAddConventionalDateAndTime()
Dim TimestampInstance As Range
Dim TimestampAndConverted As String 'Artifact of the learning process kept to prevent loops
Set TimestampInstance = ActiveDocument.Range
With TimestampInstance.Find
'Do I need more of these? Is this section the problem?
.Text = "[0-9]{13}"
.MatchWildcards = True
Do While .Execute(Forward:=True) = True
TimestampInstance.Select
' Used to test before adding the "TimestampInstance.Text = ..." line below
' Sets string variable equal to the original Timestamp and adds the standard date/time/time zone
' Uses the Function above to convert the UNIX TimeStamp
' Kept to use in the MsgBox line below as a failsafe against a runaway loop
' Adapted from FaneDuru's Solution at
' https://stackoverflow.com/questions/73011816/excel-vba-convert-unix-timestamp-to-date-time
TimestampAndConverted = TimestampInstance + "; " + Format(fromUNIX13DigitsEST(TimestampInstance), "dddd, mmmm d, yyyy - hh:nn:ss AM/PM") + " EST"
' PROBLEMATIC LINE THAT PARTIALLY DOES WHAT I INTENDED IT TO DO
' Duplicates the Function call above and adds standard date items
' Meant to replace each 13-digit timestamp with the timestamp plus standard date/time/time zone
' If commented out then loop iterates as intended and the MsgBox below displays each successive
' output from the TimestampAndConverted variable
TimestampInstance.Text = TimestampInstance + "; " + Format(fromUNIX13DigitsEST(TimestampInstance), "dddd, mmmm d, yyyy - hh:nn:ss AM/PM") + " EST"
'Displays variable TimestampAndConverted above in a message box & helps prevent a runaway loop
MsgBox TimestampAndConverted
'
'
' Should there be code here to allow the loop to iterate to next 13-digit timestamp
' or reset "TimestampInstance" variable??
'
'
''This isn't the solution since it ends up replacing the 13-digit timestamps with "" so I commented it out
'TimestampInstance.Text = ""
Loop
End With
End Sub
我设法使用字符串变量在每次迭代中在 MsgBox 中显示预期的输出,但是当我尝试使用 Range 变量重复该过程并将替换文本输入 Range 时,它会卡在第一个时间戳并不断循环重新添加常规日期/时间。
以第一个时间戳(“1702654591899”)为例,它随后添加的文本是“;2023 年 12 月 15 日星期五 - 美国东部标准时间上午 10:36:32”,但循环只是不断重新添加“;2023 年 12 月 15 日星期五 - 美国东部标准时间上午 10:36:32”,所以最终结果是:
“1702654591899;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32美国东部标准时间;2023 年 12 月 15 日星期五 - 上午 10:36:32 美国东部标准时间;2023 年 12 月 15 日星期五 - 上午 10:36:32 美国东部标准时间;2023 年 12 月 15 日星期五 - 上午 10:36:32 美国东部标准时间……”
此时我按 CTRL+Break 键终止循环并结束宏。
如何在每个找到的时间戳后添加一次常规日期/时间,然后继续下一个时间戳?
寻找一个 vba 代码来更新所有字段,而不包含属于 Ask 字段的提示,例如 Print Preview 所包含的提示。但是,打开和关闭 Print Preview 的代码并不是我们想要的,因为它会将我带回到文档的顶部,而不是让我停留在我原来的页面上。我说的是这个代码:
Sub Update()
Application.ScreenUpdating = False
With ActiveDocument
.PrintPreview
.ClosePrintPreview
End With
Application.ScreenUpdating = True
End Sub
除了告诉提示不显示之外,还有其他方法可以更新所有字段吗?或者 ask 字段的提示字段是它们固有的,在操作过程中不能禁用它们吗?
提前感谢您的支持
威廉
跟进之前的帖子(发送电子邮件时如何通过 MailItem.SendUsingAccount 设置 Account 对象?),我对该帖子中的一个回复在设置邮件项的属性时使用 SET 而另一个没有使用 SET 感兴趣。这很奇怪!我有一大堆相当复杂的 VBA 代码,
在可用帐户中查找与包含我想要发送的电子邮件地址的唯一部分的字符串匹配的帐户
创建一个新的邮件项目,然后设置用于发送该邮件的帐户。
在我的代码中,SET 语句需要在步骤 1) 中存在,但在步骤 2) 中不需要存在,这相当奇怪(包括 SET 生成 91 个运行时错误)。我认为帐户对象不知何故变成了该帐户的 SMTP 地址的字符串,然后 Outlook 将从该电子邮件地址开始使用所需的帐户。
谢谢。代码的关键部分:
a) 各种声明
Public g_olapp As outlook.Application
Public g_ol_account As outlook.Account
Private olmsg As outlook.MailItem
b) 初始化
Set g_olapp = New outlook.Application
Set g_ol_account = find_account(g_from_email, b_err)
c) 查找帐户函数,这似乎返回一个对象,需要 SET 语句
Private Function find_account(s As String, b As Boolean) As outlook.Account
Dim p_olaccount As outlook.Account
Dim s_send_from As String
s_send_from = UCase(s)
If s_send_from = "" Then
If MsgBox("No account specified, do you want to use the default account <SPJUDGE> ?", Title:=box_title, Buttons:=vbYesNo + vbQuestion) = vbNo Then
b = True
Exit Function
End If
s_send_from = "SPJUDGE"
End If
Set find_account = Nothing
For Each p_olaccount In g_olapp.Session.Accounts
If (Not InStr(UCase(p_olaccount.SmtpAddress), s_send_from) = 0) Then
Set find_account = p_olaccount
s = p_olaccount.SmtpAddress
Exit Function
End If
Next
MsgBox "Account <" & s_send_from & "> not found" _
& String(2, 13) & "Program terminating", Title:=box_title, _
Buttons:=vbOKOnly + vbCritical
b = True
End Function
d) 制作电子邮件项目
Set olmsg = make_new_email(g_olapp, s_email)
e) 另一个创建新电子邮件对象的函数。
Public Function make_new_email(olapp As Object, s As String) As outlook.MailItem
Dim arr() As String
Dim jloc As Integer
Dim jlb As Integer
Dim recip As outlook.Recipient
Set make_new_email = olapp.CreateItem(olMailItem)
' MsgBox g_ol_account note this does work.
With make_new_email
.SendUsingAccount = g_ol_account ' WHY NOT SET IN THIS LINE ??
If gb_set_reply Then
.ReplyRecipients.add (g_reply_to_email)
End If
.OriginatorDeliveryReportRequested = b_askfor_receipts
.ReadReceiptRequested = b_askfor_receipts
End With
s = Replace(s, "SIMON JUDGE", "", , , vbTextCompare)
arr = Split(s, ";") ' changed from comma Nov 2023
jlb = LBound(arr)
For jloc = jlb To UBound(arr)
Set recip = make_new_email.Recipients.add(Trim(arr(jloc)))
If jloc = jlb Then
recip.Type = olTo
Else
recip.Type = olCC
End If
Next jloc
End Function
由于我是 Access VBA 新手,请耐心等待。
我在 Form2 中有一个文本框,其名称txtEID
和Employee ID
值是从另一个表单传递过来的。
我还有一个名为的示例文本框,txtFullName
它应该从表中自动填充人员姓名,其中表tblEmployees
字段等于。EmployeeName
Form2.txtEID.value
EID
tblEmployees
我不知道如何在文本框中实现自动填充,但为了进行试用,我尝试在 ComboBox 中使用其 Rowsource 属性中的以下这一行:
SELECT [tblEmployees].[ID], [tblEmployees].[EmployeeName] FROM tblEmployees WHERE [tblEmployees].[EID] = [txtEID];
它在组合框中显示所需的值(基于 txtEID 的 EID 的人名),但仅作为下拉列表。
如何根据中显示的 EID 将文本框的值设置txtFullName
为表中的人名txtEID
。我也不知道如何在txtFullName
“控件来源”属性中进行设置。
这个问题困扰了我 6 个小时。非常感谢你的想法。
刚接触 VBA,非常沮丧。只是尝试简单地初始化一个字符串数组。
Dim MyArray() 作为字符串
MyArray = 数组(“A”,“B”,“C”)
返回运行时错误 13:类型不匹配。
我错过了什么?
从更复杂的字符串开始,简化为单个字母,但这显然不是问题。
编辑:Tim Williams 发现了这个问题。我没有将 Set 用于对象变量。
我的宏是:
Sub StyleFollowingBodyText()
' Charles Kenyon 15 December 2024
' Set the following style for most QuickStyle paragraph styles to be Body Text
Dim StyleCount As Long
Dim thisStyle As Style
Dim iCount As Long
'
With ActiveDocument
Let StyleCount = .Styles.Count
For iCount = 1 To StyleCount
Let thisStyle = .Styles(iCount)
If thisStyle.QuickStyle = True Then
If thisStyle.Type = wdStyleTypeParagraph Or wdStyleTypeLinked Then
If thisStyle.NameLocal <> "Normal" Then
thisStyle.NextParagraphStyle = "Body Text"
End If
End If
End If
Next iCount
End With
End Sub
运行它时出现以下错误消息:
此错误消息似乎有点“包罗万象”,并没有多大帮助。正如 Tim Williams 指出的那样,问题不在于对对象变量使用 Set 命令。
它正在 停止Let thisStyle = .Styles(iCount)
。
宏的目的是将大部分段落 QuickStyles 更改为具有以下样式的正文样式。我试图更改 50 多个 [快速] 样式集,因为我不想让我的大部分文档使用普通样式。