【问题标题】:code not saving the data into another sheet代码不将数据保存到另一个工作表中
【发布时间】:2017-07-17 23:14:30
【问题描述】:

我有下面的代码,它必须剪切数据并复制到另一个名为 data_base 的工作表中,但它没有发生将数据复制到 username-password.xlsx 中,我面临的另一个问题是,一旦我尝试关闭用户表单注销当我单击注销按钮时,应该会出现按钮,它在“Worksheets("data1").Range("B1").Value = Date & " " & Time ' as subscript out of range.

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

感谢任何帮助,因为我遇到了这个问题,无法继续使用我的登录和注销系统。

【问题讨论】:

    标签: excel userform vba


    【解决方案1】:

    我认为您的当前活动工作簿中没有名为“data1”的工作表。尝试将参考完全限定为

    Workbooks("Log_Details..xlsx").worksheets("data1").range("B1")= Date & " " & Time

    或先激活所需的工作簿

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多