簡體   English   中英

我想從特定月份(在組合框中給出)而不是從某個日期返回上個月

[英]I want to return to a previous month from a particular month (which are given in combo box) not from a date

我必須每月為上個月支付工資。 直到現在我都是在excel上手動完成的。 現在我想每個月通過 VBA 創建這個。 所以我創建了一個用戶表單,其中有三個組合框。 在第一個中,我選擇了我想要為其制作工資單的公司名稱,在第二個組合框中,給出了從一月到十二月的月份,並給出了第二年。

一個組合框中的值是本工作簿本身給出的列表。 第二個和第三個組合框(月和年組合框)中的值來自下面提到的代碼。

Private Sub UserForm_Activate()
Dim i As Integer
With Me.cmbmonth1
    For i = 1 To 12
        .AddItem VBA.Format(VBA.DateSerial(2021, i, 1), "mmmm")
    Next i
    
        .Value = Format(DateAdd("m", -1, Date), "mmmm")

End With

For i = VBA.Year(Date) - 1 To VBA.Year(Date) + 20
    Me.cmbyear.AddItem i
Next i
    .Value = VBA.Format(VBA.Date, "yyyy")
    If Me.cmbmonth1.Value = "December" Then
    Me.cmbyear.Value = Format(DateAdd("yyyy", -1, Date), ("yyyy"))
    Else
    Me.cmbyear.Value = Format(Date, "yyyy")
    End If
    
End Sub

首先,我想為我想要制作工資單的月份創建一個文件夾。 為此,我創建了代碼,並且很容易創建月文件夾。

現在來回答這個問題。 我想復制上個月創建的上個月工資單。 例如,這個月我必須創建 2021 年 6 月的工資單。要創建這個,我必須從 2021 年 5 月的文件夾(我稱之為上個月)復制工資單並將其粘貼到 2021 年 6 月的文件夾中。 之后,我將進行必要的更改,例如添加或刪除員工並標記他們的出勤率。

我正在尋找一個代碼,它可以從我在第二個組合框中選擇的月份的上個月的月份文件夾中復制工資單。 例如,如果我在組合框中選擇 2021 年 6 月,則工資單應從 2021 年 5 月(已經存在)復制並粘貼到 2021 年 6 月文件夾中。 如果我在組合框中選擇 2021 年 5 月,則應從 2021 年 4 月開始復制工資單,依此類推...

我嘗試過類似下面的代碼,並且在這些代碼無法執行所需的任務時發表了很多評論。

Private Sub CmdSubmit_Click()

Dim startPath As String
Dim myName As String
Dim myName1 As String
Dim previousmonthpayroll As String
Dim fso As Scripting.FileSystemObject
Dim mm As Date
Dim previousMonth
'Dim exampledate As Date



On Error Resume Next
'Application.Wait (Now + TimeValue(#12:00:03 AM#))
startPath = "C:\Users\dell\OneDrive\ESI PF"
mm = 22 - Me.cmbmonth1.Value - 2021
previousMonth = VBA.Format(VBA.DateSerial(Year(mm), Month(mm) - 1, Day(mm)), "mmmm")
'exampledate = DateValue("me.cmbmonth1.value 1,2020")



myName = Me.cmbCompanyName.Text & Application.PathSeparator & Me.cmbmonth1.Value & " " & Me.cmbyear.Text
myName1 = Me.cmbCompanyName.Text & Application.PathSeparator & previousMonth & " " & Me.cmbyear.Value ' for previous month

Dim folderpathwithname As String
Dim folderpathwithname1 As String   ' for previous month

folderpathwithname = startPath & Application.PathSeparator & myName
folderpathwithname1 = startPath & Application.PathSeparator & myName1   ' for previous month

Set fso = New Scripting.FileSystemObject
If Dir(folderpathwithname, vbDirectory) = vbNullString Then
        fso.Createfolder folderpathwithname
    
      '  End If
      Else
'Dim PayrollName As String
'Dim PreviousMonthfolder As Folder

Shell "cmd /C start """" /max """ & startPath & Application.PathSeparator & myName1 & """", vbNormalFocus
'Shell "cmd /C start """" /max """ & folderpathwithname1 & """", vbNormalFocus


'PayrollName = "Master Payroll" & "_" & Me.cmbmonth1.Value & " " & Me.cmbyear.Value & "_" & Me.cmbCompanyName.Value
'PreviousMonthfolder = startPath & "\" & Me.cmbCompanyName.Value
'previousmonthpayroll = ("startPath & " \ " & Me.cmbCompanyName.Value & " \ " Me.cmbmonth1.Value-1 & " " & Me.cmbyear.Value & " \ " Master Payroll*.xl??")

 

End If
'If PayrollName = VBA.Constants.vbNullString Then
 '   previousmonthpayroll.Select
  '  Selection.Copy
   ' VBA.FileSystem.Dir(folderpathwithname, vbDirectory).pastspecial
    
'Else
'Exit Sub
'End If


'Dim filename As String
'filename = VBA.FileSystem.Dir("C:\Users\dell\Google Drive\Goods and Service Tax-GST\Arun Kumar\Testing*.xl??")
'If filename = VBA.Constants.vbNullString Then
'MsgBox "File doesn't Exist"
'Else
'Workbooks.Open "C:\Users\dell\Google Drive\Goods and Service Tax-GST\Arun Kumar\Testing Master Payroll.xlsx"
'End If

        
        
        
        
'ActiveWorkbook.FollowHyperlink Address:=folderpathwithname
'Dim PayrollName As String
'Dim PreviousMonthPayroll As String
'PayrollName = "Master Payroll" & "_" & Me.cmbmonth1.Value & " " & Me.cmbyear.Value & "_" & Me.cmbCompanyName.Value
'PreviousMonthPayroll = "Master Payroll" & "_" & Me.cmbmonth1.Value - 1 & " " & Me.cmbyear.Value & "_" & Me.cmbCompanyName.Value

'If Dir(folderpathwithname & "\" & PayrollName & ".xlsx") = "" Then
'GoTo folderpathwithname1


'Dir(folderpathwithname1 & "\" & PreviousMonthPayroll & ".xlsx").Copy

'Dir(folderpathwithname).PasteSpecial


'Workbooks.Add
'ActiveWorkbook.SaveAs folderpathwithname & "\" & PayrollName & ".xlsx"
'ActiveWorkbook.Sheets.Add.Name = "Payroll Sheet"
'ActiveWorkbook.Save
'ActiveWorkbook.Close
'Dir(folderpathwithname, vbDirectory) = vbNullString

End Sub

請求大家幫忙

謝謝普雷特

假設您的組合框是 cboMonth。 它存儲以下數組:

1   Jan
2   Fem
3   Mar
4   Apr

等等...

第一列是隱藏的,第二列是顯示的。 所以,從表格的其他任何地方,你都可以說(這是“空氣代碼”,我的 VBA 技能有點生疏)

Set A = 0
' If the month is January, you need to go back to December
If me.cboMonth.Value = 1 then
  A = 12
'Otherwise, just subtract 1 from the month number
else
  A = me.cboMonth.Value - 1
End If

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM