代码不将数据保存到另一张表中

时间:2017-07-17 05:14:09

标签: excel-vba userform vba excel

我有下面的代码,它必须削减数据并复制到另一张名为data_base的表中,但它没有发生在username-password.xlsx中复制数据而我面临的另一个问题是我尝试关闭userform Logout当我点击退出按钮时,按钮应出现在第34页的工作表(" data1")。范​​围(" B1")。值=日期& " " &安培;时间'作为下标超出范围。

Private Sub CommandButton1_Click()
Dim username As String
Dim password As String

username = TextBox1.Text
password = TextBox2.Text



Dim info
info = IsWorkBookOpen("D:\TMS_Project\username-password.xlsx")

If info = False Then
Workbooks.Open ("D:\TMS_Project\username-password.xlsx")
End If

Dim x As Integer
x = 2
Do While Cells(x, 1).Value <> ""
If Cells(x, 1).Value = username And Cells(x, 2).Value = password Then
MsgBox "Welcome!"
Worksheets("data1").Range("A1").Value = Date & " " & Time
Selection.NumberFormat = "m/d/yyyy h:mm AM/PM"

UserForm1.Hide
ActiveWorkbook.Close True
End
Else
x = x + 1
End If

Loop
MsgBox "Please check your username or password!"
ActiveWorkbook.Close True
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.SetFocus


End Sub

Private Sub CommandButton6_Click()

Worksheets("data1").Range("B1").Value = Date & " " & Time
Selection.NumberFormat = "m/d/yyyy h:mm AM/PM"

ThisWorkbook.Save

Worksheets("data1").Range("A1:B1").Select

Selection.Cut

Unload Me

getlogindata

ActiveWorkbook.Close True

End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

If CloseMode = vbFormControlMenu Then
Cancel = True
End If

End Sub


Sub getlogindata()

Dim info
info = IsWorkBookOpen("D:\TMS_Project\Log_Details..xlsx")
' we open the workbook if it is closed
If info = False Then
Workbooks.Open ("D:\TMS_Project\Log_Details..xlsx")
End If
Worksheets("data_base").Activate
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("data_base").Range(Cells(erow, 1), Cells(erow, 2))

ActiveWorkbook.Save
ActiveWorkbook.Close

Application.Quit

End Sub

任何帮助都会受到赞赏,因为我遇到了这个问题,无法使用我的登录和注销系统。

1 个答案:

答案 0 :(得分:0)

我不认为你有一张名为&#34; data1&#34;在当前有效工作簿中。尝试将参考完全限定为

工作簿(&#34; Log_Details..xlsx&#34;)。工作表(&#34; data1&#34;)。范​​围(&#34; B1&#34;)=日期&amp; &#34; &#34; &安培;时间

或首先激活所需的工作簿