多列這是一個具有不同的列如D_ID,模塊,函數名等我的消息來源表進口從一張新紙
我想出口幾列,比如「D_ID 「,」Function_Name「,」Sub_Unit_Number「,」長度「從ActiveSheet到新的工作表。
我的輸出工作應該是這樣的:
,我試圖用首先使用VBA代碼來創建一個新的工作表,然後使用for循環來遍歷範圍的邏輯(」 A1:K1「)來查找該範圍內的每個單元格值是否與字符串」D_ID「,」Function_Name「,」Sub_Unit_Number「,」length「匹配。如果是的話,那麼我需要寫在新的工作表中。
但我的邏輯失敗了。
我真的很感激,如果有人可以幫助我用不同的邏輯或代碼來獲得它的工作。
Sub CreateNewSheet()
Dim rep As Integer
Dim sheet_name_to_create As String
sheet_name_to_create = "Test_Sheet"
'a statement to go through other worksheets
For rep = 1 To (Worksheets.Count)
'search the sheet wth the name if that exists
If LCase(Sheets(rep).Name) = LCase(sheet_name_to_create) Then
MsgBox "This sheet already exists"
Exit Sub
End If
Next
Sheets.Add After:=Sheets("Original_Sheet")
Sheets(ActiveSheet.Name).Name = sheet_name_to_create
End Sub
Sub Extract_data()
' this sub routine extracts D_ID, Function_Name, Sub_Unit_Number
'and Length from the Original_Sheet
Dim LastColumn As Integer
Dim cell As Range
Dim sht As Worksheet
Dim data As String
Set sht = Worksheets("Original_Sheet")
LastColumn = sht.Cells(1, .Columns.Count).End(xlToLeft).Column
For Each cell In sht.Range("A1:K" & LastColumn).Cells
data = cell.Value
If data = "D_ID" Then
' I need to import to the new worksheet
ElseIf data = "Function_Name" Then
' I need to import to the new worksheet
ElseIf data = "Sub_Unit_Number" Then
' I need to import to the new worksheet
ElseIf data = "Length" Then
' I need to import to the new worksheet
End If
End Sub
Sub MyNewProcedure()
Call CreateNewSheet
Call Extract_data
End Sub
提交到目前爲止編寫的代碼。 – nilsman
@nilsman我已經添加了代碼,但它不完全正確,因爲我現在使用vba進行此任務,並且我不熟悉語法和全部。 – user28