摘要:将值从列导出到INI文件。
我有一个电子表格,其中包含M列中的数据,我希望将数据从M列复制到一个.ini文件中。列中的每个单元格都包含个人进度报告的文件名。这些数据将类似于:
Smith, John Progress Report 2015-07-30.doc.ini文件应该类似于:
[Settings]
smith=Smith, John Progress Report 2015-07-30我希望使用小写的键,并从文件名中删除".doc"。
我希望将这些文件名导出到.ini,并使用姓氏作为密钥。我真不知道该从何说起。Excel没有自己的权限来写入.ini文件,但它可以使用Word或Windows从我在互联网上看到的东西。此外,我猜Excel只需将数据作为文本文件附加到.ini中即可。
所以我有一些想法,但我在StackOverflow上没有看到任何其他的东西,它们专门为我的工作做准备。如果你发现有什么特别的东西,那就太好了。否则,如果有人能让我开始写代码来完成这个任务,我会非常感激的。
发布于 2015-07-30 22:20:40
这应该能让你开始。它使用来自M1的值并将其写入.ini文件。只需添加一个循环来迭代所有行,您就应该都设置好了。
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Sub ExportToIni()
Dim re As Object
Set re = CreateObject("VBScript.RegExp")
re.IgnoreCase = True
re.Pattern = "^(([^,]+),.+)\.doc$"
Dim strText As String
strText = Trim$(Cells(1, 13)) ' [M1]
Dim m As Object, c As Object, strLastName As String, strFileName As String
If re.Test(strText) Then
Set c = re.Execute(strText)
strFileName = c(0).SubMatches(0)
strLastName = LCase$(c(0).SubMatches(1))
WritePrivateProfileString "Settings", strLastName, strFileName, "c:\path\to\your.ini"
End If
End Sub.ini文件:
[Settings]
smith=Smith, John Progress Report 2015-07-30发布于 2015-07-31 04:46:13
这能做你想做的事:
Sub SaveIni()
Dim c as range
Open "C:\Temp\Output.ini" For Output As #1
For Each c In Range("M1:M" & Range("M" & Rows.Count).End(xlUp).Row)
Print #1, LCase(Split(c, ",")(0)) & "=" & Split(c, ".doc")(0)
Next
Close #1
End Sub在逗号上使用split来获取名称和小写,然后添加一个=,然后在.doc上拆分初始字符串来删除它,如果您愿意的话,我们可以在这里使用替换。
下面是我的测试输入:
Smith, John Progress Report 2015-07-30.doc
davis, andrew Progress Report 2015-07-30.doc
MALCOLMSON, Aaron File 20jio32ejo23oji32eojie23oj23eoje23joe3e23oji-07-30.doc
Green, Frank Report 20-07-30.doc这是我的ini文件:
smith=Smith, John Progress Report 2015-07-30
davis=davis, andrew Progress Report 2015-07-30
malcolmson=MALCOLMSON, Aaron File 20jio32ejo23oji32eojie23oj23eoje23joe3e23oji-07-30
green=Green, Frank Report 20-07-30注意,如果ini文件已经存在,这将覆盖它,如果您希望追加它,那么更改如下:
Open "C:\Temp\Output.ini" For Output As #1
对此:
Open "C:\Temp\Output.ini" For Append As #1
https://stackoverflow.com/questions/31734180
复制相似问题