2016-06-26 68 views
0

Worksheet screenshot的Excel/VBA不知道從哪裏開始

所以我做了一個簡單的用戶形式,將讓人們能夠輕鬆登出設備。我想這樣做,如果「設備」欄中的內容出來了,它會在「輸入/輸出」欄中說出來。但如果設備顯示「筆記本電腦1」和最後「日期&時間」列是空的,那麼它將在「IN/OUT」列中的筆記本電腦1旁邊說出。設備列在設備選項之間多選了一個「,」。

我不知道從哪裏開始。到目前爲止,我所做的工作是減去用戶表單條目的列表框和下拉菜單。

Private Sub cmdout_Click() 

Set ws = ThisWorkbook.Worksheets("SignOut") 

Dim sh As Worksheet 
Dim LastRow As Long 

Dim i As Integer 
For i = 0 To equip.ListCount - 1 
If equip.Selected(i) Then 
Msg = Msg & equip.List(i) & ", " 
End If 
Next i 

Msg = Left(Msg, Len(Msg) - 2) 

Dim rngFound As Range 
Dim strFirst As String 
Dim strID As String 
Dim strDay As String 
Dim taken As Integer 

strID = gov.Value 
strDay = "" 

Set rngFound = Columns("C").Find(strID, Cells(Rows.Count, "C"), xlValues, xlWhole) 
If Not rngFound Is Nothing Then 
strFirst = rngFound.Address 
Do 
If LCase(Cells(rngFound.Row, "G").Text) = LCase(strDay) Then 
MsgBox "GOV is still signed out." 
taken = 1 
End If 
Set rngFound = Columns("C").Find(strID, rngFound, xlValues, xlWhole) 
Loop While rngFound.Address <> strFirst 
End If 
If taken = 0 Then 


Application.Worksheets("SignOut").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Now() 
Application.Worksheets("SignOut").Range("B" & Rows.Count).End(xlUp).Offset(1).Value = techname.Value 
Application.Worksheets("SignOut").Range("C" & Rows.Count).End(xlUp).Offset(1).Value = gov.Value 
Application.Worksheets("SignOut").Range("D" & Rows.Count).End(xlUp).Offset(1).Value = Msg 
Application.Worksheets("SignOut").Range("E" & Rows.Count).End(xlUp).Offset(1).Value = otherequip.Value 




End If 

Set rngFound = Nothing 


End Sub 

登錄形式:

Private Sub CommandButton1_Click() 

    Dim rngFound As Range 
    Dim strFirst As String 
    Dim strID As String 
    Dim strDay As String 

    strID = techname1.Value 
    strDay = "" 

    Set rngFound = Columns("B").Find(strID, Cells(Rows.Count, "B"), xlValues, xlWhole) 
    If Not rngFound Is Nothing Then 
     strFirst = rngFound.Address 
     Do 
      Application.Worksheets("SignOut").Cells(rngFound.Row, "G").Value = Now() 
      Set rngFound = Columns("B").Find(strID, rngFound, xlValues, xlWhole) 
     Loop While rngFound.Address <> strFirst 
    End If 

    Set rngFound = Nothing 
End Sub 
+0

歡迎來到Stack Overflow。 SO不是我網站或教學網站的代碼,有許多網站可以幫助您理解基礎知識。 SO是社區將幫助解決現有代碼中特定問題的地方。如果您有現有的代碼,請將其放在原始帖子中。 –

回答

3

1)A 「形式」 是一個特定的在VBA編碼構建體。根據你發佈的內容,我假定你不是指它,而是隻是將這張表格稱爲表格?如果沒有,請發佈您嘗試過的VBA代碼。 2)假設設備列表在「Tracker」列中,您應該使用該列表填充設備列中的下拉列表以確保它們匹配。我還假設您的額外設備列在下拉列表中沒有任何內容,並且如果用戶簽出2個跟蹤項目,則會爲每個項目設置一個行條目。 (我建議擺脫那個專欄,如果你得到的用戶誤用它)

3)既然你問從哪裏開始,我會給你。通過自己計算出確切的語法,您將學到更多東西。你可以谷歌「Excel VBA X」其中X基本上是這些行中的任何一行。

僞 - (將無法運行,需要與實際的代碼替換 - 也忽略了彩色的話,他們並不意味着在任何僞代碼)

Phase 1: 
trigger event on save (event handler is another search term for trigger events) 
Change all equipment values to In 
loop through first date/time column 
IF there is a value in that column and there is not a value in the second date/time column get the name of the equipment from the equipment column 
Find equipment from tracker column change In/Out value on that row to Out 
continue the loop until the next row is blank 

