我正在尝试使用vba创建一个excel表,允许我执行以下操作:
用户点击命令按钮1:创建新工作表标签并提示用户搜索要导入的Excel文件。之后带有vba代码的excel表导入标题为" Importeddata"到新创建的工作表标签。
用户点击commandbutton2:使用text to column拆分E列中的项目 - (即12332 - 西瓜)
用户点击commandbutton3:提示输入项目编号。之后,excel在E列中找到数字并使用所有命令按钮将所有值导入到工作表选项卡(Sheet1)
我目前写了一些代码,但它的效果不太好...感谢您的帮助!
这里是代码:命令按钮2是一个记录的宏(标题为子TexttoColumns),但如果我可以将它合并到代码块中它会更好:
Private Sub CommandButton1_Click()
Dim wkbCrntWorkBook As Workbook
Dim wkbSourceBook As Workbook
Dim rngSourceRange As Range
Dim rngDestination As Range
Set wkbCrntWorkBook = ActiveWorkbook
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = "Importeddata"
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "Excel", "*.xlsx; *.xlsm; *.xlsa"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
Workbooks.Open .SelectedItems(1)
Set wkbSourceBook = ActiveWorkbook
Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="A1", Type:=8)
wkbCrntWorkBook.Activate
Set rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="A1", Type:=8)
rngSourceRange.Copy rngDestination
rngDestination.CurrentRegion.EntireColumn.AutoFit
wkbSourceBook.Close False
End If
End With
End Sub
Private Sub CommandButton3_Click()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
On Error GoTo Err_Execute
'Start search in row 4
LSearchRow = 4
'Start copying data to row 2 in Sheet2 (row counter variable)
LCopyToRow = 2
While Len(Range("A" & CStr(LSearchRow)).Value) > 0
'If value in column E = "Mail Box", copy entire row to Sheet2
If Range("E" & CStr(LSearchRow)).Value = "Mail Box" Then
'Select row in Sheet1 to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into Sheet2 in next row
Sheets("Sheet2").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
'Move counter to next row
LCopyToRow = LCopyToRow + 1
'Go back to Sheet1 to continue searching
Sheets("Sheet1").Select
End If
LSearchRow = LSearchRow + 1
Wend
'Position on cell A3
Application.CutCopyMode = False
Range("A3").Select
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
Sub TexttoColumns()
'
' TexttoColumns Macro
'
' Keyboard Shortcut: Ctrl+t
'
Sheets("Gen6 Data").Select
Columns("E:E").Select
Selection.TexttoColumns Destination:=Range("E1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="-", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
Sheets("Sheet1").Select
End Sub
答案 0 :(得分:0)
仍然不确定具体哪些不适合你。但是,CommandButton1似乎对我来说很好。没有texttocolumns的数据/信息,我真的没有任何反馈。
你可以稍微清理一下CommandButton3。也许是这样的:
public Stop[] possibleStops(GPSCoordinate spot) {
Set<Stop> viableBusStops = new HashSet<Stop>();
for(Map.Entry<String, Stop> stopEntry : stops.entrySet()) {
Stop applicant = stopEntry.getValue();
Double distance = spot.distance(applicant.getGPSCoordinate());
if (distance<=threshold) {
viableBusStops.add(applicant);
}
}
Stop[] stopList = viableBusStops.toArray(new Stop[0])
return stopList;
}