在 vba 中完成后,从换行文本中提取的数据不会进入下一列单元格
Data being extracted from wrapped text not going to next column cell after complete in vba
我设法从工作簿的包装单元格中提取数据并将该数据发送到另一个工作簿。包装的单元格逐行显示文本,我使用拆分文本遍历每个单元格以获取我想要的数据。问题是在完成对包装单元格的迭代后,它不会转到下一列。我希望它转到下一栏并重复我刚才提到的过程。
我知道我设置的名为 splitText 的变量的范围显示为 .Range("I3")。
所以我知道这是设定的范围。我只是不知道如何对其进行编码以转到下一专栏。换行文本中的数据位于 I3 列中。我需要我的脚本在提取完成后转到I4等等。
我的代码如下:
Sub Get_Data_From_File()
Dim FileToOpen As Variant
' Dim i&, s, v
Dim rng As Range
Dim lr As Long
Dim value As String
Dim x As Variant
Dim i As Integer
Dim pos1 As Integer
Dim pos2 As Integer
'Variable declaration
Dim sWorkbook As Workbook
'Create New Workbook
Set sWorkbook = Workbooks.Add
'Save Above Created New Workbook
sWorkbook.SaveAs Filename:="C:\Users\username\Desktop\file.xlsx"
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Set cRange = OpenBook.Worksheets("my-worksheet").Range("I3:I10")
For Each column In Range("A3:A4")
splitText = Split(OpenBook.Worksheets("my-worksheet").Range("I3").value, Chr(10))
Debug.Print value
counter = 3
For i = 0 To UBound(splitText)
value = splitText(i)
pos1 = InStr(value, "show")
pos2 = InStr(value, "more")
myArray = Split(value)
If pos1 = 1 Or pos2 = 1 Then
Debug.Print value
If InStr(1, value, 9, 1) Then
textWithNumberUse9 = Replace(value, "show", 1)
textWithNumberAnd9 = Replace(value, "more", 1)
removeForUse = Trim(Replace(textWithNumberUse9, "and", ""))
removeForAnd = Trim(Replace(textWithNumberAnd9, "and", ""))
End If
If InStr(1, value, 6, 1) Then
textWithNumberUse6 = Replace(value, "show", "")
textWithNumberAnd6 = Replace(value, "more", "")
removeForUse = Trim(Replace(textWithNumberUse6, "and", ""))
removeForAnd = Trim(Replace(textWithNumberAnd6, "and", ""))
End If
If InStr(1, value, "show", 1) Then
Workbooks("Test.xlsx").Worksheets("Sheet1").Range("D" & counter).value = removeForUse
End If
If InStr(1, value, "more", 1) Then
Workbooks("Test.xlsx").Worksheets("Sheet1").Range("D" & counter).value = removeForAnd
End If
counter = counter + 1
End If
Next i
Next column
将 splitText = ...
行更改为此
splitText = Split(OpenBook.Worksheets("my-worksheet").Range("I" & column.Row).value, Chr(10))
(鉴于这看起来,你真的应该重新考虑你的 column
变量的名称...)
我设法从工作簿的包装单元格中提取数据并将该数据发送到另一个工作簿。包装的单元格逐行显示文本,我使用拆分文本遍历每个单元格以获取我想要的数据。问题是在完成对包装单元格的迭代后,它不会转到下一列。我希望它转到下一栏并重复我刚才提到的过程。
我知道我设置的名为 splitText 的变量的范围显示为 .Range("I3")。 所以我知道这是设定的范围。我只是不知道如何对其进行编码以转到下一专栏。换行文本中的数据位于 I3 列中。我需要我的脚本在提取完成后转到I4等等。
我的代码如下:
Sub Get_Data_From_File()
Dim FileToOpen As Variant
' Dim i&, s, v
Dim rng As Range
Dim lr As Long
Dim value As String
Dim x As Variant
Dim i As Integer
Dim pos1 As Integer
Dim pos2 As Integer
'Variable declaration
Dim sWorkbook As Workbook
'Create New Workbook
Set sWorkbook = Workbooks.Add
'Save Above Created New Workbook
sWorkbook.SaveAs Filename:="C:\Users\username\Desktop\file.xlsx"
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Set cRange = OpenBook.Worksheets("my-worksheet").Range("I3:I10")
For Each column In Range("A3:A4")
splitText = Split(OpenBook.Worksheets("my-worksheet").Range("I3").value, Chr(10))
Debug.Print value
counter = 3
For i = 0 To UBound(splitText)
value = splitText(i)
pos1 = InStr(value, "show")
pos2 = InStr(value, "more")
myArray = Split(value)
If pos1 = 1 Or pos2 = 1 Then
Debug.Print value
If InStr(1, value, 9, 1) Then
textWithNumberUse9 = Replace(value, "show", 1)
textWithNumberAnd9 = Replace(value, "more", 1)
removeForUse = Trim(Replace(textWithNumberUse9, "and", ""))
removeForAnd = Trim(Replace(textWithNumberAnd9, "and", ""))
End If
If InStr(1, value, 6, 1) Then
textWithNumberUse6 = Replace(value, "show", "")
textWithNumberAnd6 = Replace(value, "more", "")
removeForUse = Trim(Replace(textWithNumberUse6, "and", ""))
removeForAnd = Trim(Replace(textWithNumberAnd6, "and", ""))
End If
If InStr(1, value, "show", 1) Then
Workbooks("Test.xlsx").Worksheets("Sheet1").Range("D" & counter).value = removeForUse
End If
If InStr(1, value, "more", 1) Then
Workbooks("Test.xlsx").Worksheets("Sheet1").Range("D" & counter).value = removeForAnd
End If
counter = counter + 1
End If
Next i
Next column
将 splitText = ...
行更改为此
splitText = Split(OpenBook.Worksheets("my-worksheet").Range("I" & column.Row).value, Chr(10))
(鉴于这看起来,你真的应该重新考虑你的 column
变量的名称...)