Alternate: 
remove code to check everything in 
add on-edit trigger to equipment column 
add row that was edited to array 
add on-edit trigger to check in date column 
store row number to array 
change loop so it only goes through rows in array 
change if so that if something is checked out but not in, it is set out 
(You will want to do this in case someone selects the wrong thing and then changes it - don't change it to out immediately or you will need logic to realize what was changed to out the previous time and change it back to in.) 
else if something is checked out and has a value in check in date column then set it to in 

Phase 2: 
Implement an actual form that people use to fill in the sheet and check things in and out 
Reuse relevant code from above but eliminate human error on dates and other things 
(I suggest this as phase 2 as you can do this without a form and you will be using less new code. I would definitely use a form myself but it would be better if you wade into the pool instead of diving in. Unless you have coding experience and just need to learn syntax and vocab, then dive away.) 

有很多我想其他的事情如果這是我正在製作的一種表單,那麼這樣做應該讓您開始尋找建立此項目的內容。我試圖儘可能簡化,以免壓倒性。有更好的方法來做到這一點,但這些方法應該是你可以快速掌握的方法,然後在學到更多東西后再改進。祝您的設備跟蹤順利!

編輯:代碼發佈

好吧,後您發佈的代碼,一路到子行之前的頂部和投入:

Option Explicit 

這將導致VBE編輯器會在不少情況下爲您提供更有意義的反饋。例如,你在你的昏暗線之前有你的設定線。如果沒有Option Explicit,當編輯器找到一個尚未用Dim語句聲明的變量時,它就會立即將其設置爲變體類型。這消耗了額外的內存,並意味着錯別字獲得了即時創建的變量。所以,當你正在做的,你在這裏做什麼,你最終

Dim sh As Worksheet ' your sh variable is your worksheet variable. It never gets used again. 
Set ws = ThisWorkbook.Worksheets("SignOut")' the ws here should likely be sh to match the Dim statement ... or the sh in the Dim should be a ws. Except it doesn't ever get used again either. 

無論這些事情在這種情況下,因爲你不重用他們,但如果你有這指的是一個或另一個代碼,你會希望編譯器告訴你,你正在嘗試使用一個尚未聲明的變量,而不是創建一個新的變量。

通常你想把你的Dim語句全部放在子或函數的頂部。 1)更容易找到它們並調試或檢查拼寫。 2)它確保在腳本嘗試引用它們之前它們全部被聲明。

此處的代碼在引用它們之前未填充變量。幾乎任何時候,你有一個變量,你需要填充它之前,你可以做任何事情。有很多方法可以使用工作表中的數據填充變量。如果你更快地熟悉數組而不是後者(或集合而不是數組),那麼對於像這樣的任務,你會有更容易的時間。

一些具體線路:

Dim LastRow as Long 'you have this declared but you need to put in code to get the last row, which will be handy for populating variables later in your code. Do this right after declaring variables. Google excel vba find last row. 
For i = 0 To equip.ListCount - 1 ' you need to populate equip before doing this. Lookup excel vba how to copy a range into variable. Then lookup how to loop through range. You can start it at your first line of data and go down to the LastRow to grab them all into the same array (which will basically be a table). 
msg = Left(msg, Len(msg) - 2) 'lookup excel vba string manipulations if this isn't doing what you want 
'these next lines all have <Variable Name>.value which won't work for you. If those variables are supposed to be things that the sheet prompts someone to enter and then they get populated here you can see how to do that here-> http://www.excel-vba-easy.com/vba-userform-excel-vba.html 
Application.Worksheets("SignOut").Range("B" & Rows.Count).End(xlUp).Offset(1).Value = techname.Value 
Application.Worksheets("SignOut").Range("C" & Rows.Count).End(xlUp).Offset(1).Value = gov.Value 
Application.Worksheets("SignOut").Range("D" & Rows.Count).End(xlUp).Offset(1).Value = msg 
Application.Worksheets("SignOut").Range("E" & Rows.Count).End(xlUp).Offset(1).Value = otherequip.Value 

隨着你將要確保你有一個短迴路測試,並通過一遍又一遍步驟你的循環untils。如果你的邏輯錯誤並且無限循環結束,你可以很容易地停止通過並修復它(或者在通過,然後停止和重新測試時修復它),但是如果你只是玩遊戲,那麼excel會凍結在你身上。

如果遇到特定步驟的問題,您可能會在SO上找到很多現有的東西。如果沒有,請發佈一個新的線程,其中包含該步驟的具體內容。這篇文章將會在幾頁之後下載,如果你把它放在這裏,人們可能不會看到你的問題。此外,它應該擁有自己的線程,因爲在那個時候您已經經過了「從哪裏開始」的階段。

祝你好運!