想要执行"文本到列"在每第三列

时间:2016-03-28 00:01:58

标签: excel

我正在尝试遍历电子表格的列并将每个第三列拆分为逗号。我的数据看起来像这样:

coordinates   (blank)    name
x1,y1                    object 1
x2,y2                    object 2

我正在尝试使用宏脚本将其转换为此:

coordinates   (blank)    name
x1            y1         object 1
x2            y2         object 2

我知道我需要一个在列上执行拆分的循环,然后转到三列之外,然后重复。以下是我的尝试,但是当我运行时没有任何反应!

Sub SplitEveryThird(MyRange As Range)
    Dim x As Integer
    SplitEveryThird = 0
    For x = 1 To MyRange.Cells.Count
      If (x Mod 3) = 1 Then
        Dim objRange1 As Range

             'Split column at the comma
              MyRange.TextToColumns _
                   Destination:=Range("A2"), _
                   DataType:=xlDelimited, _
                   Tab:=False, _
                   Semicolon:=False, _
                   Comma:=True, _
                   Space:=False, _
                   Other:=False, _
                   OtherChar:="-"
    End If
  Next x
End Sub

2 个答案:

答案 0 :(得分:1)

这是我的回答:

+---------+--+----------+---------+--+----------+---------+--+----------+---------+--+----------+---------+--+----------+
| X1,Y1   |  | Object1  | X1,Y1   |  | Object1  | X1,Y1   |  | Object1  | X1,Y1   |  | Object1  | X1,Y1   |  | Object1  |
| X2,Y2   |  | Object2  | X2,Y2   |  | Object2  | X2,Y2   |  | Object2  | X2,Y2   |  | Object2  | X2,Y2   |  | Object2  |
| X3,Y3   |  | Object3  | X3,Y3   |  | Object3  | X3,Y3   |  | Object3  | X3,Y3   |  | Object3  | X3,Y3   |  | Object3  |
| X4,Y4   |  | Object4  | X4,Y4   |  | Object4  | X4,Y4   |  | Object4  | X4,Y4   |  | Object4  | X4,Y4   |  | Object4  |
| X5,Y5   |  | Object5  | X5,Y5   |  | Object5  | X5,Y5   |  | Object5  | X5,Y5   |  | Object5  | X5,Y5   |  | Object5  |
| X6,Y6   |  | Object6  | X6,Y6   |  | Object6  | X6,Y6   |  | Object6  | X6,Y6   |  | Object6  | X6,Y6   |  | Object6  |
| X7,Y7   |  | Object7  | X7,Y7   |  | Object7  | X7,Y7   |  | Object7  | X7,Y7   |  | Object7  | X7,Y7   |  | Object7  |
| X8,Y8   |  | Object8  | X8,Y8   |  | Object8  | X8,Y8   |  | Object8  | X8,Y8   |  | Object8  | X8,Y8   |  | Object8  |
| X9,Y9   |  | Object9  | X9,Y9   |  | Object9  | X9,Y9   |  | Object9  | X9,Y9   |  | Object9  | X9,Y9   |  | Object9  |
| X10,Y10 |  | Object10 | X10,Y10 |  | Object10 | X10,Y10 |  | Object10 | X10,Y10 |  | Object10 | X10,Y10 |  | Object10 |
| X11,Y11 |  | Object11 | X11,Y11 |  | Object11 | X11,Y11 |  | Object11 | X11,Y11 |  | Object11 | X11,Y11 |  | Object11 |
| X12,Y12 |  | Object12 | X12,Y12 |  | Object12 | X12,Y12 |  | Object12 | X12,Y12 |  | Object12 | X12,Y12 |  | Object12 |
| X13,Y13 |  | Object13 | X13,Y13 |  | Object13 | X13,Y13 |  | Object13 | X13,Y13 |  | Object13 | X13,Y13 |  | Object13 |
| X14,Y14 |  | Object14 | X14,Y14 |  | Object14 | X14,Y14 |  | Object14 | X14,Y14 |  | Object14 | X14,Y14 |  | Object14 |
| X15,Y15 |  | Object15 | X15,Y15 |  | Object15 | X15,Y15 |  | Object15 | X15,Y15 |  | Object15 | X15,Y15 |  | Object15 |
| X16,Y16 |  | Object16 | X16,Y16 |  | Object16 | X16,Y16 |  | Object16 | X16,Y16 |  | Object16 | X16,Y16 |  | Object16 |
| X17,Y17 |  | Object17 | X17,Y17 |  | Object17 | X17,Y17 |  | Object17 | X17,Y17 |  | Object17 | X17,Y17 |  | Object17 |
| X18,Y18 |  | Object18 | X18,Y18 |  | Object18 | X18,Y18 |  | Object18 | X18,Y18 |  | Object18 | X18,Y18 |  | Object18 |
| X19,Y19 |  | Object19 | X19,Y19 |  | Object19 | X19,Y19 |  | Object19 | X19,Y19 |  | Object19 | X19,Y19 |  | Object19 |
| X20,Y20 |  | Object20 | X20,Y20 |  | Object20 | X20,Y20 |  | Object20 | X20,Y20 |  | Object20 | X20,Y20 |  | Object20 |
| X21,Y21 |  | Object21 | X21,Y21 |  | Object21 | X21,Y21 |  | Object21 | X21,Y21 |  | Object21 | X21,Y21 |  | Object21 |
| X22,Y22 |  | Object22 | X22,Y22 |  | Object22 | X22,Y22 |  | Object22 | X22,Y22 |  | Object22 | X22,Y22 |  | Object22 |
| X23,Y23 |  | Object23 | X23,Y23 |  | Object23 | X23,Y23 |  | Object23 | X23,Y23 |  | Object23 | X23,Y23 |  | Object23 |
| X24,Y24 |  | Object24 | X24,Y24 |  | Object24 | X24,Y24 |  | Object24 | X24,Y24 |  | Object24 | X24,Y24 |  | Object24 |
+---------+--+----------+---------+--+----------+---------+--+----------+---------+--+----------+---------+--+----------+

