简体   繁体   English

根据条件连接或 Textjoin 表行

[英]Concatenate or Textjoin for table rows based on criteria

I have a GPS track file that I am importing into Excel (multiple cars in same file) and I want to manipulate and export the data so that it conforms to a gpx file type for a single chosen car.我有一个 GPS 轨道文件,我正在将其导入 Excel(同一文件中的多辆汽车),我想操作和导出数据,使其符合单个选定汽车的 gpx 文件类型。 Some of the columns are not needed from the original file and some text needs to be added between the existing columns.原始文件中不需要某些列,并且需要在现有列之间添加一些文本。 I have built a macro that will do half of what I want but it copies the entire row for that car instead of getting the data in the form I need.我已经构建了一个宏,它可以完成我想要的一半,但它会复制那辆车的整行,而不是以我需要的形式获取数据。

In excel I can use the textjoin formula to achieve the goal I have but I want it to be a macro and that's where I am having the problem.在 excel 中,我可以使用 textjoin 公式来实现我的目标,但我希望它是一个宏,这就是我遇到问题的地方。 Below is some sample data and my macro.下面是一些示例数据和我的宏。 I would enter the car number I am looking for into C21 on sheet1 and only rows that are for that car# (column b) would be moved to sheet2.我会在 sheet1 上的 C21 中输入我要查找的车号,并且只有该车号(b 列)的行才会移动到 sheet2。 The format I need is "trkpt lat="insert lat" lon="insert lon" time/insert time/" and this is where I would concat or textjoin specific portions of the original row onto sheet2 but in the above mentioned format.我需要的格式是“trkpt lat="insert lat" lon="insert lon" time/insert time/",这是我将原始行的特定部分连接或文本连接到 sheet2 的地方,但采用上述格式。 Here is an example of the data and my macro that is only working to copy the entire row这是一个数据示例,我的宏仅用于复制整行

Date/Time          Car# Junk Lat        Lon         Junk2       Converted Date/Time
20221125050122ES    6   0    27.19483   -82.43863   x           2022-11-25T05:01:22-05:00
20221125050158ES    6   0    27.20587   -82.44154   x           2022-11-25T05:01:58-05:00
20221125052215ES    1   0    27.35147   -82.47196   x           2022-11-25T05:22:15-05:00
20221125052355ES    2   0    27.14018   -82.41795   x           2022-11-25T05:23:55-05:00
20221125052449ES    2   0    27.15536   -82.42394   x           2022-11-25T05:24:49-05:00
20221125052519ES    1   0    27.35149   -82.47195   x           2022-11-25T05:25:19-05:00
20221125052539ES    2   0    27.16463   -82.431     x           2022-11-25T05:25:39-05:00
20221125054932ES    3   0    27.2988    -82.44879   x           2022-11-25T05:49:32-05:00
20221125055059ES    3   0    27.27847   -82.44901   x           2022-11-25T05:50:59-05:00
20221125055519ES    4   0    27.31564   -82.26689   x           2022-11-25T05:55:19-05:00
20221125060022ES    4   0    27.31564   -82.26692   x           2022-11-25T06:00:22-05:00
20221125060106ES    6   0    27.18927   -82.43754   x           2022-11-25T06:01:06-05:00
20221125062409ES    2   0    27.14827   -82.41893   x           2022-11-25T06:24:09-05:00
20221125064901ES    3   0    27.29893   -82.4458    x           2022-11-25T06:49:01-05:00
20221125065650ES    4   0    27.31566   -82.26689   x           2022-11-25T06:56:50-05:00
20221125065821ES    4   0    27.31564   -82.26691   x           2022-11-25T06:58:21-05:00
20221125072115ES    1   0    27.35146   -82.47197   x           2022-11-25T07:21:15-05:00

Sub Getdata()
Dim DriverRange As Range
Worksheets(1).Select
Set DriverRange = Worksheets(1).Range("B1", Range("B" & Rows.Count).End(xlUp))
For Each cell In DriverRange
  If cell.Value = Worksheets(1).Range("C21") Then
    lr = Worksheets(2).Range("A" & Rows.Count).End(xlUp).Row
    cell.EntireRow.Copy Destination:=Worksheets(2).Range("A" & lr + 1)
  End If
Next cell
End Sub
output desired when searching for car 6
trkpt lat="27.19483" lon="-82.43863" time/2022-11-25T05:01:22-05:00/
trkpt lat="27.20587" lon="-82.44154" time/2022-11-25T05:01:58-05:00/
trkpt lat="27.18927" lon="-82.43754" time/2022-11-25T06:01:06-05:00/

I have tried several versions of the textjoin worksheet function that would replace the cell.entirerow.copy line of code but it does not grab the correct rows that match up with the car I want.我已经尝试了几个版本的 textjoin 工作表 function 来替换 cell.entirerow.copy 代码行,但它没有获取与我想要的汽车匹配的正确行。 I feel I am headed in the right direction but am missing something.我觉得我正朝着正确的方向前进,但缺少一些东西。

Please, try the next code.请尝试下一个代码。 It should be very fast, using arrays and dropping the processing result at once.应该很快,使用arrays,一下子丢掉处理结果。 I cannot see the column headers, but the code assumes that the data to be processed starts from "A:A" column and ends to "G:G" one, second row:我看不到列标题,但代码假定要处理的数据从“A:A”列开始,到“G:G”一列结束,第二行:

Sub Getdata()
  Dim wsSource As Worksheet, wsDest As Worksheet, lastR As Long
  Dim arrS, arrD, i As Long, k As Long
  Const carNo As Long = 6  'place here the car number
  
  Set wsSource = Worksheets(1)
  Set wsDest = Worksheets(2)
  
  lastR = wsSource.Range("A" & wsSource.rows.count).End(xlUp).row
  arrS = wsSource.Range("A2:G" & lastR).Value 'place the range in an array for faster iteration/processing
  
  ReDim arrD(1 To UBound(arrS), 1 To 3) 'redim the destination array as its maximum possible number of rows
  For i = 1 To UBound(arrS)
        If arrS(i, 2) = carNo Then
            k = k + 1
            arrD(k, 1) = "trkpt lat=""" & arrS(i, 4) & """"
            arrD(k, 2) = "lon=""" & arrS(i, 5) & """"
            arrD(k, 3) = "time/" & arrS(i, 7) & "/"
        End If
  Next i
  If k > 0 Then
        wsDest.Range("A2").Resize(k, 3).Value = arrD
 End If
 
 MsgBox "Ready...": wsDest.Activate
End Sub

Please, send some feedback after testing it.请在测试后发送一些反馈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM