2016-06-15 28 views
0

我現在有一個VBScript接受一個Excel文件並重新格式化爲另一個Excel文檔那是更加有組織英寸 此代碼還必須看目錄欄(「B1」)的值,並將其放置在圖紙列(「M1」),只有當值的開頭「EDASM」開始,「EDBSM」等,但移動時必須消除「ED」前綴。從另一列填寫色譜柱的值if語句(S)

例如,目錄號EDF12-01114將導致圖紙欄中沒有任何內容,但對於EDSM10265,我們需要將SM10265放置在圖紙欄中(放下「ED」)。

所有到目前爲止,我所得到的是這個,這是不完整的,甚至:

Set objRange = objWorkSheet.Range("M1").EntireColumn 
IF 
    objWorkSheet.Range("B1").Row = "EDF*" THEN 'Maybe correct-ish? Not sure about syntax 
    objRange = Null 
Else 
    objRange = ("B1") 'Totally an awful guess, but I have no clue what to put here 
End If 

我見過有循環和諸如此類的東西類似的代碼,但沒有人似乎在做什麼,我需要完成。謝謝!基於關BruceWayne的當前代碼:

編輯。仍然不返回在Excel數據表的繪圖列東西,但它看起來像它更接近...

Sub move_Text() 
Dim lastRow, nextRow, cel , rng 

lastRow = Cells(Rows.Count, 2).End(xlUp).Row ' Since your Col. B is the data, let's find that column's last row 
Set rng = Range(Cells(1, 2), Cells(lastRow, 2)) 

nextRow = 1 

For Each cel In rng 
If Left(cel.Value, 3) <> "EDF" Then 
    Cells(nextRow, 13).Value = Mid(cel.Value, 3, Len(cel.Value) - 2) 
    nextRow = nextRow + 1 
End If 
Next 

End Sub 

另一個編輯! 目錄列現在是「C」,而不是「B」。另外,我有兩個標題行,所以第一個目錄號位於「C3」中。

再次感謝!我們正在接近。

下面是谷歌雲端硬盤文件:https://drive.google.com/folderview?id=0B2MeeQ3BKptFYnZfQWpwbTJxMm8&usp=sharing

重要的是記住

在谷歌雲端硬盤文件:TestScript.vbs是所有的代碼文件。腳本運行時,請選擇ExcelImport。這應該返回FinalDocument

+0

您是否檢查前2個字符是否爲「ED」?或者你只是在尋找「EDASM」和「EDBSM」?或者你有一個清單:「EDASM」,「EDBSM」,「EDJASM」,「EDKBSM」...等。 –

+0

一切都將有前兩個字符作爲「ED」,但如果它是「EDF」,我需要一個空值轉移到列M.如果它是除「EDF」:「EDASM」,「EDBSM」,「EDJASM 「,」EDKBSM「...等,它需要刪除前綴」ED「並將值移到 – cdomination

+0

由於您知道**它將全部以」ED「開始,爲什麼不使用'If Left(Range 「B1」),2)=「ED」然後...'。這樣你就可以避免使用通配符的任何缺陷(從我的頭頂開始,我不記得Excel是否會像查找'ED *'一樣讀取你的行,或者如果它識別'*'爲通配符)。 – BruceWayne

回答

1

我想這是你在找什麼:

Sub move_Text() 
    Dim lastRow, nextRow, cel, rng 

    'get last row with data in Column B 
    lastRow = Cells(Rows.Count, "B").End(xlUp).Row 
    'set your range starting from Cell B2 
    Set rng = Range("B2:B" & lastRow) 

    'loop through all the cells in the range to check for "EDF" and "ED" 
    For Each cel In rng 
     'below condition is to check if the string starts with "EDF" 
     If cel.Value Like "EDF*" Then 
      'do nothing 
     'below condition is to check if the string starts with "ED" 
     ElseIf cel.Value Like "ED*" Then 
      'drop first two characters of cell's value and write in Column M 
      cel.Offset(0, 11).Value = Right(cel.Value, Len(cel.Value) - 2) 
     'else condition will be executed when none of the above two conditions are satisfied 
     'else condition is based on the link mentioned in your question that will handle words like "ELECTRICAL BOX" 
     Else 
      'write cell's value in Column Q 
      cel.Offset(0, 11).Value = cel.Value 
     End If 
    Next 
End Sub 

編輯:對於VBScirpt ________________________________________________________________________________

Sub Demo() 
    Dim lastRow, nextRow, cel, rng 

    Const xlShiftToRight = -4161 
    Const xlUp = -4162 
    Const xlValues = -4163 
    Const xlWhole = 1 
    Const xlPrevious = 2 

    With objWorksheet 
     'get last row with data in Column B 
     lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row 

     'set your range starting from Cell B2 
     Set rng = .Range("C2:C" & lastRow) 
    End With 

    'loop through all the cells in the range to check for "EDF" and "ED" 
    For Each cel In rng 
     'below condition is to check if the string starts with "EDF" 
     If InStr(1, cel.Value, "EDF", 1) = 1 Then 
      'do nothing 
     'below condition is to check if the string starts with "ED" 
     ElseIf InStr(1, cel.Value, "ED", 1) = 1 Then 
      'drop first two characters of cell's value and write in Column M 
      cel.Offset(0, 10).Value = Right(cel.Value, Len(cel.Value) - 2) 
     'else condition will be executed when none of the above two conditions are satisfied 
     'else condition is based on the link mentioned in your question that will handle words like "ELECTRICAL BOX" 
     Else 
      'write cell's value in Column M 
      cel.Offset(0, 10).Value = cel.Value 
     End If 
    Next 
End Sub 
+0

這絕對看起來像我想要的,但它仍然沒有返回。 – cdomination

+0

@cdomination - 它拋出任何錯誤? – Mrig

+0

根本沒有。它正在運行並生成新的excel,M列中的值只是缺失 – cdomination

1

這個怎麼工作的嗎?

Sub move_Text() 
Dim lastRow&, nextRow& 
Dim cel As Range, rng As Range 

lastRow = Cells(Rows.Count, 2).End(xlUp).Row ' Since your Col. B is the data, let's find that column's last row 
Set rng = Range(Cells(1, 2), Cells(lastRow, 2)) 

nextRow = 1 

For Each cel In rng 
    If Left(cel.Value, 2) = "ED" Then 
     Cells(nextRow, 13).Value = Mid(cel.Value, 3, Len(cel.Value) - 2) 
     nextRow = nextRow + 1 
    End If 
Next cel 

End Sub 

它將範圍設置爲您的列B,從第1行到最後一行。然後,循環通過那裏的每個單元格,檢查左邊的兩個字母。如果「ED」,然後移動數據,但脫掉「ED」。

編輯:你使用VBScript剛剛意識到。從聲明中刪除as Range&,所以它只是Dim lastRow, nextRow, cel, rng

+0

它不讓我運行它,我一直在'Sub move_Text()'行上出現錯誤「預期的語句結束」。代碼800A0401錯誤 – cdomination

+0

@ChristinaDocenko - 嗯,你有沒有其他的宏?你把這個放在工作表模塊中嗎?您是否複製/粘貼上述內容? – BruceWayne

+0

我沒有任何宏,我目前正在使用Notepad ++編輯文件。我複製並粘貼了你的代碼。我刪除了最後/下一行之後的那個,並且保留了那個。現在它不喜歡下一行'Dim cel As Range,rng As Range' at character 9 – cdomination

1

如果條件滿足,這將列B值(減去ED前綴)複製到列M.

Sub move_Text() 
Dim lastRow , i 

lastRow = Cells(Rows.Count, 3).End(xlUp).Row 

For i = 3 To lastRow 
    If Left(Cells(i, 3), 2) = "ED" And Not (Left(Cells(i, 3), 3) = "EDF") Then 
     Cells(i, 13).Value = Right(Cells(i, 3, Len(Cells(i, 3)) - 2) 
    End If 
Next 

End Sub 
+0

在假定'lastRow = Cells(Rows.Count,3).End(xlUp).Row'仍然不起作用的情況下。另外,它實際上是C列我們正在複製,但對「Cells(i,3)」的簡單更改應該修復了這個問題。另外,因爲它是VBScript,所以我不能將「作爲...」標識符 – cdomination

0

爲什麼不使用某些Excel的公式,以加快整個事情都起來了:

Sub My_Amazing_Solution() 

    Range("M3").FormulaR1C1 = "=IF(TRIM(LEFT(RC[-10],2))=""ED"",RIGHT(TRIM(RC[-10]),LEN(RC[-10])-2),"""")" 
    Range("M3").AutoFill Destination:=Range("M3:M" & Range("C1048576").End(xlUp).Row), Type:=xlFillDefault 
    Application.Wait Now + TimeValue("00:00:03") 
    Range("M3:M" & Range("C1048576").End(xlUp).Row).Copy 
    Range("M3").PasteSpecial xlPasteValues 

End sub 

這應該爲你做!