示例数据:

+-----+-----+----------+-----+-----+----------+-----+-----+----------+-----+-----+----------+-----+-----+----------+
| X1  | Y1  | Object1  | X1  | Y1  | Object1  | X1  | Y1  | Object1  | X1  | Y1  | Object1  | X1  | Y1  | Object1  |
| X2  | Y2  | Object2  | X2  | Y2  | Object2  | X2  | Y2  | Object2  | X2  | Y2  | Object2  | X2  | Y2  | Object2  |
| X3  | Y3  | Object3  | X3  | Y3  | Object3  | X3  | Y3  | Object3  | X3  | Y3  | Object3  | X3  | Y3  | Object3  |
| X4  | Y4  | Object4  | X4  | Y4  | Object4  | X4  | Y4  | Object4  | X4  | Y4  | Object4  | X4  | Y4  | Object4  |
| X5  | Y5  | Object5  | X5  | Y5  | Object5  | X5  | Y5  | Object5  | X5  | Y5  | Object5  | X5  | Y5  | Object5  |
| X6  | Y6  | Object6  | X6  | Y6  | Object6  | X6  | Y6  | Object6  | X6  | Y6  | Object6  | X6  | Y6  | Object6  |
| X7  | Y7  | Object7  | X7  | Y7  | Object7  | X7  | Y7  | Object7  | X7  | Y7  | Object7  | X7  | Y7  | Object7  |
| X8  | Y8  | Object8  | X8  | Y8  | Object8  | X8  | Y8  | Object8  | X8  | Y8  | Object8  | X8  | Y8  | Object8  |
| X9  | Y9  | Object9  | X9  | Y9  | Object9  | X9  | Y9  | Object9  | X9  | Y9  | Object9  | X9  | Y9  | Object9  |
| X10 | Y10 | Object10 | X10 | Y10 | Object10 | X10 | Y10 | Object10 | X10 | Y10 | Object10 | X10 | Y10 | Object10 |
| X11 | Y11 | Object11 | X11 | Y11 | Object11 | X11 | Y11 | Object11 | X11 | Y11 | Object11 | X11 | Y11 | Object11 |
| X12 | Y12 | Object12 | X12 | Y12 | Object12 | X12 | Y12 | Object12 | X12 | Y12 | Object12 | X12 | Y12 | Object12 |
| X13 | Y13 | Object13 | X13 | Y13 | Object13 | X13 | Y13 | Object13 | X13 | Y13 | Object13 | X13 | Y13 | Object13 |
| X14 | Y14 | Object14 | X14 | Y14 | Object14 | X14 | Y14 | Object14 | X14 | Y14 | Object14 | X14 | Y14 | Object14 |
| X15 | Y15 | Object15 | X15 | Y15 | Object15 | X15 | Y15 | Object15 | X15 | Y15 | Object15 | X15 | Y15 | Object15 |
| X16 | Y16 | Object16 | X16 | Y16 | Object16 | X16 | Y16 | Object16 | X16 | Y16 | Object16 | X16 | Y16 | Object16 |
| X17 | Y17 | Object17 | X17 | Y17 | Object17 | X17 | Y17 | Object17 | X17 | Y17 | Object17 | X17 | Y17 | Object17 |
| X18 | Y18 | Object18 | X18 | Y18 | Object18 | X18 | Y18 | Object18 | X18 | Y18 | Object18 | X18 | Y18 | Object18 |
| X19 | Y19 | Object19 | X19 | Y19 | Object19 | X19 | Y19 | Object19 | X19 | Y19 | Object19 | X19 | Y19 | Object19 |
| X20 | Y20 | Object20 | X20 | Y20 | Object20 | X20 | Y20 | Object20 | X20 | Y20 | Object20 | X20 | Y20 | Object20 |
| X21 | Y21 | Object21 | X21 | Y21 | Object21 | X21 | Y21 | Object21 | X21 | Y21 | Object21 | X21 | Y21 | Object21 |
| X22 | Y22 | Object22 | X22 | Y22 | Object22 | X22 | Y22 | Object22 | X22 | Y22 | Object22 | X22 | Y22 | Object22 |
| X23 | Y23 | Object23 | X23 | Y23 | Object23 | X23 | Y23 | Object23 | X23 | Y23 | Object23 | X23 | Y23 | Object23 |
| X24 | Y24 | Object24 | X24 | Y24 | Object24 | X24 | Y24 | Object24 | X24 | Y24 | Object24 | X24 | Y24 | Object24 |
+-----+-----+----------+-----+-----+----------+-----+-----+----------+-----+-----+----------+-----+-----+----------+

