数据导入时重叠内容

gist102

我正在使用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

结果1结果1

test2.txt

Name Name2 Phone Phone2 Address1 Address12 Email Email2 Postcode Postcode2 SR SR2 MTM MTM2 Serial Serial2 Problem Problem2 Action Action2 Dated Dated2

结果2结果2


本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python改进数据的导入时间

导入时的 mongodb 数据类型

PYTHON->创建导入时会打印内容的模块

如何处理 csv 导入时的“列数据被截断”警告?

如何避免在Django中导入时访问数据库?

Python在导入时无法将列表数据识别为列表

检测数据类型将缩短导入时间

首次导入时未在视图中显示DECS数据

使用PHPExcel导入时将表列与数据映射

使用 LOAD DATA 导入 JSON 数据并在导入时指定日期数据类型

XML导入的内容为空库存数据

导入时覆盖常量

导入时传递arg

在 Excel 数据查询中导入时对 txt 数据文件进行排序

为什么使用Excel的“数据连接”导入时某些表头数据丢失

静态导入方法重叠

连接到python文件的Sqlite数据库在作为包导入时导致错误

Solr:通过CSV导入时如何以小写形式存储特定字段的数据?

从Codeigniter中的excel导入时,我无法使用where子句选择数据

从Matlab导入时Simulink拒绝具有负时间值的数据

尝试使用Pandas和SQLAlchemy从数据库导入时出错

R包数据在另一个包中导入时不可用

Blender'HECL'Ripping程序导致导入时动画数据在UE4中丢失

导入时间数据与日期分开,没有添加日期

从未标记的 csv 导入时,在 tensorflow 数据集中标记列的正确方法是什么?

导入时数据不完全正确-Acumatica API

在 cpanel phpmyadmin 上导入时数据库丢失外键、Auto Inc、索引

从CSV导入时的numpy数组

导入时重命名资源