将excel里某些内容提取出来填入一个txt文档里
要做成一个按钮
我举个例子,在excel里
_A________B________C________D________E________F_______
128 129 2 0 RTTF1S2-64&&-95
128 129 3 1 RTTF1S2-96&&-127
…………
…………
根据这里的excel里的内容,生成以下脚本
BLEMI:RP=129,RPT=128,EM=3;!RTTF1S2-96&&-127!
BLEMI:RP=128,RPT=129,EM=4;!RTTF1S2-128&&-159!
…………
…………
其中有一个简单的逻辑:
D为0的时候,A列的内容填到RP里,B列的内容填到RPT里
D为1的时候,A列的内容填到RPT里,B列的内容填到RP里
按下按钮,把所有生成的脚本以一个txt文档格式保存,文件名与excel的文件名相同
[解决办法]
Sub 按钮1_Click() On Error GoTo ex Dim res As String Dim i As Long, rc As Long rc = Sheet1.Range("A1").CurrentRegion.Rows.Count If (rc = 0) Then MsgBox "未发现需要处理的数据!" Exit Sub End If For i = 1 To rc If Sheet1.Range("D" & i) = 0 Then res = res & "BLEMI:" & _ "RP=" & Sheet1.Range("A" & i) & "," & _ "RPT=" & Sheet1.Range("B" & i) & "," & _ "EM=" & Sheet1.Range("C" & i) & ";" & _ "!" & Sheet1.Range("E" & i) & "!" & vbNewLine ElseIf Sheet1.Range("D" & i) = 1 Then res = res & "BLEMI:" & _ "RP=" & Sheet1.Range("B" & i) & "," & _ "RPT=" & Sheet1.Range("A" & i) & "," & _ "EM=" & Sheet1.Range("C" & i) & ";" & _ "!" & Sheet1.Range("E" & i) & "!" & vbNewLine End If Next Dim file_num As Integer, file_name As String file_num = FreeFile() file_name = Replace(ThisWorkbook.FullName, ".xls", ".txt") file_name = Replace(ThisWorkbook.FullName, ".xlsx", ".txt") Open file_name For Output As file_num Print #file_num, res Close file_num MsgBox "OK!" Exit Sub ex: MsgBox Err.Description End Sub