结果如下:

 HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://www.strava.com/api/v3/uploads");

        try {
            MultipartEntity entity = new MultipartEntity();
            FileBody bin = new FileBody(new File(FilePath));


            httppost.setHeader("Authorization:", "Bearer " + Token);

            entity.addPart("activity_type", new StringBody("run"));
            entity.addPart("file", bin);
            entity.addPart("data_type", new StringBody("GPX"));

            httppost.setEntity(entity);



            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httppost, responseHandler);
            Log.i("Kenyan Runner", "Response of file upload: " + responseBody);

        } catch (ClientProtocolException e) {
            Log.e("Kenyan Runner", e.toString());
        } catch (IOException e) {
            Log.e("Kenyan Runner", e.toString());
        }finally {
            httpclient.getConnectionManager().shutdown();
        }

希望这对你有所帮助。

答案 1 :(得分:0)

您正在传入一个参数,但也通过为其赋值来将sub视为函数。函数不用于对单元格执行操作;最好把它留给子程序。如果需要返回值,如ERROR_SUCCESS,则使用ByRef将另一个参数传递给子,并根据结果更改其值。

Option Explicit

Sub test()
    With Worksheets("Sheet1")
        SplitEveryThird_2 .Range("A1:O1")
    End With
End Sub

Sub SplitEveryThird_2(rng As Range)
    Dim c As Long

    Application.DisplayAlerts = False

    With rng.EntireColumn
        For c = 1 To .Columns.Count Step 3
            'Split column at the comma
            If CBool(Application.CountA(.Columns(c))) Then
                .Columns(c).TextToColumns Destination:=.Cells(1, c), _
                     DataType:=xlDelimited, Comma:=True, _
                     Tab:=False, Semicolon:=False, _
                     Space:=False, Other:=False, OtherChar:=vbNullString
            End If
        Next c
    End With
End Sub