我正在使用Excel VBA进行项目,但是导入数据时遇到问题。每次导入数据时,它都会与现有列重叠。
有人可以帮我解决这个问题,下面是代码。
Private Sub CommandButton1_Click()
Dim myFile As String, text As String, textline As String, Name As Integer, Phone As Integer, Address1 As Integer, Dated As Integer
Dim Email As Integer, Postcode As Integer, SR As Integer, MTM As Integer, Serial As Integer, Problem As Integer, Action As Integer
myFile = "C:\Users\test.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
Name = InStr(text, "Name")
Phone = InStr(text, "Phone")
Address1 = InStr(text, "Address1")
Email = InStr(text, "Email")
Postcode = InStr(text, "Postcode")
SR = InStr(text, "SR")
MTM = InStr(text, "MTM")
Serial = InStr(text, "Serial")
Problem = InStr(text, "Problem")
Action = InStr(text, "Action")
Dated = InStr(text, "Dated")
Range("C11").Value = Mid(text, Name + 6, 15)
Range("H13").Value = Mid(text, Phone + 6, 8)
Range("C15").Value = Mid(text, Address1 + 9, 25)
Range("C13").Value = Mid(text, Email + 6, 15)
Range("H16").Value = Mid(text, Postcode + 9, 5)
Range("C10").Value = Mid(text, SR + 4, 8)
Range("H14").Value = Mid(text, MTM + 5, 8)
Range("H15").Value = Mid(text, Serial + 8, 9)
Range("C17").Value = Mid(text, Problem + 9, 15)
Range("C18").Value = Mid(text, Action + 7, 10)
Range("H10").Value = Mid(text, Dated + 7, 10)
End Sub
在所附工作表中查找数据。手机跳到name列,这同样适用于其他列。H13中的数据也将传入C11。
。
编辑
保罗,您好,我在打印工作表并将其转换为PDF时仍然遇到问题。
不使用您的第一或第二个代码。我可以将以下代码运行到工作表中并转换为PDF,但是现在当您运行第一和第二个代码后,以下代码无法将工作表转换为PDF ...我一直在获取“应用程序定义的错误或对象定义的错误”和运行时错误“ 1004”文档未保存。该文档可能已打开,或者在保存时可能遇到错误。
我可以知道我的代码有什么问题吗?
Private Sub CommandButton2_Click()
Dim FilePath As String
Dim FileName As String
Dim MyDate As String
Dim report As String
Dim Name As String
FilePath = "C:\Users\Documents\test\"
MyDate = Format(Date, " - MM-DD-YYYY")
report = " - Quatation"
Name = Worksheets("Sheet1").Range("C10")
Sheets("Sheet1").Range("A1:I60").ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=FilePath & Name & MyDate & report
End Sub
Private Sub report()
Dim myFile As String, lastRow As Long
myFile = "C:\Users\Documents\test\" & Sheets("Sheet1").Range("C11") & "_" & Sheets("Sheet1").Range("C17") & Format(Now(), "yyyy-mm-dd") & ".pdf"
lastRow = Sheets("Sheet3").UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1
'Transfer data to sheet3
Sheets("Sheet3").Cells(lastRow, 1) = Sheets("Sheet1").Range("C11")
Sheets("Sheet3").Cells(lastRow, 2) = Sheets("Sheet1").Range("C17")
Sheets("Sheet3").Cells(lastRow, 3) = Sheets("sheet1").Range("I28")
Sheets("Sheet3").Cells(lastRow, 4) = Now
Sheets("Sheet3").Hyperlinks.Add Anchor:=Sheets("Sheet3").Cells(lastRow, 5), Address:=myFile, TextToDisplay:=myFile
'Create invoice in PDF format
Sheets("sheet1").ExportAsFixedFormat Type:=xlTypePDF, FileName:=myFile
Application.DisplayAlerts = False
'create invoice in XLSX format
ActiveWorkbook.SaveAs "C:\Users\Documents\test\" & Sheets("Sheet1").Range("C11") & "_" & Sheets("Sheet1").Range("C17") & "_" & Format(Now(), "yyyy-mm-dd") & ".xlsx", FileFormat:=51
'ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub
您可以使代码更高效,可维护且更具动态性
下面的两个版本根据下一个令牌("Phone"
)
相对于当前令牌("Name"
)的位置确定数据的大小
。
版本1使用数组将令牌映射到上的不同单元Sheet5
Option Explicit
Private Sub CommandButton1_Click()
Const FULL_PATH = "C:\Users\test1.txt"
Const TOKENS = "Name Phone Address1 Email Postcode SR MTM Serial Problem Action Dated"
Const LOCATIONS = "C11 H13 C15 C13 H16 C10 H14 H15 C17 C18 H10"
Dim fId As String, txt As String, txtLen As Long, idArr As Variant, locArr As Variant
fId = FreeFile
Open FULL_PATH For Input As fId
txt = Input(LOF(fId), fId) 'Read entire file (not line-by-line)
Close fId
txtLen = Len(txt)
idArr = Split(TOKENS)
locArr = Split(LOCATIONS)
Dim i As Long, k As String, sz As Long, found As Long, ub As Long
ub = UBound(idArr)
With ThisWorkbook.Worksheets("Sheet5") '<--- Update sheet name
For i = LBound(idArr) To ub
k = idArr(i) 'Name, Phone, etc
found = InStr(txt, k) + Len(k) + 1 'Find current key in file
If found > 0 Then 'Determine item length by finding the next key
If i < ub Then sz = InStr(txt, idArr(i + 1)) Else sz = txtLen + 2
.Range(locArr(i)).Value2 = Trim$(Mid$(txt, found, sz - found - 1))
End If
Next
End With
End Sub
。
第2版使用字典
Private Sub CommandButton1_Click()
Const FULL_PATH = "C:\Users\test2.txt"
Dim fId As String, txt As String, txtLen As Long, d As Object, dc As Long
fId = FreeFile
Open FULL_PATH For Input As fId
txt = Input(LOF(fId), fId) 'Read entire file (not line-by-line)
Close fId
txtLen = Len(txt)
Set d = CreateObject("Scripting.Dictionary")
d("Name") = "C11" 'Same as: d.Add Key:="Name", Item:="C11"
d("Phone") = "H13"
d("Address1") = "C15"
d("Email") = "C13"
d("Postcode") = "H16"
d("SR") = "C10"
d("MTM") = "H14"
d("Serial") = "H15"
d("Problem") = "C17"
d("Action") = "C18"
d("Dated") = "H10"
dc = d.Count
Dim i As Long, k As String, sz As Long, found As Long
With ThisWorkbook.Worksheets("Sheet5") '<--- Update sheet name
For i = 0 To dc - 1 'd.Keys()(i) is a 0-based array
k = d.Keys()(i) 'Name, Phone, etc
found = InStr(txt, k) + Len(k) + 1 'Find the (first) key in file
If found > 0 Then 'Determine item length by finding the next key
If i < dc - 1 Then sz = InStr(txt, d.Keys()(i + 1)) Else sz = txtLen + 2
.Range(d(k)).Value2 = Trim$(Mid$(txt, found, sz - found - 1))
End If
Next
End With
End Sub
。
test1.txt
Name Name1
Phone Phone1
Address1 Address11
Email Email1
Postcode Postcode1
SR SR1
MTM MTM1
Serial Serial1
Problem Problem1
Action Action1
Dated Dated1
。
test2.txt
Name Name2 Phone Phone2 Address1 Address12 Email Email2 Postcode Postcode2 SR SR2 MTM MTM2 Serial Serial2 Problem Problem2 Action Action2 Dated Dated2
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句