- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有循环遍历文件夹并将文本值添加到 G1、H1、I1 等工作簿的代码。
在图 1 中,您可以看到我的文件夹中有几个文件。不同的 Excel 文件或工作簿会添加不同的文本值。
要添加到“Professional”工作簿的文本值与要添加到“ProfessionalAddress”或“ProfessionalCommunication”的文本值不同。
我尝试使用 InStr
但这将采用包含特定文本片段的任何文件名。
例如,我有几个包含单词“Professional”的文件,这意味着代码随后会将“Professional”文件的文本值添加到包含文本“Professional”的所有文件。
我需要当文件名包含“Professional”时添加这些文本值,当文件包含“ProfessionalAddress”时添加这些文本值。对于“ session ”“组织”“客户”也是如此。
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
If InStr(myFile, "Professional") > 0 Then
'Add Column Headings
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "ProfessionalId"
Range("J1").Value = "StatusCode"
Range("K1").Value = "ProfessionalTypeCode"
Range("L1").Value = "StatusDate"
Range("M1").Value = "Qualification"
Range("N1").Value = "ProfessionalSubtypeCode"
Range("O1").Value = "FirstName"
Range("P1").Value = "MiddleName"
Range("Q1").Value = "LastName"
Range("R1").Value = "SecondLastName"
Range("S1").Value = "MeNumber"
Range("T1").Value = "ImsPrescriberId"
Range("U1").Value = "NdcNumber"
Range("V1").Value = "TitleCode"
Range("W1").Value = "ProfessionalSuffixCode"
Range("X1").Value = "GenderCode"
Range("Y1").Value = "Reserved for future use"
Range("Z1").Value = "Reserved for future use"
Range("AA1").Value = "Reserved for future use"
Range("AB1").Value = "Reserved for future use"
Range("AC1").Value = "SourceDataLevelCode"
Range("AD1").Value = "PatientsPerDay"
Range("AE1").Value = "PrimarySpecialtyCode"
Range("AF1").Value = "SecondarySpecialtyCode"
Range("AG1").Value = "TertiarySpecialtyCode"
Range("AH1").Value = "NationalityCode"
Range("AI1").Value = "TypeOfStudy"
Range("AJ1").Value = "UniversityAffiliation"
Range("AK1").Value = "SpeakerStatusCode"
Range("AL1").Value = "OneKeyId"
Range("AM1").Value = "NucleusId"
Range("AN1").Value = "Suffix"
Range("AO1").Value = "ClientField1"
Range("AP1").Value = "ClientField2"
Range("AQ1").Value = "ClientField3"
Range("AR1").Value = "ClientField4"
Range("AS1").Value = "ClientField5"
Range("AT1").Value = "Reserved for future use"
Range("AU1").Value = "NPICountry"
Range("AV1").Value = "CountryCode"
Range("AW1").Value = "Reserved for future use"
Range("AX1").Value = "MassachusettsId"
Range("AY1").Value = "NPIId"
Range("AZ1").Value = "UniversityCity"
Range("BA1").Value = "UniversityPostalArea"
End If
If InStr(myFile, "ProfessionalAddress") > 0 Then
'Add Column Headings
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "ProfessionalAddressId"
Range("J1").Value = "EffectiveDate"
Range("K1").Value = "StatusCode"
Range("L1").Value = "ProfessionalId"
Range("M1").Value = "AddressTypeCode"
Range("N1").Value = "StatusDate"
Range("O1").Value = "Reserved for future use"
Range("P1").Value = "AddressLine1"
Range("Q1").Value = "AddressLine2"
Range("R1").Value = "AddressLine3"
Range("S1").Value = "City"
Range("T1").Value = "State"
Range("U1").Value = "PostalArea"
Range("V1").Value = "PostalAreaExtension"
Range("W1").Value = "CountryCode"
Range("X1").Value = "Reserved for future use"
Range("Y1").Value = "Reserved for future use"
Range("Z1").Value = "Reserved for future use"
Range("AA1").Value = "DeaNumber"
Range("AB1").Value = "DeaExpirationDate"
Range("AC1").Value = "LocationName"
Range("AD1").Value = "EndDate"
Range("AE1").Value = "N/A"
End If
If InStr(myFile, "ProfessionalStateLicense") > 0 Then
'Add Column Headings
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "ProfessionalLicenseId"
Range("J1").Value = "EffectiveDate"
Range("K1").Value = "EndDate"
Range("L1").Value = "ProfessionalId"
Range("M1").Value = "StateLicenseNumber"
Range("N1").Value = "StateLicenseState"
Range("O1").Value = "StateLicenseExpirationDate"
Range("P1").Value = "SamplingStatusCode"
Range("Q1").Value = "Reserved for future use"
Range("R1").Value = "N/A"
End If
If InStr(myFile, "ProfessionalCommunication") > 0 Then
'Add Column Headings
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "ProfessionalCommunicationId"
Range("J1").Value = "ProfessionalId"
Range("K1").Value = "CommunicationTypeCode"
Range("L1").Value = "CommunicationValue1"
Range("M1").Value = "CommunicationValue2"
Range("N1").Value = "ProfessionalAddressId"
Range("O1").Value = "N/A"
End If
If InStr(myFile, "Organization") > 0 Then
'Add Column Headings
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "OrganizationId"
Range("J1").Value = "StatusCode"
Range("K1").Value = "OrganizationTypeCode"
Range("L1").Value = "StatusDate"
Range("M1").Value = "Reserved for future use"
Range("N1").Value = "OrganizationSubtypeCode"
Range("O1").Value = "OrganizationName"
Range("P1").Value = "NPICountry"
Range("Q1").Value = "Reserved for future use"
Range("R1").Value = "Reserved for future use"
Range("S1").Value = "Reserved for future use"
Range("T1").Value = "Reserved for future use"
Range("U1").Value = "SourceDataLevelCode"
Range("V1").Value = "Reserved for future use"
Range("W1").Value = "Reserved for future use"
Range("X1").Value = "OneKeyId"
Range("Y1").Value = "FederalTaxId"
Range("Z1").Value = "Reserved for future use"
Range("AA1").Value = "NucleusId"
Range("AB1").Value = "Reserved for future use"
Range("AC1").Value = "ClientField1"
Range("AD1").Value = "ClientField2"
Range("AE1").Value = "ClientField3"
Range("AF1").Value = "ClientField4"
Range("AG1").Value = "ClientField5"
Range("AH1").Value = "MassachusettsId"
Range("AI1").Value = "NPIId"
Range("AJ1").Value = "N/A"
End If
If InStr(myFile, "OrganizationAddress") > 0 Then
'Add Column Headings
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "OrganizationAddressId"
Range("J1").Value = "EffectiveDate"
Range("K1").Value = "StatusCode"
Range("L1").Value = "OrganizationId"
Range("M1").Value = "AddressTypeCode"
Range("N1").Value = "StatusDate"
Range("O1").Value = "Reserved for future use"
Range("P1").Value = "AddressLine1"
Range("Q1").Value = "AddressLine2"
Range("R1").Value = "AddressLine3"
Range("S1").Value = "City"
Range("T1").Value = "State"
Range("U1").Value = "PostalArea"
Range("V1").Value = "PostalAreaExtension"
Range("W1").Value = "CountryCode"
Range("X1").Value = "Reserved for future use"
Range("Y1").Value = "Reserved for future use"
Range("Z1").Value = "Reserved for future use"
Range("AA1").Value = "DeaNumber"
Range("AB1").Value = "DeaExpirationDate"
Range("AC1").Value = "LocationName"
Range("AD1").Value = "EndDate"
Range("AE1").Value = "N/A"
End If
If InStr(myFile, "OrganizationCommunication") > 0 Then
'Add Column Headings
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "OrganizationCommunicationId"
Range("J1").Value = "OrganizationId"
Range("K1").Value = "CommunicationTypeCode"
Range("L1").Value = "CommunicationValue1"
Range("M1").Value = "CommunicationValue2"
Range("N1").Value = "OrganizationAddressId"
Range("O1").Value = "N/A"
End If
If InStr(myFile, "OrganizationSpecialty") > 0 Then
'Add Column Headings
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "OrganizationSpecialtyId"
Range("J1").Value = "OrganizationId"
Range("K1").Value = "SpecialtyTypeCode"
Range("L1").Value = "SpecialtyCode"
Range("M1").Value = "N/A"
End If
If InStr(myFile, "Agreement01_MSD") > 0 Then
'Add Column Headings
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "AgreementId"
Range("J1").Value = "CompanyId"
Range("K1").Value = "AgreementName"
Range("L1").Value = "AgreementType"
Range("M1").Value = "StatusCode"
Range("N1").Value = "Description"
Range("O1").Value = "AgreementDate"
Range("P1").Value = "CustomerId"
Range("Q1").Value = "ApprovalDate"
Range("R1").Value = "StartDate"
Range("S1").Value = "EndDate"
Range("T1").Value = "SignatureDate"
Range("U1").Value = "SecondaryCustomerId"
Range("V1").Value = "AgreementCountry"
Range("W1").Value = "ClientField1"
Range("X1").Value = "ClientField2"
Range("Y1").Value = "ClientField3"
Range("Z1").Value = "ClientField4"
Range("AA1").Value = "ClientField5"
Range("AB1").Value = "ClientDate1"
Range("AC1").Value = "ClientDate2"
Range("AD1").Value = "ClientNumber1"
Range("AE1").Value = "ClientNumber2"
Range("AF1").Value = "DataSourceId"
Range("AG1").Value = "CreationUser"
Range("AH1").Value = "CommentText"
Range("AI1").Value = "FirstName"
Range("AJ1").Value = "MiddleName"
Range("AK1").Value = "LastName"
Range("AL1").Value = "AddressId"
Range("AM1").Value = "AddressLine1"
Range("AN1").Value = "AddressLine2"
Range("AO1").Value = "AddressLine3"
Range("AP1").Value = "City"
Range("AQ1").Value = "State"
Range("AR1").Value = "PostalArea"
Range("AS1").Value = "Country"
Range("AT1").Value = "SecondaryFirstName"
Range("AU1").Value = "SecondaryMiddleName"
Range("AV1").Value = "SecondaryLastName"
Range("AW1").Value = "SecondaryAddressId"
Range("AX1").Value = "SecondaryAddressLine1"
Range("AY1").Value = "SecondaryAddressLine2"
Range("AZ1").Value = "SecondaryAddressLine3"
Range("BA1").Value = "SecondaryCity"
Range("BB1").Value = "SecondaryState"
Range("BC1").Value = "SecondaryPostalArea"
Range("BD1").Value = "SecondaryCountry"
Range("BE1").Value = "EventVenue"
Range("BG1").Value = "EventName"
Range("BG1").Value = "EventDate"
Range("BH1").Value = "AgreementVenueOrganizer"
Range("BI1").Value = "AgreementReason"
End If
If InStr(myFile, "Consent11_MSD") > 0 Then
'Add Column Headings
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "ConsentId"
Range("J1").Value = "CompanyId"
Range("K1").Value = "ConsentType"
Range("L1").Value = "ConsentIndicator"
Range("M1").Value = "CustomerId"
Range("N1").Value = "ExpensePurposeCode"
Range("O1").Value = "EffectiveDate"
Range("P1").Value = "EndDate"
Range("Q1").Value = "ConsentDate"
Range("R1").Value = "CommentText"
Range("S1").Value = "AgreementId"
Range("T1").Value = "CustomerExpenseId"
Range("U1").Value = "MeetingId"
Range("V1").Value = "DataSourceId"
Range("W1").Value = "ClientField1"
Range("X1").Value = "ClientField2"
Range("Y1").Value = "ClientField3"
Range("Z1").Value = "ClientField4"
Range("AA1").Value = "ClientField5"
Range("AB1").Value = "N/A"
End If
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
用于测试的精简代码
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
myFile = "20170614Agreement01_MSD.xls"
If getTextBtwnNumbers(myFile) = "Agreement" Then
'Add Text
wb.Worksheets(1).Range("F1").Value = "Error code"
Range("G1").Value = "Error description"
Range("H1").Value = "ActionCode"
Range("I1").Value = "ProfessionalId"
Range("J1").Value = "StatusCode"
Range("K1").Value = "ProfessionalTypeCode"
Range("L1").Value = "StatusDate"
Range("M1").Value = "Qualification"
'etc etc etc
End If
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Private Function getTextBtwnNumbers(s As String) As String
Dim pos1 As Long, pos2 As Long
Dim i As Long, j As Long
For i = 1 To Len(s)
If pos1 = 0 Then
Select Case Asc(Mid(s, i, 1))
Case 65 To 90, 97 To 122
pos1 = i
End Select
Else
For j = pos1 To Len(s)
Select Case Asc(Mid(s, j, 1))
Case 65 To 90, 97 To 122
Case Else
pos2 = j ' - 1
Exit For
End Select
Next j
End If
If pos1 <> 0 And pos2 <> 0 Then Exit For
Next i
If pos1 <> 0 And pos2 <> 0 Then
getTextBtwnNumbers = Trim(Mid(s, pos1, pos2 - pos1))
Else
getTextBtwnNumbers = "Invalid Text Format"
End If
End Function
最佳答案
问题是文件名中的单词没有空格。在这种情况下,很难防止误报。
话虽如此,如果您要查找的文本始终位于 2 个数字之间;例如,Agreement
位于 20170614Agreement01_MSD.xls
中的 20170614
和 01
之间,那么我们可以采用这种方法
将此函数添加到您的代码中
Private Function getTextBtwnNumbers(s As String) As String
Dim pos1 As Long, pos2 As Long
Dim i As Long, j As Long
For i = 1 To Len(s)
If pos1 = 0 Then
Select Case Asc(Mid(s, i, 1))
Case 65 To 90, 97 To 122
pos1 = i
End Select
Else
For j = pos1 To Len(s)
Select Case Asc(Mid(s, j, 1))
Case 65 To 90, 97 To 122
Case Else
pos2 = j ' - 1
Exit For
End Select
Next j
End If
If pos1 <> 0 And pos2 <> 0 Then Exit For
Next i
If pos1 <> 0 And pos2 <> 0 Then
getTextBtwnNumbers = Trim(Mid(s, pos1, pos2 - pos1))
Else
getTextBtwnNumbers = "Invalid Text Format"
End If
End Function
然后你就可以像这样使用它
Sub Sample()
Dim flName As String
flName = "20170614Agreement01_MSD.xls"
If getTextBtwnNumbers(flName) = "Agreement" Then
MsgBox "Match Found"
End If
End Sub
注意:
我假设文本位于 NumberTEXTNumber
格式的 2 个数字之间。
如果您的格式为 NumberTEXTONENumberTEXTTWONumber
那么该函数将仅提取 TEXTONE
编辑
我意识到使用LIKE
有更好的方法。这样你就不需要上面的功能了。
Sub Sample()
Dim flName As String, Searchtext As String
flName = "20170614Agreement01_MSD.xls"
Searchtext = "Agreement"
If flName Like "*#" & Searchtext & "#*.xls" Then MsgBox "Match Found"
End Sub
关于vba - 如果文件名包含特定文本则执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44756275/
任何帮助深表感谢。我正在尝试创建一个 SSIS 包来遍历文件夹中的文件并获取路径+文件名,最后执行存储的过程,参数为路径+文件名。我不确定如何获取路径+文件名并将其作为参数插入到存储过程中。我附上了截
我想编写一个小脚本来搜索确切的文件名,而不是文件名中的字符串。 例如,如果我使用资源管理器搜索“主机”,默认情况下我会得到多个结果。对于脚本,我只需要我指定的名称。我假设这可能吗? 我才真正开始编写脚
str(文件.key) = '1011/101011/文件名' newFileName = str(file.key) 但是,当我运行代码时,我得到: UnicodeEncodeError: 'asc
下面这段子程基本上可以算是比较不错的通用匹配了。(PS:我突然发现CODE_LITE把我的UBB转义了!!!晕,我只好自己转义了。。。) Dim objRegExp,Matc
PHP 无法处理带有 Unicode 字符的文件:当我在浏览器上访问 testSite/главная.php 时,它会抛出此错误。 Warning: Unknown: failed to open
我正在尝试包含 Dim在 Vlookup 中。 Dim filename As String filename = Format(DateAdd("d", -6, Now()), "mm-dd-yy"
在我的日常构建项目中,我们将其库存储到其版本名称目录中。 . 对于最新的,我们正在创建符号链接(symbolic link)作为“最新”。 前任。- ls -ltr drw-r--r-- 1 4096
重新安装了 Windows 10(版本 10.0.14393)。重新安装了以下内容: java java version "1.8.0_121" Java(TM) SE Runtime Environ
我想使用 Jekyll 和 GitHub Pages 构建文档站点。问题是 Jekyll 只接受 _posts 下的文件名具有精确的图案,如 YYYY-MM-DD-your-title-is-here
我不知道我发生了什么事。我想访问一个包含多个文件的目录,假设: folder\\1.txt 2.txt 3.txt.... 现在我想根据它们的出现情况来阅读它们,我的意思是首先是最低的,只是我想按升
如何将/放入文件名(即/不分隔路径的组成部分)? 最佳答案 你不知道。 UNIX 文件名中不允许使用斜线。 关于unix - 你如何获得 a/into 文件名?,我们在Stack Overflow上找
我需要复制一个大文件夹,并重命名其中的所有文件和文件夹(如果它们包含特定字符串)。基本上我想复制所有内容并将 10 的任何实例更改为 11。 例如,如果我有一个结构如下的文件夹: mainfolder
我有一个简单的 python (2.7) 脚本,应该执行一些 svn 命令: def getStatusOutput(cmd): print cmd p = subprocess.Po
我正在尝试读取以字符串形式存储在数据文件中的文件名。那里没问题。如果我将它传递给 genfromtxt,我会收到错误“IOError:Z:\Python\Rb input.txt not found”
简单的问题。 当我尝试打开名为 text.txt 的文件时,它可以正常工作。 但是,如果我将文件重命名为 text.cir.txt,则会出现错误。 我可以做什么来修复它? FILE *fd; char
我是 c# 的业余爱好者,我一直无法找到这个问题的答案。也许我不知道要使用的正确术语。 当一个视频文件被拖到我的 exe 应用程序上时,我希望应用程序知道它是用一个文件启动的,并且能够知道该文件的路径
我知道我必须使用 Substring 来删除,但我不知道该怎么做。我需要像这样删除字符串的结尾 来自 "C:\\Users\\myname\\Pictures\\shoeImage.jpg" 到 "C
运行 eclipse 时我收到此错误。但是当我运行我的项目时,它是在内部浏览器中执行的。但它不会在默认的系统浏览器中执行。对此任何一个答案。先谢谢您的回答 最佳答案 您可以从 eclipse 更改浏览
我想要求用户选择一个要从外部存储打开的文件并接收它的路径。最好我想避免过多的编码并使用一些标准方法(众所周知,系统提供的 Intent 或类似方法)。所说的文件是SpatiaLite db文件(*.s
我有一个文件名数据库,我正在尝试使用 PG 的全文搜索工具在其中进行搜索。我在文件名表上运行搜索查询,问题是排名函数没有按照我希望的那样对结果进行排名。为了便于讨论,我们假设架构如下所示: creat
我是一名优秀的程序员,十分优秀!